Mihail Yordanov
Mihail Yordanov

Reputation: 25

inflater.inflate results in NullPointerException when called for the second time

I have the following layout. Lets call it mapFragment.xml

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:id="@+id/fragmentId"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      class="com.google.android.gms.maps.SupportMapFragment"
      android:orientation="vertical">

   <com.google.android.gms.maps.MapView
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/mapIdfragmentId"
          android:layout_width="match_parent"
          android:layout_height="match_parent" />

</fragment>

It is used in a tab view. This is the first tab of 3. When loading the tab view this is the first tab that is loaded. No problems here. If I go to the second tab and then go back - again I have no problems. But if I go to the third tab and try to go back (no matter if the second or first tab) Then I have a NPE in the first tab's onCreate method

   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
     super.onCreateView(inflater, container, savedInstanceState);
     View v = inflater.inflate(R.layout.mapFragment, container, false);
     return v;
   }

The NPE happens at the second line - View v = .... The root cause of the exception is

 Caused by: java.lang.IllegalArgumentException: Binary XML file line #7: Duplicate id 0x7f040005, tag null, or parent id 0xffffffff with another fragment for com.google.android.gms.maps.MapFragment
 E/AndroidRuntime(17423):    at android.app.Activity.onCreateView(Activity.java:4722)

I'm using the Sherlock library and the device is running at Android v2.2.2

PS: the suggested solution here - Duplicate ID, tag null, or parent id with another fragment for com.google.android.gms.maps.MapFragment fixes the problem but after that the map is totally frozen :(

Upvotes: 0

Views: 275

Answers (2)

Biraj Zalavadia
Biraj Zalavadia

Reputation: 28484

You are on wrong track

remove this from xml

 <com.google.android.gms.maps.MapView
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/mapIdfragmentId"
          android:layout_width="match_parent"
          android:layout_height="match_parent" />

Because this Fragment is containing map itself Thats why it throwing duplication error

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:id="@+id/fragmentId"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      class="com.google.android.gms.maps.SupportMapFragment"
      android:orientation="vertical">
</fragment>

Upvotes: 0

Raghunandan
Raghunandan

Reputation: 133560

Change to

 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
 View v = inflater.inflate(R.layout.mapFragment, container, false);
 return v;
}

Your are not returning the view inflated.

Also if you are using map api v1 its deprecated. Switch to api v2

https://developers.google.com/maps/documentation/android/

Upvotes: 1

Related Questions