2013-04-30 24 views
0

我想从单选按钮中检索值。我正在做下面的代码。但是当我点击确定按钮的警报对话框我的应用程序被强制关闭。为什么是这样。 AI不retriving值正确从alertDialog中的radioButton中检索值

final SharedPreferences settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE); 
    if (settings.getBoolean("isFirstRun", true)) 
    { 
     LayoutInflater li = LayoutInflater.from(this); 
     View promptsView = li.inflate(R.layout.prompts, null); 
     AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this); 
     alertDialogBuilder.setView(promptsView); 
     final EditText userInput = (EditText) promptsView 
       .findViewById(R.id.editTextDialogUserInput); 

     final RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup1); 


    // set dialog message 
     alertDialogBuilder.setCancelable(false).setPositiveButton("OK", 
       new DialogInterface.OnClickListener() 
       { 
      public void onClick(DialogInterface dialog,int which) 
      { 
       int selectedId = radioGroup.getCheckedRadioButtonId();     
       // find the radiobutton by returned id 
        final RadioButton radioButton = (RadioButton) findViewById(selectedId); 
        String radio_value = radioButton.getText().toString(); 
        data_mode=Integer.parseInt(radio_value); 

       String value = userInput.getText().toString(); 
       currentIntervalChoice=Integer.parseInt(value); 

       SharedPreferences.Editor editor = settings.edit(); 
       editor.putBoolean("isFirstRun", false); 
       editor.putInt("currentIntervalChoice", currentIntervalChoice); 
       editor.putInt("data_mode", data_mode); 
       editor.commit();    
       dialog.dismiss(); 
      } 
       }); 
     // create alert dialog 
     AlertDialog alertDialog = alertDialogBuilder.create(); 
     // show it 
     alertDialog.show();  
    } 

prompt.xml

 <?xml version="1.0" encoding="utf-8"?> 
     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/layout_root" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" 
android:padding="10dp" > 


    <TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Type Interval : " 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 

<EditText 
    android:id="@+id/editTextDialogUserInput" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:ems="10" 
    android:inputType="number" > 

    <requestFocus /> 
</EditText> 

<RadioGroup 
    android:id="@+id/radioGroup1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" > 

    <RadioButton 
     android:id="@+id/radio1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="RadioButton" /> 

    <RadioButton 
     android:id="@+id/radio2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="RadioButton" /> 
    </RadioGroup> 

    </LinearLayout> 
+0

是您的RadioGroup R.id.radioGroup1在AlertDialog的视图(PromptsView)内部?粘贴你的布局XML。 – SKK 2013-04-30 11:38:31

+0

请发布您的logcat。 – 2013-04-30 11:39:15

+0

是AlertDialog中的'RadioButton'吗? – Pragnani 2013-04-30 11:39:27

回答

1

RadioButton为空,当您试图访问它,它会扔NullPointerException所以得到你的RadioButton这样的

RadioButton radioButton = (RadioButton)promptsView. findViewById(selectedId); 
0

尝试设置你的对话框取消:

dialog.setCancelable(true); 
dialog.setCanceledOnTouchOutside(false) 
+0

我不希望它被取消。 – user2310041 2013-04-30 11:41:56

0

尝试使这些修正:

final RadioGroup radioGroup = (RadioGroup) promptsView.findViewById(R.id.radioGroup1);

onClick();final RadioButton radioButton = (RadioButton) promptsView.findViewById(selectedId);

+0

你完成了你想要达到的目标吗? – 2013-04-30 11:48:34

0

我想你会得到一个NullPointerException尝试使用选定的单选按钮时。如果您的活动类名称为MyActivity,请尝试如下所示:

// find the radiobutton by returned id 
final RadioButton radioButton = (RadioButton) MyActivity.this.findViewById(selectedId); 
+0

它不工作 – user2310041 2013-04-30 11:59:13