1

I am trying to use the android map to locate the specified address. I am able to locate and mark the address on map for the first time successfully. But when I try to locate the another address it throws an Exception in the fragment :

Binary XML file line #1: Error inflating class fragment

axml layout:

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

The problem is that when the fragment is replaced for the second time to locate new address, android is unable to inflate from axml. It crashes on line:

view = inflater.Inflate(Resource.Layout.map_layout, null);

enter image description here

I gone through these links, but could find the solution:

Android map v2 error on second inflating

Android - SupportMapFragment error when openning for second time

https://forums.xamarin.com/discussion/7378/google-maps-wont-work-error-on-fragmenttransaction

I also tried using SupportMapFragment, behaviour is same.

Community
  • 1
  • 1
Himanshu Dwivedi
  • 7,934
  • 3
  • 31
  • 52
  • try this [demo](https://developer.xamarin.com/samples/monodroid/MapsAndLocationDemo_v3/) in the BasicDemoActivity with `MapFragment` – Mike Ma Dec 29 '16 at 08:45
  • Can you add more code regarding your fragment transaction or upload a mvce(http://stackoverflow.com/help/mcve) to your post? My guess is that you need to swap the fragment using `replace()` or to reuse the fragment and not inflate. Otherwise you need to use `ChildFragmentMananger` if you have a `fragment` inside a `fragment` – Jon Douglas Dec 29 '16 at 15:41
  • Jon: Possibly, your opinion of swapping the fragment is right. But instead of that, I found another way of doing. I have updated the answer. Could please post any reference to understand this concept of replace(), reusing the fragment. – Himanshu Dwivedi Jan 01 '17 at 13:59

1 Answers1

1

Finally, after searching a lot I found the solution. Instead of using MapFragment, I used the MapView. Following is my code :

axml layout:

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

Fragment code:

public class MapViewFragment : Android.Support.V4.App.Fragment, IOnMapReadyCallback
{
    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View view = inflater.Inflate(Resource.Layout.map_layout, null);
        // Now this line works fine without throwing Inflate Exception
    }
}
Himanshu Dwivedi
  • 7,934
  • 3
  • 31
  • 52