新手问题:增加RadioGroup监听器,运行出错

zz997788 zz997788 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
就是下面的代码,如果不加上红色部分,运行没问题。加上后运行出错:
Sorry! The application XXX has stopped.unexpectedly.Please try again.

	
public static class PlaceholderFragment extends Fragment
{
//对控件对象进行声明
private RadioGroup genderGroup=null;
private RadioButton femaleButton=null;
private RadioButton maleButton=null;
public PlaceholderFragment()
{
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
//通过控件的ID来得到代表控件的对象
genderGroup=(RadioGroup)getActivity().findViewById(R.id.genderGrop);
femaleButton=(RadioButton)getActivity().findViewById(R.id.femaleButton);
maleButton=(RadioButton)getActivity().findViewById(R.id.maleButton);

//为RadioGroup设置监听器

genderGroup.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(RadioGroup group, int checkedId)
{
if(femaleButton.getId()==checkedId){
System.out.println("female");
}
else if(maleButton.getId()==checkedId){
System.out.println("male");
}
}
});

return rootView;
}
}

}


请大狭帮我查查看是什么原因
...全文

相关推荐

评论 7
收藏 已收藏
分享
返回顶部
zz997788
Android 80,489 成员 91,375 内容