Android Fragment Rotation导致崩溃

时间:2014-01-31 00:35:43

标签: java android android-fragments

我有一个非常有气势的Android应用程序,我正在努力。

我有一个导航抽屉,根据用户选择的内容交换碎片。

但是,当屏幕旋转时,应用程序会因此错误而崩溃:

java.lang.RuntimeException: Unable to destroy activity {MainActivity}: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState

我的片段都扩展了基类'BaseFragment':

    public class BaseFragment  extends Fragment{
    protected FragmentManager fragmentManager;

    public void setFragmentManager(FragmentManager fragmentManager){
        this.fragmentManager=fragmentManager;
    }
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Fragment f = (Fragment) fragmentManager
                .findFragmentById(R.id.container);
        if (f != null)
            fragmentManager.beginTransaction().remove(f).commit();
    }

}

我的默认片段'NewsFeedFragment'如下所示:

    public class NewsFeedFragment extends BaseFragment{

    /**
     * Returns a new instance of this fragment for the given section
     * number.
     */
    public static NewsFeedFragment newInstance(int sectionNumber) {
        NewsFeedFragment fragment = new NewsFeedFragment();
        return fragment;
    }

    public NewsFeedFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        setFragmentManager(getFragmentManager());
        View rootView = inflater.inflate(R.layout.fragment_news_feed, container, false);;
        return rootView;
    }
}

我的主要活动如下:

    public class MainActivity extends BaseActivity {

    /**
     * Fragment managing the behaviors, interactions and presentation of the navigation drawer.
     */
    private NavigationDrawerFragment mNavigationDrawerFragment;

    /**
     * Used to store the last screen title. For use in {@link #restoreActionBar()}.
     */
    private CharSequence mTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.titlebar);
        ActionBar actionBar=getActionBar();

        mNavigationDrawerFragment = (NavigationDrawerFragment)
                getFragmentManager().findFragmentById(R.id.navigation_drawer);
        mTitle = getTitle();

        // Set up the drawer.
        mNavigationDrawerFragment.setUp(
                R.id.navigation_drawer,
                (DrawerLayout) findViewById(R.id.drawer_layout));

    }



    public void onSectionAttached(int number) {
        switch (number) {
            case 1:
                mTitle = getString(R.string.news_feed_activity);
                break;
            case 2:
                mTitle = getString(R.string.friends_activity);
                break;
            case 3:
                mTitle = getString(R.string.events_activity);
                break;
            case 4:
                mTitle = getString(R.string.locations_activity);
                break;
            case 5:
                mTitle = getString(R.string.settings_activity);
                break;
        }
    }

    public void restoreActionBar() {
        ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
        actionBar.setDisplayShowTitleEnabled(true);
        actionBar.setTitle("");
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        if (!mNavigationDrawerFragment.isDrawerOpen()) {
            // Only show items in the action bar relevant to this screen
            // if the drawer is not showing. Otherwise, let the drawer
            // decide what to show in the action bar.
            getMenuInflater().inflate(R.menu.news_feed, menu);
            restoreActionBar();
            return true;
        }
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        //if (id == R.id.action_settings) {
        //    return true;
        //}
        return super.onOptionsItemSelected(item);
    }


}

正如我所说,这个错误仅在旋转时发生。

这是完整的错误输出流:

 EXCEPTION: main
    Process: APP ID, PID: 8399
    java.lang.RuntimeException: Unable to destroy activity {MainActivity}: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
            at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3497)
            at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3515)
            at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3715)
            at android.app.ActivityThread.access$900(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1202)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
            at android.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1323)
            at android.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1341)
            at android.app.BackStackRecord.commitInternal(BackStackRecord.java:597)
            at android.app.BackStackRecord.commit(BackStackRecord.java:575)
            at uk.co.stephen_robinson.uni.lufelf.NewsFeedFragment.onDestroyView(NewsFeedFragment.java:43)
            at android.app.Fragment.performDestroyView(Fragment.java:1898)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:954)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1044)
            at android.app.FragmentManagerImpl.dispatchDestroy(FragmentManager.java:1881)
            at android.app.Activity.performDestroy(Activity.java:5402)
            at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1117)
            at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3484)
            at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3515)
            at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3715)
            at android.app.ActivityThread.access$900(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1202)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)

我真的很感激你的帮助。

提前致谢,

詹姆斯。

1 个答案:

答案 0 :(得分:1)

你得到错误,因为你不能这样做......

 @Override
    public void onDestroyView() {
        super.onDestroyView();
        Fragment f = (Fragment) fragmentManager
                .findFragmentById(R.id.container);
        if (f != null)
            fragmentManager.beginTransaction().remove(f).commit();
    }

...在onDestroyView()函数中。相反,您应该删除活动类的onPause()方法中的片段。同样,您应该在活动的onResume()方法中添加片段。