0

我正在尝试在我的 SherlockFragment 活动中实现 Radio 组......但无法做到这一点。当我运行应用程序并且屏幕上没有显示任何活动时,我收到“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;
}

}
4

1 回答 1

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 ...
            }
        }
    }

}
于 2013-08-03T21:24:32.543 回答