我正在使用由 fragment 组成的导航栏,每当我打开下面的 fragment 时,应用程序就会崩溃
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);
谁能解释一下如何修复它?谢谢
最佳答案
试试这个:
addListenerOnButton(myView);
并且在 addListenerOnButton() 方法中使用:
public void addListenerOnButton(View v) {
radioGroup = (RadioGroup) v.findViewById(R.id.radio);
}
关于java - 添加特定代码行后,Android 应用程序不断崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41321360/