qq_35881717
我叫jc
采纳率100%
2016-12-28 03: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;
}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

7条回答

  • czhilong9231 czhilong9231 4年前

    把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;
    }
    

    }

    点赞 8 评论 复制链接分享
  • u013147734 渐行渐远是否还有一种坚持留在心间 5年前

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

    点赞 3 评论 复制链接分享
  • stone_you stone_you 5年前

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

    点赞 1 评论 复制链接分享
  • u014411348 兲涳 4年前

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

    点赞 评论 复制链接分享
  • xiaoxiaoDlw xiaoxiaoDlw 3年前

    图片说明 这个报错啊 怎么做啊?

    点赞 评论 复制链接分享
  • u011727826 u011727826 3年前

    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 3年前

    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);
            }
        }
    }
    
    点赞 评论 复制链接分享

相关推荐