从alertDialog中的单选按钮中检索值

时间:2013-04-30 11:33:24

标签: android

我想从单选按钮中检索值。我正在做以下代码。但是,当我单击警报对话框的确定​​按钮时,我的应用程序被强制关闭为什么会如此。我没有正确地恢复价值

 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>

4 个答案:

答案 0 :(得分:1)

您的RadioButton为空,当您尝试访问它时,它会抛出NullPointerException,所以请像这样RadioButton

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

答案 1 :(得分:0)

尝试将对话框设置为可取消:

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

答案 2 :(得分:0)

尝试进行这些更正:

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

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

答案 3 :(得分:0)

我想在尝试使用选定的单选按钮时会出现NullpointerException。如果您的活动类名称是MyActivity,请尝试以下操作:

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