2014-06-08 29 views
1

我想在組按鈕中使用RadioButton和所有在dialog框架,但是當我想從我的RadioButton獲得文本,我得到一個NullPointerException空指針異常當我使用單選按鈕

有我的佈局xml文件:

LayoutInflater layoutInflater = LayoutInflater.from(context); 

         View promptView = layoutInflater.inflate(R.layout.prompt_permission, null); 

         final RadioGroup radioSexGroup = (RadioGroup) promptView.findViewById(R.id.droit); 

         AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context); 

         // set prompts.xml to be the layout file of the alertdialog builder 
         alertDialogBuilder.setView(promptView); 

         final EditText input = (EditText) promptView.findViewById(R.id.editTextMailUserInput); 


         // setup a dialog window 
         alertDialogBuilder 
           .setCancelable(false) 
           .setPositiveButton("OK", new android.content.DialogInterface.OnClickListener() { 
              public void onClick(DialogInterface dialog, int id) {         
               //input.setText(input.getText()); 

               int selectedId = radioSexGroup.getCheckedRadioButtonId(); 

               RadioButton radioSexButton =(RadioButton) findViewById(selectedId); 
               System.out.println(radioSexButton.getText()); 
              } 
             }) 
           .setNegativeButton("Cancel", 
             new DialogInterface.OnClickListener() { 
              public void onClick(DialogInterface dialog, int id) { 
               dialog.cancel(); 
              } 
             }); 

         // create an alert dialog 
         AlertDialog alertD = alertDialogBuilder.create(); 

         alertD.show();` 

這是我的logcat錯誤:

06-08 16:10:14.420: E/AndroidRuntime(3645): FATAL EXCEPTION: main 
06-08 16:10:14.420: E/AndroidRuntime(3645): java.lang.NullPointerException 
06-08 16:10:14.420: E/AndroidRuntime(3645):  at com.example.poc_cubbyhole.MainActivity$4.onClick(MainActivity.java:190) 
06-08 16:10:14.420: E/AndroidRuntime(3645):  at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:159) 
06-08 16:10:14.420: E/AndroidRuntime(3645):  at android.os.Handler.dispatchMessage(Handler.java:99) 
06-08 16:10:14.420: E/AndroidRuntime(3645):  at android.os.Looper.loop(Looper.java:123) 
06-08 16:10:14.420: E/AndroidRuntime(3645):  at android.app.ActivityThread.main(ActivityThread.java:3683) 
06-08 16:10:14.420: E/AndroidRuntime(3645):  at java.lang.reflect.Method.invokeNative(Native Method) 
06-08 16:10:14.420: E/AndroidRuntime(3645):  at java.lang.reflect.Method.invoke(Method.java:507) 
06-08 16:10:14.420: E/AndroidRuntime(3645):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
06-08 16:10:14.420: E/AndroidRuntime(3645):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
06-08 16:10:14.420: E/AndroidRuntime(3645):  at dalvik.system.NativeStart.main(Native Method) 
+0

請張貼logcat的錯誤跟蹤? –

+0

我添加了我的LogCat –

回答

1

你應該這樣做:

public void onClick(DialogInterface dialog, int id) {         
    //input.setText(input.getText()); 

    int selectedId = radioSexGroup.getCheckedRadioButtonId(); 

    // change this part like this 
    RadioButton radioSexButton =(RadioButton) promptView.findViewById(selectedId); 

    System.out.println(radioSexButton.getText()); 
} 
+0

你是對的,這就是解決我的問題,謝謝:D –

+0

你可以投我的答案,謝謝:) –