片段在方向更改后未创建视图异常

时间:2015-04-24 15:53:45

标签: android android-fragments

当我旋转设备然后将其旋转回来时我得到一个例外,说我的导航抽屉碎片没有创建视图

Caused by: java.lang.IllegalStateException: Fragment xxx.xxx.Fragments.NavigationDrawerFragment did not create a view.
            at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2200)
            at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:300)
            at android.support.v7.app.AppCompatDelegateImplV7.callActivityOnCreateView(AppCompatDelegateImplV7.java:816)
            at android.support.v7.app.AppCompatDelegateImplV11.callActivityOnCreateView(AppCompatDelegateImplV11.java:72)
            at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:804)
            at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:684)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
            at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249)
            at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)
            at xxx.xxxxx.MainActivity.onCreate(MainActivity.java:198)
            at android.app.Activity.performCreate(Activity.java:5248)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
            at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3805)
            at android.app.ActivityThread.access$900(ActivityThread.java:139)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1216)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5086)
            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:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)

这是我的活动布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v4.widget.DrawerLayout

        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <android.support.v7.widget.Toolbar android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:id="@+id/toolbar"
                android:minHeight="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

            </android.support.v7.widget.Toolbar>

            <View android:layout_width="match_parent"
                android:layout_height="3dp"
                android:background="@drawable/toolbar_shadow"/>

            <FrameLayout
                android:id="@+id/container"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

        </LinearLayout>

        <fragment android:id="@+id/navigation_drawer"
            android:layout_width="@dimen/navigation_drawer_width"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:name="xxx.xxxx.Fragments.NavigationDrawerFragment" />

        </android.support.v4.widget.DrawerLayout>

</RelativeLayout>

这是我的一些MainActivity代码

@Override
public void onCreate(Bundle state){
    super.onCreate(state);
    setContentView(R.layout.main);

    mToolbar = (Toolbar)findViewById(R.id.toolbar);
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ListFragment lf = new ListFragment();
    ft.replace(R.id.container, lf).commit();

    mNavigationDrawerFragment = (NavigationDrawerFragment) getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);

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

我可以旋转一次就好了但是当我再次旋转时它就会崩溃。我看了这个问题的其他问题,但似乎没有一个问题跟着我的问题

编辑:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_navigation_drawer, container, false);

        mHeader = inflater.inflate(R.layout.header_view,null);
        mDrawerListView = (ListView)v.findViewById(R.id.list);
        mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(position > 0){
                    selectItem(position);
                }else{
                    if (mDrawerListView != null) {
                        mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
                    }
                }
            }
        });
        mDrawerListView.addHeaderView(mHeader);
        mDrawerListView.setAdapter(new NavDrawerAdapter(
                getActivity(),
                R.layout.drawer_row_layout,
                R.id.text1,
                getResources().getStringArray(R.array.menu_list)));
        mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
        return v;
    }

    public void setUp(int fragmentId, DrawerLayout drawerLayout,Toolbar toolbar) {
        mFragmentContainerView = getActivity().findViewById(fragmentId);
        mDrawerLayout = drawerLayout;

        mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

        mDrawerToggle = new ActionBarDrawerToggle(
                getActivity(),                    /* host Activity */
                mDrawerLayout,                    /* DrawerLayout object */
                toolbar,
                R.string.navigation_drawer_open,  /* "open drawer" description for accessibility */
                R.string.navigation_drawer_close  /* "close drawer" description for accessibility */
        ) {
            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
                if (!isAdded()) {
                    return;
                }

                //getActivity().invalidateOptionsMenu(); // calls onPrepareOptionsMenu()
            }

            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                if (!isAdded()) {
                    return;
                }

                if (!mUserLearnedDrawer) {
                    mUserLearnedDrawer = true;
                    SharedPreferences sp = PreferenceManager
                            .getDefaultSharedPreferences(getActivity());
                    sp.edit().putBoolean(PREF_USER_LEARNED_DRAWER, true).apply();
                }

                //getActivity().invalidateOptionsMenu(); // calls onPrepareOptionsMenu()
            }

            @Override
            public void onDrawerSlide(View drawerView, float slideOffset){
                super.onDrawerSlide(drawerView, slideOffset);
            }
        };

        if (!mUserLearnedDrawer && !mFromSavedInstanceState) {
            mDrawerLayout.openDrawer(mFragmentContainerView);
        }

        // Defer code dependent on restoration of previous instance state.
        mDrawerLayout.post(new Runnable() {
            @Override
            public void run() {
                mDrawerToggle.syncState();
            }
        });

        mDrawerLayout.setDrawerListener(mDrawerToggle);
    }

1 个答案:

答案 0 :(得分:0)

我不确定为什么这会解决这里的问题,但我所做的不是在xml中创建片段而是我像这样动态创建它

if(!(getSupportFragmentManager().findFragmentById(R.id.navigation_drawer) instanceof NavigationDrawerFragment)){
    mNavigationDrawerFragment = new NavigationDrawerFragment();
    ft.replace(R.id.navigation_drawer,mNavigationDrawerFragment).commit();
}else{
    ft.commit();
}

奇怪的是我有另一个项目,它在xml中声明了app抽屉,我可以将它旋转得很好