标签:
Fragment是构成灵活UI的重要部分。最近学习到Fragment的使用时候,碰到一个问题。主程序程序布局非常简单:
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.SlidingPaneLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/slidingPanelLayout" android:layout_width="match_parent" android:layout_height="match_parent"> <fragment android:id="@+id/bookmarkFragment" android:name="com.hnb.zzk.slidingpanellayouttest.BookMarkerFragment" android:layout_width="100dp" android:layout_height="match_parent" android:layout_gravity="left"/> <fragment android:id="@+id/browserFragment" android:name="com.hnb.zzk.slidingpanellayouttest.BrowserFragment" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="right" android:layout_weight="1"/> </android.support.v4.widget.SlidingPaneLayout>
就是两个Fragment构成主要布局,然后在Java代码中获取Fragment:
SlidingPaneLayout spl = (SlidingPaneLayout) this.findViewById(R.id.slidingPanelLayout); spl.setPanelSlideListener(new SlidingPaneLayout.PanelSlideListener() { @Override public void onPanelSlide(View panel, float slideOffset) { } @Override public void onPanelOpened(View panel) { MainActivity.this.getFragmentManager().findFragmentById(R.id.bookmarkFragment).setHasOptionsMenu(true); } @Override public void onPanelClosed(View panel) { MainActivity.this.getFragmentManager().findFragmentById(R.id.bookmarkFragment).setHasOptionsMenu(false); } });
初步看起来似乎一切顺利,代码也是精简的一塌糊涂。于是乎运行起来看看,在模拟器中却直接奔溃,赶紧加上异常捕捉代码,得到如下结果:
java.lang.NullPointerException at com.hnb.zzk.slidingpanellayouttest.MainActivity$2.onPanelOpened(MainActivity.java:44) at android.support.v4.widget.SlidingPaneLayout.dispatchOnPanelOpened(SlidingPaneLayout.java:343) at android.support.v4.widget.SlidingPaneLayout$DragHelperCallback.onViewDragStateChanged(SlidingPaneLayout.java:1318) at android.support.v4.widget.ViewDragHelper.setDragState(ViewDragHelper.java:874) at android.support.v4.widget.ViewDragHelper.forceSettleCapturedViewAt(ViewDragHelper.java:592) at android.support.v4.widget.ViewDragHelper.settleCapturedViewAt(ViewDragHelper.java:569) at android.support.v4.widget.SlidingPaneLayout$DragHelperCallback.onViewReleased(SlidingPaneLayout.java:1354) at android.support.v4.widget.ViewDragHelper.dispatchViewReleased(ViewDragHelper.java:767) at android.support.v4.widget.ViewDragHelper.releaseViewForPointerUp(ViewDragHelper.java:1395) at android.support.v4.widget.ViewDragHelper.processTouchEvent(ViewDragHelper.java:1213) at android.support.v4.widget.SlidingPaneLayout.onTouchEvent(SlidingPaneLayout.java:826)
在onPanelOpend()方法里面出现了空指针异常。可能出现空指针异常的就是getFragmentManager()和findFragmentById()两个调用。findFragmentById()应该不太可能出现空指针异常,布局文件中两个fragment明摆着。那么只能说是getFragmentManager()这个调用出现了指针。在Fragment的开发文档中明确指出,Fragment的管理都是通过FragmentManager来实现的,那么问题出现在哪呢?在网上搜了一番终于发现了症结所在。我们引用的是v4兼容包中的Fragment组件,而getFragmentManager()是针对v11+的Fragment的,因此将getFragmentManager()替换成getSupportFragmentManager()就好了。
标签:
原文地址:http://www.cnblogs.com/csuftzzk/p/android_fragment_manager_null_exception.html