2015-09-27 68 views
0

我有滑塊菜單displayview方法爲:的Android片段從導航抽屜切換導致應用程序退出

private void displayView(int position) { 
       // update the main content by replacing fragments 
       Fragment fragment = null; 
       switch (position) { 
        case 0: 
         fragment = new HomeFragment(); 

        FragmentManager fm = getSupportFragmentManager(); 
        String tag = fragment.getTag(); // instance method of a to get a tag 

        FragmentTransaction ft = fm.beginTransaction(); 

        ft.replace(R.id.frame_container, fragment, tag); 
        ft.addToBackStack(tag); 
        ft.commit(); 

        default: 
         break; 
       } 

       if (fragment != null) { 
        FragmentManager fragmentManager = getFragmentManager(); 
        fragmentManager.beginTransaction() 
          .replace(R.id.frame_container, fragment).commit(); 

        // update selected item and title, then close the drawer 
        drawerListView.setItemChecked(position, true); 
        drawerListView.setSelection(position); 
        setTitle(navMenuTitles[position]); 
        drawer.closeDrawer(drawerListView); 
       } else { 
        // error in creating fragment 
        Log.e("MainActivity", "Error in creating fragment"); 
       } 
      } 

我有一個名爲home_fragment其代碼後面是HomeFragment.java

當我點擊該片段片段首先它工作正常,但點擊索引0旁邊的菜單時,出現如下錯誤:

Attempt to write to field 'int android.support.v4.app.Fragment.mNextAnim' on a null object reference 

我在做什麼錯?謝謝。

這是我遇到的錯誤:

case OP_REMOVE: { 
        Fragment f = op.fragment; 
        f.mNextAnim = exitAnim; 
        mManager.removeFragment(f, transition, transitionStyle); 
       } break; 

此代碼存在於BackStackRecord.java

+0

你在哪裏使用mNextAnim? – freddieptf

回答

1

你不應該叫container.removeAllViewsInLayout()

當您撥打FragmentTransaction.replace()時,FragmentManager將處理容器上的所有視圖操作,包括刪除當前片段視圖。您可能會收到錯誤消息,因爲FragmentManager預計片段視圖位於容器中,但您已將其刪除。

所有你需要的是這樣的:

 private void displayView(int position) { 
      // update the main content by replacing fragments 
      Fragment fragment = null; 
      switch (position) { 
       case 0: 
        fragment = new HomeFragment(); 
        break; 
       case 1: 
        // set fragment to other nav target 
        fragment = ... 
        break; 
       . 
       . 
       . 
       default: 
        break; 
      } 

      if (fragment != null) { 
       FragmentManager fragmentManager = getSupportFragmentManager(); 
       fragmentManager.beginTransaction() 
         .replace(R.id.frame_container, fragment).commit(); 

       // update selected item and title, then close the drawer 
       drawerListView.setItemChecked(position, true); 
       drawerListView.setSelection(position); 
       setTitle(navMenuTitles[position]); 
       drawer.closeDrawer(drawerListView); 
      } else { 
       // error in creating fragment 
       Log.e("MainActivity", "Error in creating fragment"); 
      } 
     } 

而且我注意到你正在使用getFragmentManager()。請檢查您的課程進口並確保您有正確的電話。

  • 如果HomeFragment延伸android.app.Fragment,請使用getFragmentManager()
  • 如果HomeFragment延伸android.support.v4.app.Fragment,請使用getSupportFragmentManager()
+0

現在我用這個:'fragment = new HomeFragment(); FragmentManager fm = getSupportFragmentManager(); String tag = fragment.getTag(); //獲取標籤的實例方法 FragmentTransaction ft = fm.beginTransaction(); ft.replace(R.id.frame_container,fragment,tag); ft.addToBackStack(tag); ftp.mit();'同樣的錯誤。 –

+0

你也不需要那個。對'replace()'的一次調用將處理刪除當前片段並添加新片段,請參閱我的更新答案。 –

+0

謝謝。我會試試這個,向它解釋邏輯給我,一旦我通過就會接受。 –