2015-04-24 39 views
2

當我轉動我的設備,然後旋轉回我得到一個異常說我的抽屜式導航片段沒有創建一個視圖片段並沒有創造方向變化後的視圖異常

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

會很好指出什麼是'NavigationDrawerFragment'和什麼'NavigationDrawerFragment.setUp()'。 – shkschneider

+1

顯示你的'onCreateView' – Xcihnegn

+0

@shkschneider添加了NavigationDrawerFragment的代碼 – tyczj

回答

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聲明的應用程序的抽屜另一個項目,我可以旋轉它只是罰款

相關問題