原来为:
<fragment
android:id="@+id/navigation_drawer_old"
android:name="com.android.yunshi.fragment.NavigationLeftFragment"
android:layout_width="@dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
android:visibility="gone"
tools:layout="@layout/fragment_navigation_drawer" />
mNavigationDrawerFragment = (NavigationLeftFragment) getSupportFragmentManager()
// .findFragmentById(R.id.navigation_drawer);
mNavigationDrawerFragment.setUp(R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.yunshi_root));
这几天,收集到的回馈中,有大量错误这样就会出现错误:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.yunshi/com.android.yunshi.MainActivity}: android.view.InflateException: Binary XML file line #25: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2099)
at android.app.ActivityThread.access$600(ActivityThread.java:134)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1207)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4796)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:776)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:543)
at dalvik.system.NativeStart.main(Native Method)
原因就是,v4.fragment不支持<fragment>静态标签,部分型号的手机,会导致程序崩溃
解决方案:
mNavigationDrawerFragment=new NavigationLeftFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.navigation_drawer, mNavigationDrawerFragment);
fragmentTransaction.commit();
fragmentManager.executePendingTransactions();
// Set up the drawer. 找到抽屉控件
mNavigationDrawerFragment.setUp(R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.yunshi_root));
注意,一定要fragmentManager.executePendingTransactions(); 立即提交事务
不然commit()总是在activity的UI完成后,才提交,这样你在Fragment中的getActivity()就会报错,因为是空