Android中的奇怪的IllegalArgumentException

时间:2014-04-17 20:27:09

标签: android

我正在尝试使用导航抽屉上的项目打开图像库,但我收到此错误:

04-17 13:21:19.067: E/FragmentManager(9210): No view found for id 0x7f0c005e (com.example.example:id/content_frame) for fragment ImageGallery {42721da8 #0 id=0x7f0c005e}
04-17 13:21:19.067: E/FragmentManager(9210): Activity state:
04-17 13:21:19.087: E/AndroidRuntime(9210): FATAL EXCEPTION: main
04-17 13:21:19.087: E/AndroidRuntime(9210): Process: com.example.example, PID: 9210
04-17 13:21:19.087: E/AndroidRuntime(9210): java.lang.IllegalArgumentException: No view found for id 0x7f0c005e (com.example.example:id/content_frame) for fragment ImageGallery{42721da8 #0 id=0x7f0c005e}
04-17 13:21:19.087: E/AndroidRuntime(9210):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:919)
04-17 13:21:19.087: E/AndroidRuntime(9210):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
04-17 13:21:19.087: E/AndroidRuntime(9210):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
04-17 13:21:19.087: E/AndroidRuntime(9210):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
04-17 13:21:19.087: E/AndroidRuntime(9210):     at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
04-17 13:21:19.087: E/AndroidRuntime(9210):     at android.os.Handler.handleCallback(Handler.java:733)
04-17 13:21:19.087: E/AndroidRuntime(9210):     at android.os.Handler.dispatchMessage(Handler.java:95)
04-17 13:21:19.087: E/AndroidRuntime(9210):     at android.os.Looper.loop(Looper.java:136)
04-17 13:21:19.087: E/AndroidRuntime(9210):     at android.app.ActivityThread.main(ActivityThread.java:5017)
04-17 13:21:19.087: E/AndroidRuntime(9210):     at java.lang.reflect.Method.invokeNative(Native Method)
04-17 13:21:19.087: E/AndroidRuntime(9210):     at java.lang.reflect.Method.invoke(Method.java:515)
04-17 13:21:19.087: E/AndroidRuntime(9210):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-17 13:21:19.087: E/AndroidRuntime(9210):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-17 13:21:19.087: E/AndroidRuntime(9210):     at dalvik.system.NativeStart.main(Native Method)

以下是我打开ImageGallery的地方:

//Handle what happens when each nav drawer item is pressed
    private void selectItem(int position) {
        Fragment newFragment = new FragmentHome();
        FragmentManager fm = getSupportFragmentManager();
        switch (position) {
            case 0:
                newFragment = new ImageGallery();
                break;
        }

        fm.beginTransaction().replace(R.id.content_frame, newFragment).commit();

        mLeftDrawer.setItemChecked(position, true);
        setTitle(mFragmentTitles[position]);
        mDrawerLayout.closeDrawer(mLeftDrawer);
}

如果我发布图片库代码,请告诉我。请注意它很长,所以这就是为什么它不在这里。

感谢您的帮助!

编辑:

以下是图库的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:padding="5dp">

    <Gallery
        android:id="@+id/Gallery01"
        android:layout_width="fill_parent"
        android:layout_height="90dp"/>

    <LinearLayout
        android:id="@+id/ImageView01"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>
</LinearLayout>

编辑2:

以下是包含content_frame的抽屉布局:

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

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

    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="250dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:background="#ffffff"
        android:dividerHeight="1dp" />


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

1 个答案:

答案 0 :(得分:1)

您需要查看onCreate()方法中setContentView()中指定的布局。

您尝试在以下内容中使用的布局:

fm.beginTransaction().replace(R.id.content_frame, newFragment).commit();

必须是该布局的孩子。如果没有onCreate()方法,很难诊断出你的问题,但这就是我要开始的地方。