問題描述
我已經(jīng)搜索了線程,但到目前為止我還沒有找到我要找的東西.我創(chuàng)建了一個顯示的自定義警報對話框,我?guī)缀蹩梢杂盟鋈魏问虑?它的自定義對話框由 3 個 TextViews 和 3 個 EditText 組成,但是每當(dāng)我需要獲取 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());
但是這樣做時,第一行運(yùn)行良好,第二行導(dǎo)致崩潰,控件為空.這是我用來創(chuàng)建我需要的自定義 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;
}
非常感謝任何幫助.
推薦答案
注意所有方法調(diào)用是如何在匿名內(nèi)部類中解析的.
Take note of how all of your method calls are being resolved within your anonymous inner classes.
findViewById
是一種存在于視圖和您的活動中的方法.您的活動上的此方法版本在活動窗口的視圖層次結(jié)構(gòu)中搜索視圖.視圖版本搜索該視圖實(shí)例和所有附加的子視圖.
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.
您對有問題的代碼行的調(diào)用:
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);
這篇關(guān)于AlertDialog 中的 EditText 始終為空的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!