我的代码上出现了 NPE。我正在尝试获取所选 RadioButton 的值。当我单击按钮时,异常被触发,不知道为什么,我可以想象它采取了错误的观点......
import android.app.Activity;
import android.app.DialogFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
public class CategoryDialog extends DialogFragment{
Button btnDone;
RadioGroup rg;
RadioButton radioBtn;
String selectedCategory;
Communicator communicator;
public static interface Communicator{
public void onCategorySelected(String category);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
communicator = (Communicator) activity;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_video_detail_category_dialog, container);
this.getDialog().setTitle(getString(R.string.z2_videodetail_category_dialog_title));
btnDone = (Button) view.findViewById(R.id.category_done_btn);
rg = (RadioGroup) view.findViewById(R.id.category_radioGroup);
btnDone.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int selectedId = rg.getCheckedRadioButtonId();
radioBtn = (RadioButton) v.findViewById(selectedId);
selectedCategory = radioBtn.getText().toString();
if(v.getId() == R.id.category_done_btn){
communicator.onCategorySelected(selectedCategory);
dismiss();
}
}
});
return view;
}
}
XML文件
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<RadioGroup
android:id="@+id/category_radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" >
<RadioButton
android:id="@+id/category_radioGroup_radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/z2_videodetail_category_dialog_radioButton1" />
<RadioButton
android:id="@+id/category_radioGroup_radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/z2_videodetail_category_dialog_radioButton2" />
<RadioButton
android:id="@+id/category_radioGroup_radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/z2_videodetail_category_dialog_radioButton3" />
<RadioButton
android:id="@+id/category_radioGroup_radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/z2_videodetail_category_dialog_radioButton4" />
</RadioGroup>
<Button
android:id="@+id/category_done_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/category_radioGroup"
android:text="@string/z2_videodetail_category_dialog_done_btn" />
</RelativeLayout>
非常感谢您,非常感谢您的帮助!:)