位于对话框中的 EditText 上的 NullPointerException [英] NullPointerException on EditText located in Dialog

查看:16
本文介绍了位于对话框中的 EditText 上的 NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的 email_dialog.xml 布局:

<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 已正确定义,无需担心.我不断得到:

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

txtEA.getText().toString() 

抛出空点异常.我有正确的 R.id 值,我验证了 50 次,我在尝试访问 EditText 之前验证了 setContentView()> 和带有 setOnClickListener 的两个按钮效果很好.

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!

推荐答案

你应该这样做:

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

您忘记在对话框中搜索txtEA.

这篇关于位于对话框中的 EditText 上的 NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