Java android.view.InflateException屏幕旋转

Java android.view.InflateException屏幕旋转,java,android,android-layout,android-fragments,Java,Android,Android Layout,Android Fragments,我一直在努力让片段为一个项目工作,所有的片段都很好,直到我在更改细节片段后旋转屏幕,当我在做出选择后旋转屏幕时,它崩溃到仪表板上,出现以下错误(如果有帮助,我启用了片段调试器) 包含两个片段的我的xml文件: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layo

我一直在努力让片段为一个项目工作,所有的片段都很好,直到我在更改细节片段后旋转屏幕,当我在做出选择后旋转屏幕时,它崩溃到仪表板上,出现以下错误(如果有帮助,我启用了片段调试器)

包含两个片段的我的xml文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="@color/app_background_color"
    tools:context=".MainActivity" >

    <ImageButton
        android:id="@+id/live_feed_button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:background="@drawable/button_style"
        android:contentDescription="@string/live_feed"
        android:minHeight="200dp" 
        android:scaleType="fitXY"
        android:src="@drawable/img2"/>

    <View android:id="@+id/strut"
        android:layout_width="0dp"
        android:layout_height="0dp" 
        android:layout_centerHorizontal="true"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weightSum="1.49" 
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/live_feed_button"
            android:layout_alignLeft="@id/strut"
            android:gravity="right">

        <fragment
            android:id="@+id/detail"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="fill_parent"

            class="ca.nait.myartego.Detail" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weightSum="3"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/live_feed_button" 
        android:layout_alignRight="@id/strut">

    <fragment
        android:id="@+id/list"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="fill_parent"

        class="ca.nait.myartego.HomeMenuItems" />
    </LinearLayout>
</RelativeLayout>
列表片段代码:

public class HomeMenuItems extends ListFragment
    {
        // items on menu
        String[] menuItems =
        { "Catalogue", "Events", "Find Us", "Contact Us", "Professional Login" };
        boolean case_0 = false, case_1 = false, case_2 = false, case_3 = false,
                case_4 = false;

        @SuppressLint("NewApi")
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
        }

        @Override
        public void onActivityCreated(Bundle savedInstanceState)
        {
            super.onActivityCreated(savedInstanceState);
            setListAdapter(new ArrayAdapter<String>(getActivity(),R.layout.menu_item, menuItems));
            FragmentManager.enableDebugLogging(true);
        }

        @Override
        public void onListItemClick(ListView list, View v, int position, long id)
        {
            System.out.println("test");
            Fragment newFragment = null;
            FragmentManager fm = getFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();

            switch (position)
            {
            case 0:
                if (!case_0)
                {
                    newFragment = new ChoosePDFActivity();
                    ft.setCustomAnimations(R.anim.push_left_in, R.anim.push_left_out);
                    ft.replace(R.id.detail, newFragment);
                    ft.disallowAddToBackStack().commit();

                    case_0 = true;
                    case_1 = false;
                    case_2 = false;
                    case_3 = false;
                    case_4 = false;
                }
                break;

            case 1:
                if (!case_1)
                {
                    case_1 = true;
                    newFragment = new EventsActivity();
                    ft.setCustomAnimations(R.anim.push_left_in, R.anim.push_left_out);
                    ft.replace(R.id.detail, newFragment);
                    ft.disallowAddToBackStack().commit();

                    case_0 = false;
                    case_2 = false;
                    case_3 = false;
                    case_4 = false;
                }
                break;

            case 2:
                if (!case_2)
                {
                    case_2 = true;
                    newFragment = new MapsActivity();
                    ft.setCustomAnimations(R.anim.push_left_in, R.anim.push_left_out);
                    ft.replace(R.id.detail, newFragment);
                    ft.disallowAddToBackStack().commit();

                    case_0 = false;
                    case_1 = false;
                    case_3 = false;
                    case_4 = false;
                }
                break;

            case 3:
                if (!case_3)
                {
                    case_3 = true;
                    newFragment = new ContactsActivity();
                    ft.setCustomAnimations(R.anim.push_left_in, R.anim.push_left_out);
                    ft.replace(R.id.detail, newFragment);
                    ft.disallowAddToBackStack().commit();

                    case_0 = false;
                    case_1 = false;
                    case_2 = false;
                    case_4 = false;
                }
                break;

            case 4:
                if (!case_4)
                {
                    case_4 = true;
                    newFragment = new LoginActivity();
                    ft.setCustomAnimations(R.anim.push_left_in, R.anim.push_left_out);
                    ft.replace(R.id.detail, newFragment);
                    ft.disallowAddToBackStack().commit();

                    case_0 = false;
                    case_1 = false;
                    case_2 = false;
                    case_3 = false;
                }
                break;
            }
        }

    }

