屏幕旋转时出现android.view.InflateException

时间:2014-02-28 03:25:31

标签: java android android-layout android-fragments

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

02-27 20:08:01.194: V/FragmentManager(32480): movefrom RESUMED: Detail{42311948 #0   id=0x7f0a0007}
02-27 20:08:01.194: V/FragmentManager(32480): movefrom RESUMED: HomeMenuItems{42314338 #1 id=0x7f0a0008}
02-27 20:08:01.194: V/FragmentManager(32480): movefrom RESUMED: ChoosePDFActivity{4234be08 #2 id=0x7f0a0007}
02-27 20:08:01.194: V/FragmentManager(32480): Saved state of Detail{42311948 #0 id=0x7f0a0007}: null
02-27 20:08:01.194: V/FragmentManager(32480): Saved state of HomeMenuItems{42314338 #1   id=0x7f0a0008}: Bundle[{android:view_state={16711681=android.view.AbsSavedState$1@416dbfb0,       16711682=android.view.AbsSavedState$1@416dbfb0,   16711683=android.view.AbsSavedState$1@416dbfb0, 16908298=AbsListView.SavedState{42359778       selectedId=-9223372036854775808 firstId=-1 viewTop=0 position=0 height=1424 filter=null   checkState=null}}}]
02-27 20:08:01.204: V/FragmentManager(32480): Saved state of ChoosePDFActivity{4234be08 #2 id=0x7f0a0007}: Bundle[{android:view_state={16908298=AbsListView.SavedState{4235cab8 selectedId=-9223372036854775808 firstId=-1 viewTop=0 position=0 height=514 filter=null checkState=null}, 2131361801=android.view.AbsSavedState$1@416dbfb0}}]
02-27 20:08:01.214: V/FragmentManager(32480): saveAllState: adding fragment #0: Detail{42311948 #0 id=0x7f0a0007}
02-27 20:08:01.214: V/FragmentManager(32480): saveAllState: adding fragment #1: HomeMenuItems{42314338 #1 id=0x7f0a0008}
02-27 20:08:01.214: V/FragmentManager(32480): saveAllState: adding fragment #2: ChoosePDFActivity{4234be08 #2 id=0x7f0a0007}
02-27 20:08:01.214: V/FragmentManager(32480): movefrom STARTED: Detail{42311948 #0 id=0x7f0a0007}
02-27 20:08:01.214: V/FragmentManager(32480): movefrom STARTED: HomeMenuItems{42314338 #1 id=0x7f0a0008}
02-27 20:08:01.214: V/FragmentManager(32480): movefrom STARTED: ChoosePDFActivity{4234be08 #2 id=0x7f0a0007}
02-27 20:08:01.214: V/FragmentManager(32480): movefrom STOPPED: Detail{42311948 #0 id=0x7f0a0007}
02-27 20:08:01.214: V/FragmentManager(32480): movefrom STOPPED: HomeMenuItems{42314338 #1 id=0x7f0a0008}
02-27 20:08:01.214: V/FragmentManager(32480): movefrom STOPPED: ChoosePDFActivity{4234be08 #2 id=0x7f0a0007}
02-27 20:08:01.214: V/FragmentManager(32480): movefrom ACTIVITY_CREATED: Detail{42311948 #0 id=0x7f0a0007}
02-27 20:08:01.214: V/FragmentManager(32480): movefrom CREATED: Detail{42311948 #0 id=0x7f0a0007}
02-27 20:08:01.214: V/FragmentManager(32480): Freeing fragment index Detail{42311948 #0 id=0x7f0a0007}
02-27 20:08:01.214: V/FragmentManager(32480): movefrom ACTIVITY_CREATED: HomeMenuItems{42314338 #1 id=0x7f0a0008}
02-27 20:08:01.214: V/FragmentManager(32480): movefrom CREATED: HomeMenuItems{42314338 #1 id=0x7f0a0008}
02-27 20:08:01.214: V/FragmentManager(32480): Freeing fragment index HomeMenuItems{42314338 #1 id=0x7f0a0008}
02-27 20:08:01.214: V/FragmentManager(32480): movefrom ACTIVITY_CREATED: ChoosePDFActivity{4234be08 #2 id=0x7f0a0007}
02-27 20:08:01.224: V/FragmentManager(32480): movefrom CREATED: ChoosePDFActivity{4234be08 #2 id=0x7f0a0007}
02-27 20:08:01.224: V/FragmentManager(32480): Freeing fragment index ChoosePDFActivity{4234be08 #2 id=0x7f0a0007}
02-27 20:08:01.244: V/FragmentManager(32480): Instantiated fragment Detail{42364658 #0 id=0x7f0a0007}
02-27 20:08:01.244: V/FragmentManager(32480): restoreAllState: active #0: Detail{42364658 #0 id=0x7f0a0007}
02-27 20:08:01.244: V/FragmentManager(32480): Instantiated fragment HomeMenuItems{42365038 #1 id=0x7f0a0008}
02-27 20:08:01.244: V/FragmentManager(32480): restoreAllState: active #1: HomeMenuItems{42365038 #1 id=0x7f0a0008}
02-27 20:08:01.254: V/FragmentManager(32480): Instantiated fragment ChoosePDFActivity{42365a28 #2 id=0x7f0a0007}
02-27 20:08:01.254: V/FragmentManager(32480): restoreAllState: active #2: ChoosePDFActivity{42365a28 #2 id=0x7f0a0007}
02-27 20:08:01.254: V/FragmentManager(32480): restoreAllState: added #0: Detail{42364658 #0 id=0x7f0a0007}
02-27 20:08:01.254: V/FragmentManager(32480): restoreAllState: added #1: HomeMenuItems{42365038 #1 id=0x7f0a0008}
02-27 20:08:01.254: V/FragmentManager(32480): restoreAllState: added #2: ChoosePDFActivity{42365a28 #2 id=0x7f0a0007}
02-27 20:08:01.254: V/FragmentManager(32480): moveto CREATED: ChoosePDFActivity{42365a28 #2 id=0x7f0a0007}
02-27 20:08:01.264: V/FragmentActivity(32480): onCreateView: id=0x7f0a0007 fname=ca.nait.myartego.Detail existing=ChoosePDFActivity{42365a28 #2 id=0x7f0a0007}
02-27 20:08:01.264: D/AndroidRuntime(32480): Shutting down VM
02-27 20:08:01.264: W/dalvikvm(32480): threadid=1: thread exiting with uncaught exception (group=0x41616ba8)
02-27 20:08:01.264: E/AndroidRuntime(32480): FATAL EXCEPTION: main
02-27 20:08:01.264: E/AndroidRuntime(32480): Process: com.artifex.mupdf, PID: 32480
02-27 20:08:01.264: E/AndroidRuntime(32480): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.artifex.mupdf/ca.nait.myartego.HomeActivity}: android.view.InflateException: Binary XML file line #34: Error inflating class fragment
02-27 20:08:01.264: E/AndroidRuntime(32480):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
02-27 20:08:01.264: E/AndroidRuntime(32480):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
02-27 20:08:01.264: E/AndroidRuntime(32480):    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3738)
02-27 20:08:01.264: E/AndroidRuntime(32480):    at android.app.ActivityThread.access$900(ActivityThread.java:135)
02-27 20:08:01.264: E/AndroidRuntime(32480):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1202)
02-27 20:08:01.264: E/AndroidRuntime(32480):    at android.os.Handler.dispatchMessage(Handler.java:102)
02-27 20:08:01.264: E/AndroidRuntime(32480):    at android.os.Looper.loop(Looper.java:136)
02-27 20:08:01.264: E/AndroidRuntime(32480):    at android.app.ActivityThread.main(ActivityThread.java:5017)
02-27 20:08:01.264: E/AndroidRuntime(32480):    at java.lang.reflect.Method.invokeNative(Native Method)
02-27 20:08:01.264: E/AndroidRuntime(32480):    at java.lang.reflect.Method.invoke(Method.java:515)
02-27 20:08:01.264: E/AndroidRuntime(32480):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-27 20:08:01.264: E/AndroidRuntime(32480):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-27 20:08:01.264: E/AndroidRuntime(32480):    at dalvik.system.NativeStart.main(Native Method)
02-27 20:08:01.264: E/AndroidRuntime(32480): Caused by: android.view.InflateException: Binary XML file line #34: Error inflating class fragment
02-27 20:08:01.264: E/AndroidRuntime(32480):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
02-27 20:08:01.264: E/AndroidRuntime(32480):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
02-27 20:08:01.264: E/AndroidRuntime(32480):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
02-27 20:08:01.264: E/AndroidRuntime(32480):    at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
02-27 20:08:01.264: E/AndroidRuntime(32480):    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
02-27 20:08:01.264: E/AndroidRuntime(32480):    at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
02-27 20:08:01.264: E/AndroidRuntime(32480):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
02-27 20:08:01.264: E/AndroidRuntime(32480):    at android.app.Activity.setContentView(Activity.java:1929)
02-27 20:08:01.264: E/AndroidRuntime(32480):    at ca.nait.myartego.HomeActivity.onCreate(HomeActivity.java:16)
02-27 20:08:01.264: E/AndroidRuntime(32480):    at android.app.Activity.performCreate(Activity.java:5231)
02-27 20:08:01.264: E/AndroidRuntime(32480):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
02-27 20:08:01.264: E/AndroidRuntime(32480):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
02-27 20:08:01.264: E/AndroidRuntime(32480):    ... 12 more
02-27 20:08:01.264: E/AndroidRuntime(32480): Caused by: java.lang.IllegalStateException: Fragment ca.nait.myartego.Detail did not create a view.
02-27 20:08:01.264: E/AndroidRuntime(32480):    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:314)
02-27 20:08:01.264: E/AndroidRuntime(32480):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
02-27 20:08:01.264: E/AndroidRuntime(32480):    ... 23 more

我的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 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);
    }
}

List片段代码:

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);
    }

}

1 个答案:

答案 0 :(得分:0)

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

Fragments and rotating screen problems

基本上,通过动态地将Fragment添加到屏幕来解决问题,而不是在XML中明确声明它。