0

I am using GoogleMap in android app using Android Studio. It was working fine and don't know why suddenly it stopped working. minSdkVersion is 15 and targetSdkVersion is 24. Please check my below code and help me to come out of this :

XML:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <fragment
        android:id="@+id/mapFragment"
        class="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</FrameLayout>

Gridle:

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.2.0'
    compile 'com.android.support:design:24.2.0'
    compile 'com.google.android.gms:play-services:9.4.0'
    compile 'com.google.maps.android:android-maps-utils:0.4.3'
    compile 'com.loopj.android:android-async-http:1.4.9'
    compile 'com.google.code.gson:gson:2.7'
    compile 'com.googlecode.android-query:android-query:0.24.3'
    compile 'com.makeramen:roundedimageview:2.2.1'
}

Java:

    public class MapsActivity extends Fragment implements OnMapReadyCallback,
            GoogleApiClient.ConnectionCallbacks,
            GoogleApiClient.OnConnectionFailedListener,
            LocationListener {
          @Override
          public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
          SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager()
                        .findFragmentById(R.id.mapFragment);
          }
    }

Manifest:

        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="@string/google_maps_key" />

Error:

It is giving error in xml at <fragment, error: android.view.InflateException: Binary XML file : Error inflating class fragment

Jeeten Parmar
  • 5,568
  • 15
  • 62
  • 111

6 Answers6

4

After adding permission in manifest, It solved my issue. Check below code :

<uses-permission
        android:name="android.permission.WRITE_EXTERNAL_STORAGE"
        android:maxSdkVersion="24" />
<uses-permission
        android:name="android.permission.READ_EXTERNAL_STORAGE"
        android:maxSdkVersion="24" />
Jeeten Parmar
  • 5,568
  • 15
  • 62
  • 111
0

Please use getMapAsync.

SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this)
Mangal
  • 424
  • 4
  • 5
0

In your onCreate add:

 SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

In XML add context:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.___yourPackageName______"
android:layout_width="match_parent"
android:layout_height="match_parent">

<fragment
    android:id="@+id/mapFragment"
    class="com.google.android.gms.maps.SupportMapFragment"
    tools:context="com.___yourPackageName______"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</FrameLayout>
W4R10CK
  • 5,502
  • 2
  • 19
  • 30
0

getChildFragmentManager is used in Nested Fragments. The fragment you've used is not a child of another fragment. Use getSupportFragmentManager or getFragmentManager for fragment transactions.

Take a look at this question for more information.

In addition, I would like to mention that You're integrating an old Maps API. Try the latest one https://developers.google.com/maps/documentation/android-api/map

Community
  • 1
  • 1
Srikar Reddy
  • 3,650
  • 4
  • 36
  • 58
0

If it crashes second time, you need to destroy it.

public void onDestroy() {
    super.onDestroy();
    getFragmentManager().beginTransaction().remove(mapfragmentnamehere).commit();
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Priyanka
  • 381
  • 1
  • 6
  • 8
-1
public void onMapReady(GoogleMap googleMap) {

    setUpMap(googleMap);

}
Mangal
  • 424
  • 4
  • 5