AlertDialog,editText组件的问题

时间:2013-06-23 12:36:54

标签: java android alertdialog

我尝试使用AlertDialog的东西,但是如果我尝试访问editText组件,则会出错。我有自己的布局,有两个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)

有人对我有暗示吗?

1 个答案:

答案 0 :(得分:0)

您无法从DialogInterface对象获取视图。您可以执行此操作

,而不是在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);

           ...
        }
    })