Fragment null must be a public static class to be properly recreated from instance state.

Fragment null must be a public static class to be properly recreated from instance state.
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;
}

###解决办法
把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;
	}
}

你可能感兴趣的:(Android)