Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Null pointer on inflated view when loading for the second time a google map fragment

I'm using a DrawerLayout and when I load for the second time a google map fragment the exception will be caught as expected; however, further in the same method when I try

Button btnShowList = (Button) ui.findViewById(R.id.btnShowList);

and I get a null pointer exception. I guess that's because ui was not assigned any value.

How can I deal with this?

Page2.java:

public class Page2 extends Fragment {

    View ui;
    final String TAG = "POPE";
    final int CODIGO_DE_RESULTADO_1 = 1; // Es para usarlo en onActivityResult();

    private GoogleMap mMap;

    public Page2(){
        // Este es el constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        if (ui != null) {
            ViewGroup parent = (ViewGroup) ui.getParent();
            if (parent != null){
                parent.removeView(ui);
            }
        }

        try {
            ui = inflater.inflate(R.layout.activity_page2,container,false);
        } catch (InflateException e) {
            Log.i(TAG,"HERE COMES THE STACK TRACE");
            e.printStackTrace();
        }

        FragmentManager fm = getFragmentManager();
        mMap = ((MapFragment) fm.findFragmentById(R.id.map)).getMap();
        mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);     
        mMap.setMyLocationEnabled(true);


        Button btnShowList = (Button) ui.findViewById(R.id.btnShowList);
        btnShowList.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                showFileslist();
            }
        });

        return ui;

    }
}

activity_page2.xml:

<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">

<fragment
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.MapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<Button
    android:id="@+id/btnShowList"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="Load points" />

Log:

07-08 22:17:44.203: I/POPE(11859): HERE COMES THE STACK TRACE
07-08 22:17:44.213: W/System.err(11859): android.view.InflateException: Binary XML file line #6: Error inflating class fragment
07-08 22:17:44.213: W/System.err(11859):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:710)
07-08 22:17:44.213: W/System.err(11859):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
07-08 22:17:44.213: W/System.err(11859):    at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
07-08 22:17:44.213: W/System.err(11859):    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
07-08 22:17:44.213: W/System.err(11859):    at com.jorgeandresserrano.gpstools.Page2.onCreateView(Page2.java:52)
07-08 22:17:44.213: W/System.err(11859):    at android.app.Fragment.performCreateView(Fragment.java:1695)
07-08 22:17:44.213: W/System.err(11859):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:903)
07-08 22:17:44.213: W/System.err(11859):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1075)
07-08 22:17:44.213: W/System.err(11859):    at android.app.BackStackRecord.run(BackStackRecord.java:682)
07-08 22:17:44.213: W/System.err(11859):    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1455)
07-08 22:17:44.223: W/System.err(11859):    at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441)
07-08 22:17:44.223: W/System.err(11859):    at android.os.Handler.handleCallback(Handler.java:725)
07-08 22:17:44.223: W/System.err(11859):    at android.os.Handler.dispatchMessage(Handler.java:92)
07-08 22:17:44.223: W/System.err(11859):    at android.os.Looper.loop(Looper.java:137)
07-08 22:17:44.223: W/System.err(11859):    at android.app.ActivityThread.main(ActivityThread.java:5293)
07-08 22:17:44.223: W/System.err(11859):    at java.lang.reflect.Method.invokeNative(Native Method)
07-08 22:17:44.223: W/System.err(11859):    at java.lang.reflect.Method.invoke(Method.java:511)
07-08 22:17:44.223: W/System.err(11859):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
07-08 22:17:44.223: W/System.err(11859):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
07-08 22:17:44.223: W/System.err(11859):    at dalvik.system.NativeStart.main(Native Method)
07-08 22:17:44.223: W/System.err(11859): Caused by: java.lang.IllegalArgumentException: Binary XML file line #6: Duplicate id 0x7f05003f, tag null, or parent id 0xffffffff with another fragment for com.google.android.gms.maps.MapFragment
07-08 22:17:44.223: W/System.err(11859):    at android.app.Activity.onCreateView(Activity.java:4863)
07-08 22:17:44.233: W/System.err(11859):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:686)
07-08 22:17:44.233: W/System.err(11859):    ... 19 more
07-08 22:17:44.233: D/AndroidRuntime(11859): Shutting down VM
07-08 22:17:44.233: W/dalvikvm(11859): threadid=1: thread exiting with uncaught exception (group=0x410feac8)
07-08 22:17:44.233: E/AndroidRuntime(11859): FATAL EXCEPTION: main
07-08 22:17:44.233: E/AndroidRuntime(11859): java.lang.NullPointerException
07-08 22:17:44.233: E/AndroidRuntime(11859):    at com.myname.myapp.Page2.onCreateView(Page2.java:66)
07-08 22:17:44.233: E/AndroidRuntime(11859):    at android.app.Fragment.performCreateView(Fragment.java:1695)
07-08 22:17:44.233: E/AndroidRuntime(11859):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:903)
07-08 22:17:44.233: E/AndroidRuntime(11859):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1075)
07-08 22:17:44.233: E/AndroidRuntime(11859):    at android.app.BackStackRecord.run(BackStackRecord.java:682)
07-08 22:17:44.233: E/AndroidRuntime(11859):    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1455)
07-08 22:17:44.233: E/AndroidRuntime(11859):    at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441)
07-08 22:17:44.233: E/AndroidRuntime(11859):    at android.os.Handler.handleCallback(Handler.java:725)
07-08 22:17:44.233: E/AndroidRuntime(11859):    at android.os.Handler.dispatchMessage(Handler.java:92)
07-08 22:17:44.233: E/AndroidRuntime(11859):    at android.os.Looper.loop(Looper.java:137)
07-08 22:17:44.233: E/AndroidRuntime(11859):    at android.app.ActivityThread.main(ActivityThread.java:5293)
07-08 22:17:44.233: E/AndroidRuntime(11859):    at java.lang.reflect.Method.invokeNative(Native Method)
07-08 22:17:44.233: E/AndroidRuntime(11859):    at java.lang.reflect.Method.invoke(Method.java:511)
07-08 22:17:44.233: E/AndroidRuntime(11859):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
07-08 22:17:44.233: E/AndroidRuntime(11859):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
07-08 22:17:44.233: E/AndroidRuntime(11859):    at dalvik.system.NativeStart.main(Native Method)

Line 66 is:

Button btnShowList = (Button) ui.findViewById(R.id.btnShowList);
like image 925
Jorge Avatar asked Jul 08 '13 18:07

Jorge


1 Answers

The problem can be resolved by removing the map fragment in the onDestroyView() method just before leaving. Like this:

@Override
public void onDestroyView() {
    super.onDestroyView();
    MapFragment f = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
    if (f != null){ 
        getFragmentManager().beginTransaction().remove(f).commit();
    }
}

This post explains this more in detail https://stackoverflow.com/a/14484640/2422833

like image 162
Jorge Avatar answered Oct 15 '22 11:10

Jorge