简体   繁体   English

EditText.getText Java.lang空指针异常

[英]EditText.getText Java.lang nullpointer exception

I'm having a problem with my android program. 我的Android程序有问题。 I have about every thing working what i want, but now im trying to add a password dialog. 我有我想要的一切,但现在我试图添加密码对话框。

I want to show a Dialog with a EditText(enter password) and two buttons (ok, cancel). 我想显示一个带有EditText(输入密码)和两个按钮(确定,取消)的对话框。 (working fine till so far) (到目前为止工作正常)

When clicking on the OK button the password should be saved in mij SendData Class. 单击“确定”按钮时,密码应保存在mij SendData类中。

But every time I try to get the content of the EditText it gives a Java.lang NullPointerException. 但是每次我尝试获取EditText的内容时,它都会给出一个Java.lang NullPointerException。

Can some one help me please? 有人能帮助我吗?

(I don't have a stack trace as i don't know where to find it :( because i'm testing on android emulator.) (我没有堆栈跟踪,因为我不知道在哪里找到它:(因为我正在android模拟器上进行测试。)

If there any thing else you need, please feel free to ask. 如果您还有其他需要,请随时询问。

Here is the XML-FILE 这是XML-FILE

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <requestFocus />

    <EditText
        android:id="@+id/txt_wachtwoord"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:inputType="textPassword" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/DialogOK"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/txt_wachtwoord"
        android:layout_below="@+id/txt_wachtwoord"
        android:layout_marginTop="22dp"
        android:text="OK" />

    <Button
        android:id="@+id/Cancel"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/DialogOK"
        android:layout_alignBottom="@+id/DialogOK"
        android:layout_toRightOf="@+id/DialogOK"
        android:text="Cancel" />

</RelativeLayout>

Here is the code: 这是代码:

    package SebApp.Phone.Remote;

    import java.net.DatagramSocket;

    import android.app.Activity;
    import android.app.Dialog;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.TextView;
    import android.widget.EditText;
    import android.widget.Toast;
    import android.os.Vibrator; 
    import android.app.AlertDialog;
    import android.content.DialogInterface;

    public class PhoneRemote extends Activity {

    private static final int VIBRATE_TIME = 100;

    /** Called when the activity is first created. */
    public Vibrator vibrator = null;
    public sendData sd = new sendData();

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);    
        setContentView(R.layout.main);

        vibrator = (Vibrator)getSystemService(VIBRATOR_SERVICE);      

        //button to call the Dialog
        Button ww = (Button) findViewById(R.id.ww);

        //listener password button
        ww.setOnClickListener(wwOnClickListener);

    }

    Button.OnClickListener wwOnClickListener = new Button.OnClickListener() {    

        public void onClick(View v) {           
            try{
                final EditText ww = (EditText) findViewById(R.id.txt_wachtwoord);
                //set up dialog
                Dialog dialog = new Dialog(PhoneRemote.this);
                dialog.setContentView(R.layout.wachtwoord);
                dialog.setTitle("This is my custom dialog box");
                dialog.setCancelable(true);
                //there are a lot of settings, for dialog, cheout!

                //set up button
                Button button = (Button) dialog.findViewById(R.id.DialogOK);
                button.setOnClickListener(new OnClickListener() {
                    public void onClick(View v) {                 
                        try{
                            Toast.makeText(v.getContext(),"PWfield contains: " + ww.getText().toString(), Toast.LENGTH_SHORT).show();
                            //finish();
                        }catch(Exception e)
                        {
                            Toast.makeText(v.getContext(),e.toString(), Toast.LENGTH_SHORT).show();
                        }
                    }
                });           
                dialog.show();
            }catch(Exception E){
                Toast.makeText(v.getContext(),E.toString(), Toast.LENGTH_SHORT).show();
            }

        }
    };
}
 Button.OnClickListener wwOnClickListener = new Button.OnClickListener() {    

        public void onClick(View v) {           
            try{
                final EditText ww = (EditText) findViewById(R.id.txt_wachtwoord);
                //set up dialog
                Dialog dialog = new Dialog(PhoneRemote.this);
                dialog.setContentView(R.layout.wachtwoord);
                dialog.setTitle("This is my custom dialog box");
                dialog.setCancelable(true);
....

instead use 代替使用

Button.OnClickListener wwOnClickListener = new Button.OnClickListener() {    

        public void onClick(View v) {           
            try{

                Dialog dialog = new Dialog(PhoneRemote.this);
                dialog.setContentView(R.layout.wachtwoord);
                final EditText ww = (EditText) dialog.findViewById(R.id.txt_wachtwoord);
                //set up dialog
                dialog.setTitle("This is my custom dialog box");
                dialog.setCancelable(true);
....

you had declared EditText before setting the dialog view 您在设置对话框视图之前已经声明了EditText

I have yet to run your code. 我尚未运行您的代码。 However, I would say the variable wwOnClickListener is being initialized before the onCreate method. 但是,我会说变量wwOnClickListener是在onCreate方法之前初始化的。

Therefore the layout is not set, as the layout is set within the onCreate method. 因此,未设置布局,因为在onCreate方法中设置了布局。 To fix this I would pull out the following 为了解决这个问题,我会拉出以下内容

final EditText ww = (EditText) findViewById(R.id.txt_wachtwoord);

From the buttonlistener and make ww a global variable which is initialized in the onCreate method. 从buttonlistener中,使ww成为在onCreate方法中初始化的全局变量。

I would expect that will resolve the issue. 我希望这将解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM