0

ラジオボタンから値を取得したい。以下のコードを実行しています。しかし、アラート ダイアログの [OK] ボタンをクリックすると、アプリが強制的に閉じられます。どうしてですか。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

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 に答える
0

ダイアログをキャンセル可能に設定してみてください。

dialog.setCancelable(true);
dialog.setCanceledOnTouchOutside(false)
于 2013-04-30T11:40:36.780 に答える