問題我想使用的AlertDialog的事情,但我得到了一個錯誤,如果我試圖訪問EDITTEXT組件。我有我自己的兩個editText字段的佈局。AlertDialog,與EDITTEXT組件
看來我無法訪問組件。
我知道有很多關於這個東西的問題,但我沒有發現一個提示怎麼解決我的問題。
代碼:
protected void showLoginDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
// Get the layout inflater
LayoutInflater inflater = this.getLayoutInflater();
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(inflater.inflate(R.layout.login_laylout, null, false))
.setPositiveButton("Login", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Dialog f = (Dialog) dialog;
EditText txtLoginUserPhoneNumber = (EditText) f.findViewById(R.id.txt_LoginUserPhoneNumber);
EditText txtLoginUserPW = (EditText) f.findViewById(R.id.txt_LoginUserPW);
/* ERROR HERE! */
Log.i("CHAT: ", txtLoginUserPhoneNumber.getText().toString() + " " + txtLoginUserPW.getText().toString());
}
})
.setNeutralButton("Create new account", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
builder.create().show();
}
編輯:
LocCat:
06-23 14:41:28.495: W/dalvikvm(21233): threadid=1: thread exiting with uncaught exception (group=0x40b04930)
06-23 14:41:28.505: E/AndroidRuntime(21233): FATAL EXCEPTION: main
06-23 14:41:28.505: E/AndroidRuntime(21233): java.lang.NullPointerException
06-23 14:41:28.505: E/AndroidRuntime(21233): at com.example.listviewexample.ListViewExampleActivity$2.onClick(ListViewExampleActivity.java:96)
06-23 14:41:28.505: E/AndroidRuntime(21233): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
06-23 14:41:28.505: E/AndroidRuntime(21233): at android.os.Handler.dispatchMessage(Handler.java:99)
06-23 14:41:28.505: E/AndroidRuntime(21233): at android.os.Looper.loop(Looper.java:137)
06-23 14:41:28.505: E/AndroidRuntime(21233): at android.app.ActivityThread.main(ActivityThread.java:5041)
06-23 14:41:28.505: E/AndroidRuntime(21233): at java.lang.reflect.Method.invokeNative(Native Method)
06-23 14:41:28.505: E/AndroidRuntime(21233): at java.lang.reflect.Method.invoke(Method.java:511)
06-23 14:41:28.505: E/AndroidRuntime(21233): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-23 14:41:28.505: E/AndroidRuntime(21233): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-23 14:41:28.505: E/AndroidRuntime(21233): at dalvik.system.NativeStart.main(Native Method)
是否有人有我一個暗示?
請發佈錯誤的logcat跟蹤。 – Evilunclebill
什麼錯誤?儘量把它放在一個try-catch,並找出它是什麼類型的異常.. –
有了嘗試捕捉我得到如下:06-23 14:43:30.245:I/CHAT:(21704):java.lang中。 NullPointerException異常 – Scuido