Problems with Fragmentmanager in Android

Source: Internet
Author: User
Tags spl

Fragment is an important part of a flexible UI. Recently learned the use of fragment, encountered a problem. The main program layout is very simple:

<?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>

Is that two fragment form the main layout, and then get the fragment in the Java code:

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

Initially it seemed like everything went well and the code was a mess of streamlining. So run up to see, in the simulator but directly collapse, and quickly add the exception capture code, get the following results:

Java.lang.NullPointerExceptionat 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) 

A null pointer exception occurred in the Onpanelopend () method. Possible null pointer exceptions are Getfragmentmanager () and Findfragmentbyid () two calls. Findfragmentbyid () should be less likely to have a null pointer exception, and two fragment in the layout file are obvious. Then it can only be said that Getfragmentmanager () This call appears with a pointer. In fragment's development documentation, it is clear that fragment management is implemented through Fragmentmanager, so where does the problem arise? Search on the internet finally found the crux of the problem. We are referencing the fragment component in the V4 Compatibility Pack, and Getfragmentmanager () is fragment for v11+, so replace Getfragmentmanager () with Getsupportfragmentmanager () just fine.

Problems with Fragmentmanager in Android

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.