[英]AlertDialog, Problems with editText components
I tried to use that AlertDialog thing but I got an error if I try to access the editText components. 我尝试使用AlertDialog东西,但是如果尝试访问editText组件,则会出现错误。 I have my own layout with two editText fields. 我有两个editText字段自己的布局。
It seems that I cannot access the components. 看来我无法访问这些组件。
I know there are lots of questions about this stuff but I didn't found a hint how to solve my problem. 我知道关于这些东西有很多问题,但是我没有找到如何解决问题的提示。
Code: 码:
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();
}
EDIT: 编辑:
LocCat: 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)
Does Someone has a hint for me? 有人对我有提示吗?
You can't get the views from a DialogInterface object. 您无法从DialogInterface对象获取视图。 Instead of initializing the views in onClick(), you can do this 无需在onClick()中初始化视图,您可以执行此操作
LayoutInflater inflater = this.getLayoutInflater();
View view = inflater.inflate(R.layout.login_laylout, null, false);
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(view)
.setPositiveButton("Login", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Dialog f = (Dialog) dialog;
EditText txtLoginUserPhoneNumber = (EditText) view.findViewById(R.id.txt_LoginUserPhoneNumber);
EditText txtLoginUserPW = (EditText) view.findViewById(R.id.txt_LoginUserPW);
...
}
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.