我有两个片段,MyFirst片段和MySecond片段。MySecond片段继承自MyFirst片段。
类定义如下:
public class MyFirstFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
...
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
...
// Check view and map, request to recreate if each of them is null
if(MyFirstFragment.this.getView() == null || googleMap == null) {
Toast.makeText(getActivity().getApplicationContext(), R.string.my_message, Toast.LENGTH_LONG).show();
return;
}
}
}
public class MySecondFragment extends MyFirstFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
...
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
...
}
}
我的问题是,在MySecondFragment的onActivityCreated()方法中,
super.onActivityCreated(savedInstanceState);
调用了它的父类。由于我想隐藏这个方法在父类中的功能(对此片段没有用处),因此我已经在MySecondFragment中添加了onActivityCreated()方法。问题是,如果我删除这行代码,那么我会运行时错误,抛出“SuperNotCalledException”。
你认为呢?看起来我必须扩展Fragment类,而不是扩展MyFirstFragment。我在MyFirstFragment中有一些变量,我需要它们在MySecondFragment中使用。