我想从单选按钮中检索值。我正在做下面的代码。但是当我单击警报对话框的确定按钮时,我的应用程序被强制关闭。为什么是这样。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>