Binary XML file -Error inflating class fragment in Google Map V2 (Android Studio)

1.7k views Asked by At

I am trying to integrate Google Map V2 in my project in android studio.I added google play services as dependencies in build.gradle file as below :

dependencies {
        compile fileTree(include: ['*.jar'], dir: 'libs')
        compile 'com.android.support:appcompat-v7:21.0.0'
        compile 'com.google.android.gms:play-services:6.1.71'
    }

I also changed my AndroidManifest.xml file as follows:

<permission  android:name="app.me.locationalarm.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-permission android:name="app.me.locationalarm.permission.MAPS_RECEIVE" />

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <!-- Required to show current location -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <!-- Required OpenGL ES 2.0. for Maps V2 -->
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".HomeActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

    <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="MY_API_KEY" />
</manifest>

In my view layout I add a fragment.

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

In my activity I create a method createMapView as below:

private void createMapView(){
        try {
            if(null == googleMap){
                     googleMap =  ((MapFragment) getFragmentManager().findFragmentById(R.id.mapView)).getMap();
            if(null == googleMap) {
                    Toast.makeText(getApplicationContext(),
                            "Error creating map", Toast.LENGTH_SHORT).show();
                }
            }
        } catch (NullPointerException exception){
            Log.e("mapApp", exception.toString());
        }
    }

I called this method from my OnCreate Method. When I tried to execute my project, I found an Error like that:

FATAL EXCEPTION: main
    Process: app.me.locationalarm, PID: 18446
    java.lang.RuntimeException: Unable to start activity ComponentInfo{app.me.locationalarm/app.me.locationalarm.HomeActivity}: android.view.InflateException: Binary XML file line #11: Error inflating class fragment

My target SdkVersion is 21.Any idea how I can solve it? I have found some answer but none of these help me out.

2

There are 2 answers

0
mahfuz195 On BEST ANSWER

put "meta-data" tag information within "applicaiton" tag in mainfest.

<?xml version="1.0" encoding="utf-8"?>

<permission
    android:name="app.me.locationalarm.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

<uses-permission android:name="app.me.locationalarm.permission.MAPS_RECEIVE" />

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<!-- Required to show current location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<!-- Required OpenGL ES 2.0. for Maps V2 -->
<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".HomeActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <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="API_KEY" />
</application>

0
Haresh Chhelana On

Try to replace MapFragment by SupportMapFragment. It would be something like:

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

googleMap =  ((SupportMapFragment)getFragmentManager().findFragmentById(R.id.mapView)).getMap();