0

我试图使用那个 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();
}

编辑:

定位猫:

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)

有人对我有提示吗?

4

1 回答 1

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);

           ...
        }
    })
于 2013-06-23T12:55:03.153 回答