我正在使用由片段组成的导航栏,每当我打开下面的片段时,应用程序崩溃
public class FirstFragment extends Fragment{
View myView;
private RadioGroup radioGroup;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
myView = inflater.inflate(first_layout, container, false);
addListenerOnButton();
return myView;
}
public void addListenerOnButton() {
radioGroup = (RadioGroup) getView().findViewById(R.id.radio);
}
}
现在我做了一些实验来找到问题,结果就是这一行
radioGroup = (RadioGroup) getView().findViewById(R.id.radio);
任何人都可以解释如何解决它吗?谢谢
答案 0 :(得分:1)
试试这个:
addListenerOnButton(myView);
在addListenerOnButton()方法中使用:
public void addListenerOnButton(View v) {
radioGroup = (RadioGroup) v.findViewById(R.id.radio);
}