看起来这里解决了相同的问题:


基本上,通过在屏幕上动态添加片段来解决这个问题,而无需在XML中显式声明它。

您可以发布详细片段的代码吗?似乎问题出在哪里..我添加了细节片段,希望能有所帮助
 public class HomeActivity extends FragmentActivity
 {
    @Override
     protected void onCreate(Bundle savedInstanceState)
     {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_home);

     }

    @Override
    public void onBackPressed()
    {
        super.onBackPressed();
        overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
    }
}
public class HomeMenuItems extends ListFragment
    {
        // items on menu
        String[] menuItems =
        { "Catalogue", "Events", "Find Us", "Contact Us", "Professional Login" };
        boolean case_0 = false, case_1 = false, case_2 = false, case_3 = false,
                case_4 = false;

        @SuppressLint("NewApi")
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
        }

        @Override
        public void onActivityCreated(Bundle savedInstanceState)
        {
            super.onActivityCreated(savedInstanceState);
            setListAdapter(new ArrayAdapter<String>(getActivity(),R.layout.menu_item, menuItems));
            FragmentManager.enableDebugLogging(true);
        }

        @Override
        public void onListItemClick(ListView list, View v, int position, long id)
        {
            System.out.println("test");
            Fragment newFragment = null;
            FragmentManager fm = getFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();

            switch (position)
            {
            case 0:
                if (!case_0)
                {
                    newFragment = new ChoosePDFActivity();
                    ft.setCustomAnimations(R.anim.push_left_in, R.anim.push_left_out);
                    ft.replace(R.id.detail, newFragment);
                    ft.disallowAddToBackStack().commit();

                    case_0 = true;
                    case_1 = false;
                    case_2 = false;
                    case_3 = false;
                    case_4 = false;
                }
                break;

            case 1:
                if (!case_1)
                {
                    case_1 = true;
                    newFragment = new EventsActivity();
                    ft.setCustomAnimations(R.anim.push_left_in, R.anim.push_left_out);
                    ft.replace(R.id.detail, newFragment);
                    ft.disallowAddToBackStack().commit();

                    case_0 = false;
                    case_2 = false;
                    case_3 = false;
                    case_4 = false;
                }
                break;

            case 2:
                if (!case_2)
                {
                    case_2 = true;
                    newFragment = new MapsActivity();
                    ft.setCustomAnimations(R.anim.push_left_in, R.anim.push_left_out);
                    ft.replace(R.id.detail, newFragment);
                    ft.disallowAddToBackStack().commit();

                    case_0 = false;
                    case_1 = false;
                    case_3 = false;
                    case_4 = false;
                }
                break;

            case 3:
                if (!case_3)
                {
                    case_3 = true;
                    newFragment = new ContactsActivity();
                    ft.setCustomAnimations(R.anim.push_left_in, R.anim.push_left_out);
                    ft.replace(R.id.detail, newFragment);
                    ft.disallowAddToBackStack().commit();

                    case_0 = false;
                    case_1 = false;
                    case_2 = false;
                    case_4 = false;
                }
                break;

            case 4:
                if (!case_4)
                {
                    case_4 = true;
                    newFragment = new LoginActivity();
                    ft.setCustomAnimations(R.anim.push_left_in, R.anim.push_left_out);
                    ft.replace(R.id.detail, newFragment);
                    ft.disallowAddToBackStack().commit();

                    case_0 = false;
                    case_1 = false;
                    case_2 = false;
                    case_3 = false;
                }
                break;
            }
        }

    }
public class Detail extends Fragment{
    View view;
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
    }

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

}