2013-06-23 67 views
0

問題我想使用的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) 

是否有人有我一個暗示?

+0

請發佈錯誤的logcat跟蹤。 – Evilunclebill

+0

什麼錯誤?儘量把它放在一個try-catch,並找出它是什麼類型的異常.. –

+0

有了嘗試捕捉我得到如下:06-23 14:43:30.245:I/CHAT:(21704):java.lang中。 NullPointerException異常 – Scuido

回答

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

      ... 
     } 
    }) 
+0

我仍然得到異常:06-23 15:10:28.635:I/CHAT:(23312):顯示java.lang.NullPointerException。我必須使用最後的視圖decleration – Scuido

+0

沒關係,這很奇怪。它適用於txt_LoginUserPW,但對於txtLoginUserPhoneNumber,我將Null作爲參考。我會盡力找出原因。 – Scuido

+0

我想出了問題......我通過R.id得到了一個錯誤的組件......感謝你們幫助我。 – Scuido