我尝试实现 RadioGroup 视图并将其放在 DialogFragment 中,但出现了问题。我已经按照这个教程
http://developer.android.com/guide/topics/ui/controls/radiobutton.html
而是在 DialogFragment 中初始化 Activity,视图:
public class MenuDialog extends DialogFragment {
public MenuDialog(){}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.menu_layout, container);
this.getDialog().setTitle("title");
return view;
}
public void onRadioButtonClicked(View view) {
boolean checked = ((RadioButton) view).isChecked();
switch(view.getId()) {
case R.id.radio1:
if(checked) {
Log.i("MENU", ((RadioButton)view).getText());
}
break;
case R.id.radio2:
if(checked) {
Log.i("MENU", ((RadioButton)view).getText());
}
break;
case R.id.radio3:
if(checked) {
Log.i("MENU", ((RadioButton)view).getText());
}
break;
case R.id.radio4:
if(checked) {
Log.i("MENU", ((RadioButton)view).getText());
}
break;
}
this.getDialog().dismiss();
}
}
xml:
<?xml version="1.0" encoding="utf-8"?>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/radio_group" >
<RadioButton
android:id="@+id/radio1"
android:text="20" />
<RadioButton
android:id="@+id/radio2"
android:text="40" />
<RadioButton
android:id="@+id/radio3"
android:text="60" />
<RadioButton
android:id="@+id/radio4"
android:text="80" />
</RadioGroup>
onRadioButtonClicked listener 它的实现方式与教程相同。现在,当启动对话框时,如果我点击一个单选按钮,我会收到此错误:
09-27 09:41:08.018: E/AndroidRuntime(7447): FATAL EXCEPTION: main
09-27 09:41:08.018: E/AndroidRuntime(7447): java.lang.IllegalStateException: Could not
find a method onRadioButtonClicked(View) in the activity class
android.view.ContextThemeWrapper for onClick handler on view class
android.widget.RadioButton with id 'radio1'
怎么了?