题主想要给一个fragment添加两个视图,分为有网络和无网络,有网的时候显示第一个,没网的时候显示第二个。
然而代码这样写一直报错,如下:
```public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = null;
if (CheckInternet.checkNetworkAvailable(getActivity().getApplication())) {
rootView = inflater.inflate(R.layout.fragment_index, container, false);
}else if (!CheckInternet.checkNetworkAvailable(getActivity().getApplication())) {
rootView = inflater.inflate(R.layout.fragment_index_no_internet, container, false);
}
return rootView;
}
debug到return rootView这一行的时候报错如图所示。

(ps:checkNetworkAvailable()是题主测试无误的方法,用来检测手机是否有网)
if (CheckInternet.checkNetworkAvailable(getActivity().getApplication())) {
rootView = inflater.inflate(R.layout.fragment_index, container, false);
}else {
rootView = inflater.inflate(R.layout.fragment_index_no_internet, container, false);
}
//题主可以这样试试
//如果报为空的话 应该是两个if条件都没执行