0

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

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

提示.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

4 回答 4

1

RadioButton是空的,当你试图访问它时,它会抛出NullPointerException,所以让你 RadioButton喜欢这样

 RadioButton  radioButton = (RadioButton)promptsView. findViewById(selectedId);
于 2013-04-30T11:44:06.857 回答
0

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

dialog.setCancelable(true);
dialog.setCanceledOnTouchOutside(false)
于 2013-04-30T11:40:36.780 回答
0

尝试进行以下更正:

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

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

于 2013-04-30T11:46:30.760 回答
0

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

// find the radiobutton by returned id
final RadioButton  radioButton = (RadioButton) MyActivity.this.findViewById(selectedId);
于 2013-04-30T11:52:29.030 回答