EditText inside AlertDialog always null(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 始终为空
基础教程推荐
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01