解决 Fragment null must be a public static class to be properly recreated
·
今天同事遇到这个问题。后来查了一下原因是:不允许在实例化对象的时候重写方法。
以下两种都是不允许的:
1. 匿名类直接new 加重写的方式。
getSupportFragmentManager().beginTransaction().add(R.id.activitymap, new Fragment(){
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { BasePager basePager = getBasePager();
if(basePager!=null) {
return basePager.rootView;
}
return null;
} }).commit();
2. 实例对象时,有直接重写的方法。
MyFragment fragment = new MyFragment(){
//不能在实例化对象的时候重写的方法。
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { BasePager basePager = getBasePager();
if(basePager!=null) {
return basePager.rootView;
}
};
这时把MyFragment加进去也照样会出现错误。
更多推荐



所有评论(0)