我正試圖在我嘗試創建的應用內實現導航抽屜。目前,每個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);
}
}
發佈您的MessageFragment。在這似乎是錯誤的。 –
請提及它失敗的行。 –
確保MessagesFragment()中的onCreateView()不返回null。 –