AlertDialog 内的 EditText 始终为 null
我已经搜索了线程,但到目前为止我还没有找到我要找的东西。 我创建了一个显示的自定义警报对话框,我几乎可以用它做任何事情。它的自定义对话框由 3 个 TextView 和 3 个 EditText 组成,但每当我需要获取 EditText 时,我都会得到一个空组件。
从我的 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 字段:
EditText txtAccName = (EditText) findViewById(R.id.txtEditName);
Log.d("#############################", "txtAccName="+txtAccName.getText().toString());
但是,在执行此操作时,第一行运行良好,第二行会导致崩溃,并且控件为空。 这是我用来创建我需要的自定义 AlertDialog 的 Overwrite 方法。
@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;
}
非常感谢任何帮助。
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.
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"
>
I'm trying to get the EditText field with:
EditText txtAccName = (EditText) findViewById(R.id.txtEditName);
Log.d("#############################", "txtAccName="+txtAccName.getText().toString());
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请注意您的所有方法调用是如何在匿名内部类中解析的。
findViewById
是存在于视图和 Activity 上的方法。此方法在您的活动上的版本会在活动窗口的视图层次结构中搜索视图。视图上的版本搜索该视图实例和所有附加的子视图。您对有问题的代码行的调用:
正在解析为
Activity#findViewById
。但是您的对话框的布局并未附加到您的活动窗口,而是附加到对话框。您可以通过多种方式找到正确的视图引用,但在您的情况下最简单的可能是从您膨胀的布局的根搜索:Take note of how all of your method calls are being resolved within your anonymous inner classes.
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:
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:当您使用
findViewById
时,您隐式引用了当前活动。您必须更改的部分是在
您尝试解决问题时。您还必须将
AlertDialog
对象ad
的范围更改为类范围,并确保它不为 null,然后再在其中搜索视图。差不多就这样了。
When you use
findViewById
you implicitly refer to the current activity. The part you have to change isinstead of
when you attempt to resolve it. You'll also have to change the scope of the
AlertDialog
objectad
to class-wide and make sure that it's not null, before you search for views in it.That's pretty much it.
adamp 的解释很棒。但这个方法对我不起作用。对我来说最简单的方法是这样的:
希望这对某人有帮助。
The explanation by adamp is great. But that method didn't work for me. The simplest way for me is like this:
Hope this helps somebody.