2013-11-27 62 views
1

我正試圖在我嘗試創建的應用內實現導航抽屜。目前,每個Activity的內容顯示在Fragment之內。由於我無法在佈局中使用靜態<fragment />,因此我選擇使用虛擬<FrameLayout />代替。這裏是我的佈局:將新片段添加到FrameLayout會產生IllegalStateException

<android.support.v4.widget.DrawerLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/drawerLayout_messages" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<!-- Main content view --> 
<FrameLayout 
    android:id="@+id/messages_content" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

<!-- Drawer view --> 
<ListView 
    android:id="@+id/messages_drawer_list" 
    android:layout_width="240dp" 
    android:layout_height="match_parent" 
    android:layout_gravity="start" 
    android:choiceMode="singleChoice" 
    android:divider="@android:color/transparent" 
    android:dividerHeight="0dp" 
    android:background="#F0F0F0"/> 

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

現在,試圖用一個片段來代替<FrameLayout />的時候,我想這樣的錯誤:

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

這是我爲了在添加執行代碼片段:

public class MessagesActivity extends Activity { 
    public static final String TAG = Constants.PACKAGE + ".MessagesActivity"; 

    private DrawerLayout drawerLayout; 
    private ActionBarDrawerToggle drawerToggle; 
    private ListView drawerItems; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     Log.d(TAG, "Starting activity: " + TAG); 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_messages); 

     drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout_messages); 
     String[] drawerItemsList = getResources().getStringArray(R.array.drawer_items); 
     drawerItems = (ListView) findViewById(R.id.messages_drawer_list); 
     drawerItems.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, drawerItemsList)); 

     getActionBar().setDisplayHomeAsUpEnabled(true); 
     getActionBar().setHomeButtonEnabled(true); 
     drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.drawable.ic_drawer, R.string.title_app, R.string.title_messages) { 

      public void onDrawerClosed(View view) { 
       getActionBar().setTitle(R.string.title_messages); 
       invalidateOptionsMenu(); 
      } 

      public void onDrawerOpened(View view) { 
       getActionBar().setTitle(R.string.title_app); 
       invalidateOptionsMenu(); 
      } 
     }; 
     drawerLayout.setDrawerListener(drawerToggle); 

     if (savedInstanceState == null) { 
      Log.d(TAG, "Setting fragment"); 
      setFragment(0); 
     } 

     Log.d(TAG, TAG + "'s layout is initialized. Now starting service connection"); 
    } 

    public void setFragment(int position) { 
     FragmentManager fragmentManager = getFragmentManager(); 
     FragmentTransaction transaction = fragmentManager.beginTransaction(); 

     transaction.replace(R.id.messages_content, new MessagesFragment()); 
     transaction.addToBackStack(null); 
     transaction.commit(); 

     drawerItems.setItemChecked(position, true); 
     drawerLayout.closeDrawer(drawerItems); 
    } 
} 

Activity文件基本上是直接從谷歌的開發者資源拉動,但我仍然收到錯誤消息。我忘了做點什麼嗎?

編輯:爲我們添加了我的MessagesFragment代碼。

public class MessagesFragment extends Fragment { 

    public MessagesFragment() {} 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     return inflater.inflate(R.layout.fragment_messages, container); 
    } 
} 
+0

發佈您的MessageFragment。在這似乎是錯誤的。 –

+0

請提及它失敗的行。 –

+0

確保MessagesFragment()中的onCreateView()不返回null。 –

回答

2
return inflater.inflate(R.layout.fragment_messages, null); 

能否請您嘗試使用容器的空代替。

+0

非常好,工作!我的後續問題是:爲什麼這會起作用? –

+0

膨脹方法說,我們傳遞的第二個參數(ViewGRoup root)應該成爲膨脹佈局的父/根,如果我們不傳遞任何東西(null,則該片段將成爲默認父項。)「root - \t可選視圖成爲生成的層次結構的父代。「 –