我的代码上有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>
非常感谢,非常感谢您的帮助! :)
答案 0 :(得分:2)
您将片段视图命名为视图,但您也可以在onClick方法中使用变量视图来引用按钮本身,因此当您按ID查找单选按钮时,程序将查看错误的视图。正在查看按钮内部而不是查看您的片段。重命名onClick方法上的变量视图以避免这种混淆
你必须将视图对象声明为final,这样它才能在匿名内部类中使用!
final View view = inflater.inflate(R.layout.fragment_video_detail_category_dialog, container);
this.getDialog().setTitle(getString(R.string.z2_videodetail_category_dialog_title));
final RadioGroup rg = (RadioGroup) view.findViewById(R.id.category_radioGroup);
@Override
public void onClick(View v) { //Change it from view to v or whatever you want
int selectedId = rg.getCheckedRadioButtonId();
radioBtn = (RadioButton) view.findViewById(selectedId);
selectedCategory = radioBtn.getText().toString();
if(v.getId() == R.id.category_done_btn){ //And update this value
communicator.onCategorySelected(selectedCategory);
dismiss();
}
}