NullPointerException on EditText located in Dialog

2019-02-21 03:12发布

Here is my email_dialog.xml layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

<TableRow
    android:id="@+id/tableRow0"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</TableRow>

<TableRow
    android:id="@+id/tableRow1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <EditText
        android:id="@+id/txtEmailAddress5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textEmailAddress" 
        android:text="mFry@smithmicro.com"/>

</TableRow>

   <TableRow
    android:id="@+id/tableRow2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

       <Button
           android:id="@+id/btnCancelEmail"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:width="200px"
           android:text="Cancel" />

       <Button
           android:id="@+id/btnOkEmail"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:width="200px"
           android:text="Email" />

</TableRow>

Here is my method that calls and uses it:

void showEmailDialog() {
    // Final prevents the error in the newest onClick callback.
    final Dialog dialog = new Dialog(this);

    dialog.setContentView(R.layout.email_dialog);
    dialog.setTitle("Enter Email Address");
    dialog.setCancelable(true);

    final EditText txtEA = (EditText) findViewById(R.id.txtEmailAddress5);
    final Button cancelButton = (Button) dialog.findViewById(R.id.btnCancelEmail);
    final Button sendButton = (Button) dialog.findViewById(R.id.btnOkEmail);

    // set up cancel button
    cancelButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();
        }

    });

    // set up send button
    sendButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d(TAG, "sendButton onClick()");
            String emailAddress;

            Log.d(TAG, "sendButton onClick() - String emailAddress");

            Log.d(TAG,
                    "sendButton onClick() - txtEmailAddress = (EditText)");

            emailAddress = txtEA.getText().toString();
            Log.d(TAG,
                    "sendButton onClick() - emailAddress = getText().toString();");

            sendEmail(emailAddress);

            dialog.dismiss();

        }
    });

    dialog.show();
    //
}

TAG is properly defined no worries there. I keep getting:

txtEA.getText().toString() 

to throw the null point exception. I have the right R.id value which I verified like 50 times, I verify that the setContentView() is before i try to access the EditText and the two Buttons with setOnClickListener works perfect.

I could definitely use another set of eyes on this! I've dug around the similar questions and tried their solutions but none of them solved mine!

2条回答
三岁会撩人
2楼-- · 2019-02-21 03:37

You should do this:

 final EditText txtEA = (EditText) dialog.findViewById(R.id.txtEmailAddress5);

You forgot to search for the txtEA in dialog.

查看更多
走好不送
3楼-- · 2019-02-21 03:41
final EditText txtEA = (EditText) dialog.findViewById(R.id.txtEmailAddress5);

You must call findViewById on dialog.

查看更多
登录 后发表回答