我正在尝试在我的SherlockFragment活动中实现Radio组....但是无法做到这一点。我得到了#34; java.lang.NullPointer"我运行应用程序时屏幕上没有显示任何活动。
这是我的代码
public class Fragment_1 extends SherlockFragment {
RadioGroup cls;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
final View V= inflater.inflate(R.layout.fragment_1, container, false);
cls= (RadioGroup) V.findViewById(R.id.radioclass);
cls.clearCheck();
OnClickListener listener = new OnClickListener(){
/@Override
public void onClick(View V) {
// TODO Auto-generated method stub
RadioButton rb = (RadioButton) V;
clss=rb.getText().toString();
}
};
RadioButton rb1 = (RadioButton)V. findViewById(R.id.radioButton1);
rb1.setOnClickListener(listener);
RadioButton rb2 = (RadioButton)V. findViewById(R.id.radioButton2);
rb2.setOnClickListener(listener);
rb2.setChecked(true);
return V;
}
}
答案 0 :(得分:0)
我不认为我会通过发布剪切和粘贴解决方案为您做任何好处,但这可能是 很有帮助(如果你发现一些错误,不要太惊讶):
public class Fragment_1 extends SherlockFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
// inflate ...
View view = inflater.inflate(R.layout.fragment_1, container, false);
return view;
}
public void onActivityCreated (Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Button rb1 = (Button) getActivity().findViewById(R.id.radioButton1);
//then do other buttons ...
rb1.setOnClickListener(next_Listener);
//then do other buttons ...
}
private OnClickListener next_Listener = new OnClickListener() {
public void onClick(View v) {
//xml find out which radio button has been checked ...
RadioGroup radio_grp=(RadioGroup)getActivity().findViewById(R.id.radio_grp); //change or leave out this line (I've put it in because you might find it useful later )
RadioButton rb1=(RadioButton)getActivity().findViewById(R.id.radioButton1); //you dont need to do this again if global ...
if(rb1.isChecked() == true) {
//toast ... button has been selected ...
}
}
}
}