AlertDialog 中的 EditText 始终为空

EditText inside AlertDialog always null(AlertDialog 中的 EditText 始终为空)

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

问题描述

我已经搜索了线程,但到目前为止我还没有找到我要找的东西.我创建了一个显示的自定义警报对话框,我几乎可以用它做任何事情.它的自定义对话框由 3 个 TextViews 和 3 个 EditText 组成,但是每当我需要获取 EditText 时,我都会得到一个空组件.

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 文件

From my xml file

 <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());

但是这样做时,第一行运行良好,第二行导致崩溃,控件为空.这是我用来创建我需要的自定义 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;
}

非常感谢任何帮助.

推荐答案

注意所有方法调用是如何在匿名内部类中解析的.

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 始终为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:AlertDialog 中的 EditText 始终为空

基础教程推荐