2 qq 35881717 qq_35881717 于 2016.12.28 11:10 提问

Fragment null must be a public static class

Android里做一个RadioGroup实现底部导航页面,遇到了这样的问题:
FATAL EXCEPTION: main
java.lang.IllegalStateException: Fragment null must be a public static class to be properly recreated from instance state.
at android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:435)
at android.support.v4.app.BackStackRecord.replace(BackStackRecord.java:481)
at android.support.v4.app.BackStackRecord.replace(BackStackRecord.java:472)
at com.example.wu.mobileplayer.Activity.Activity.MainActivity.setFragment(MainActivity.java:72)
at com.example.wu.mobileplayer.Activity.Activity.MainActivity.onCheckedChanged(MainActivity.java:65)
at android.widget.RadioGroup.setCheckedId(RadioGroup.java:174)
at android.widget.RadioGroup.access$600(RadioGroup.java:54)
at android.widget.RadioGroup$CheckedStateTracker.onCheckedChanged(RadioGroup.java:358)
at android.widget.CompoundButton.setChecked(CompoundButton.java:139)
at android.widget.CompoundButton.toggle(CompoundButton.java:91)
at android.widget.RadioButton.toggle(RadioButton.java:76)
at android.widget.CompoundButton.performClick(CompoundButton.java:103)
at android.view.View$PerformClick.run(View.java:18803)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5493)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
at dalvik.system.NativeStart.main(Native Method)

代码的一部分:
public class MainActivity extends FragmentActivity implements RadioGroup.OnCheckedChangeListener{
private RadioGroup radioGroup;
private ArrayList basePagers;
private int position;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioGroup = (RadioGroup) findViewById(R.id.rg_selector);
radioGroup.check(R.id.rb_video);

    basePagers = new ArrayList<>();
    basePagers.add(new VideoPager(this));
    basePagers.add(new AudioPager(this));
    basePagers.add(new NetVideoPager(this));
    basePagers.add(new NetAudioPager(this));
    //设置监听
    radioGroup.setOnCheckedChangeListener(this);

    }



    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.rb_video:
                position = 0;
                break;
            case R.id.rb_audio:
                position=1;
                break;
            case R.id.rb_net_video:
                position=2;
                break;
            case R.id.rb_net_audio:
                position=3;
                break;
        }
        setFragment();
    }


private void setFragment() {
   FragmentManager manager = getSupportFragmentManager();
    FragmentTransaction ft = manager.beginTransaction();
    ft.replace(R.id.fl_content,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;

        }
    });
    ft.commit();
}

private BasePager getBasePager() {
    BasePager basePager = basePagers.get(position);
    if(basePager!=null && !basePager.isinitData) {
        basePager.isinitData = true;
        basePager.initData();
    }
    return basePager;
}

6个回答

czhilong9231
czhilong9231   2017.02.16 11:17
已采纳

把new Fragment那段代码拿出来,单独创建一个Fragment,然后使用时直接new出来就可以了。
原new Fragmen代码修改为:

ft.replace(R.id.fl_main_content,new ReplaceFragment(getBasePager()));

然后创建ReplaceFragment类,代码发如下:
public class ReplaceFragment extends Fragment {

private BasePager currPager;

public ReplaceFragment(BasePager pager) {
    this.currPager=pager;
}


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    return currPager.rootView;
}

}

qq_39182568
qq_39182568 哇 这个老哥 感谢感谢
6 个月之前 回复
qq_32812501
qq_32812501 回复NotMYida: 能给个代码吗,怎么修改成为静态类
大约一年之前 回复
qq_27360387
qq_27360387 我真想亲你一口
大约一年之前 回复
xu_xiang_123
xu_xiang_123 回复QingSheng33: 感谢
一年多之前 回复
qq_37569209
qq_37569209 回复QingSheng33: 可不可以把代码发一下给我看看 谢谢
一年多之前 回复
QingSheng33
QingSheng33 我把这个ReplaceFragment要设为public static,程序就可以正常运行了。感谢这位朋友
一年多之前 回复
u013147734
u013147734   2016.12.29 11:51

Fragment null must be a public static class就是说不能用内部类

stone_you
stone_you   Rxr 2016.12.28 13:01

把fragment的定义放到别的文件里,不要用匿名内部类的方式

u014411348
u014411348   2017.02.14 09:53

请问楼主决解了吗?我也遇到同样的问题

u011727826
u011727826   2018.01.14 20:27

class MyOnCheckedChangeListener implements RadioGroup.OnCheckedChangeListener {

    @Override
    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
        switch (checkedId){
            default:
                position = 0;
                break;
            case R.id.rb_audio: //音乐
                position = 1;
                break;
            case R.id.rb_net_video: //网络视频
                position = 2;
                break;
            case R.id.rb_net_audio: //网络音乐
                position = 3;
                break;
        }
        setFragment();
    }

    /**
     * 把页面添加到Fragment中
     *
     */
    private void setFragment() {

        //1.得到FragmentManger
        FragmentManager manager = getSupportFragmentManager();
        //2.开启事务
        FragmentTransaction ft = manager.beginTransaction();
        //3.替换
        ft.replace(R.id.fl_main_content, new MyFragment(getBasePager() );
        //4.提交事务
        ft.commit();
    }

    public class MyFragment extends Fragment {
        private BasePager currPager;
        public MyFragment(BasePager basePager) {
            this.currPager = Pager;
        }

        @Override
        public void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        }

        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            return super.onCreateView(inflater, container, savedInstanceState);
        }
    }
}
u011727826
u011727826   2018.01.14 20:28

class MyOnCheckedChangeListener implements RadioGroup.OnCheckedChangeListener {

    @Override
    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
        switch (checkedId){
            default:
                position = 0;
                break;
            case R.id.rb_audio: //音乐
                position = 1;
                break;
            case R.id.rb_net_video: //网络视频
                position = 2;
                break;
            case R.id.rb_net_audio: //网络音乐
                position = 3;
                break;
        }
        setFragment();
    }

    /**
     * 把页面添加到Fragment中
     *
     */
    private void setFragment() {

        //1.得到FragmentManger
        FragmentManager manager = getSupportFragmentManager();
        //2.开启事务
        FragmentTransaction ft = manager.beginTransaction();
        //3.替换
        ft.replace(R.id.fl_main_content, new MyFragment(getBasePager() );
        //4.提交事务
        ft.commit();
    }

    public class MyFragment extends Fragment {
        private BasePager currPager;
        public MyFragment(BasePager basePager) {
            this.currPager = Pager;
        }

        @Override
        public void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        }

        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            return super.onCreateView(inflater, container, savedInstanceState);
        }
    }
}我也遇到同样的问题,我这样写也报错啊啊
gkkunguo
gkkunguo 回复windyxue: 我解决了,就按照上面解决方法来
2 个月之前 回复
Dulan6622
Dulan6622 回复windyxue: 我也是,你最后弄好了吗
3 个月之前 回复
windyxue
windyxue 我的也是这样写的一直是闪退,楼主解决了吗?如何解决呢?急求
5 个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!