AlertDialog中的EditText始终为null [英] EditText inside AlertDialog always null

查看:84
本文介绍了AlertDialog中的EditText始终为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索了线程,但是到目前为止,我还没有找到想要的东西. 我创建了一个自定义的警告对话框",该对话框可以显示,并且几乎可以执行任何操作.它的自定义对话框由3个TextViews和3个EditText组成,但是每当我需要获取EditText时,我都会得到一个null组件.

I've search over the threads but so far I have not found what I'm looking for. I created a custom Alert Dialog that show up and I can do almost anything with it. It custom dialog is made of 3 TextViews and 3 EditText but whenever I need to get the EditText I get a null component.

从我的xml文件中

 <TextView android:layout_alignParentTop="true" 
    android:id="@+id/txtAccName" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Name"
    android:textSize="23dp"
    >

</TextView>
<EditText android:id="@+id/txtEditName" 
    android:layout_below="@+id/txtAccName" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="" 
    android:maxLength="20"
    >

我正在尝试通过以下方式获取EditText字段:

I'm trying to get the EditText field with:

    EditText txtAccName = (EditText) findViewById(R.id.txtEditName);
    Log.d("#############################", "txtAccName="+txtAccName.getText().toString());

但是,这样做时,第一行效果很好,第二个主目录会导致崩溃,并且控件为null. 这是我用来创建所需的自定义AlertDialog的Overwrite方法.

But when doing this, the first line works well, the second home causes a crash and the control is null. This is the Overwrite method that I use to create the custom AlertDialog that I need.

    @Override
protected Dialog onCreateDialog(int id) {
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final View layout = inflater.inflate(R.layout.accountdialog, (ViewGroup) findViewById(R.id.accDialog));

    AlertDialog.Builder builder = new AlertDialog.Builder(this);

    builder.setNegativeButton(android.R.string.cancel,new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {

        }
        });
    builder.setPositiveButton(android.R.string.ok,
            new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                EditText txtAccName = (EditText) findViewById(R.id.txtEditName);
                //EditText txtAccCur = (EditText) findViewById(R.id.txtEditCur);
                //EditText txtAccCountry = (EditText) findViewById(R.id.txtEditCountry);
                //DBConn db = new DBConn(Accounts.this.getApplicationContext(), "msaverdb", null, 0);
                Log.d("#############################", "CALLING db.insertAccount");
                Log.d("#############################", "txtAccName="+txtAccName.getText().toString());
                //db.insertAccount(txtAccName.getText().toString(), txtAccCountry.getText().toString(), 
                //      txtAccCur.getText().toString());
                Accounts.this.removeDialog(ACC_DIALOG);
            }
            });
    builder.setNegativeButton(android.R.string.cancel,
            new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                Accounts.this.removeDialog(ACC_DIALOG);
            }
            });
    builder.setView(layout);
    AlertDialog ad = builder.create();
    return ad;
}

我们非常感谢您的帮助.

Any help is most appreciated.

推荐答案

请注意如何在匿名内部类中解决所有方法调用.

Take note of how all of your method calls are being resolved within your anonymous inner classes.

findViewById是一种存在于视图和活动上的方法.活动上此方法的版本在活动窗口的视图层次结构内搜索视图.视图上的版本会搜索该视图实例和所有附加的子代.

findViewById is a method that exists on views and on your activity. The version of this method on your activity searches for a view within the activity window's view hierarchy. The version on views searches that view instance and all attached children.

您对有问题的代码行的调用:

Your call on the problematic line of code:

EditText txtAccName = (EditText) findViewById(R.id.txtEditName);

正在解析为Activity#findViewById.但是对话框的布局未附加到活动窗口,而是已附加到对话框.您可以通过几种方式找到正确的视图引用,但最简单的情况可能是从夸大的布局的根目录中进行搜索:

is resolving to Activity#findViewById. But your dialog's layout is not attached to your activity window, it's attached to the dialog. You can find the correct view reference in several ways but the simplest in your case is probably to search from the root of the layout that you inflated:

EditText txtAccName = (EditText) layout.findViewById(R.id.txtEditName);

这篇关于AlertDialog中的EditText始终为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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