6

I was trying to show the maps in android application. I am using map V2. But the error "Binary XML file line #9: error inflating class fragment" is thrown. The app is crashed on launch.

Below is my code. Please help me in solving this stuff

MainActivity.Java

    package com.example.googlemapsv2;
    import android.os.Bundle;
    import android.support.v4.app.FragmentActivity;

    public class MainActivity extends FragmentActivity {

      @Override
      protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
    }
   }

activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

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

Android manifest

 <?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.googlemapsv2"
android:versionCode="1"
android:versionName="1.0" >
 <permission
    android:name="com.example.googlemapsv2.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

<uses-permission android:name="com.example.googlemapsv2.permission.MAPS_RECEIVE" />
<uses-sdk
    android:minSdkVersion="12"
    android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<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="com.example.googlemapsv2.MainActivity"
        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.maps.v2.API_KEY"
 android:value="AIzaSyBAvQK4RAin10dqyflr95tH45Ce_Eko3G0" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</manifest>
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
ARN
  • 175
  • 1
  • 1
  • 13
  • change this `android:minSdkVersion="12"` to `11`. – Raghunandan Dec 25 '13 at 04:03
  • @Raghunandan: That is not going to help me in any way. I am using emulator 4.3. For your sake, I tried. Still got the same error – ARN Dec 25 '13 at 04:08
  • 1
    https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/MapFragment. pls check this Use this class only if you are targeting API 12 and above. Otherwise, use SupportMapFragment.. why would it not help?. Your min sdk is 12 in which case you should use `MapFragment` and extending `Activity`. Also i posted it as a comment not an answer. Need more info. Have you referenced google play services library project. – Raghunandan Dec 25 '13 at 04:10
  • 1
    Yes.. I referenced google play services. I hope minSDKVersion is different from TargetSDKVersion. Going with your comment, I use MapFragment with MinSDKVersion 12. As mentioned by you, I am using this by targeting API level 12 and above. There is no need of changing that to 11. – ARN Dec 25 '13 at 04:19
  • 1
    Then you need to change `SupportMapFragment` to `MapFragment` in xml and your Activity class should extend Activity. also post the stacktrace – Raghunandan Dec 25 '13 at 04:22
  • Not a solution specifically to this issue but related and solved my problem. The manifest cannot specify both `com.google.android.maps.v2.API_KEY` (deprecated) and `com.google.android.geo.API_KEY`. If your manifest has both, get rid of the maps.v2 one and keep the geo one. If both are specified then you run into this inflation error issue. This is addressed specifically at https://developers.google.com/maps/documentation/android/start – JZC Apr 09 '15 at 18:58

4 Answers4

9

The below must be inside application tag of manifest file

Reference

https://developers.google.com/maps/documentation/android/start#add_the_google_play_services_version_to_your_apps_manifest

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

Since your minsdk is 12. You need to use MapFragment and your class should extend Activity.

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

Then

public class MainActivity extends Activity

Make sure you reference the library project properly and enabled maps for android in the google api console.

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
5

Try this with SupportMapFragment activity_main.xml

change android:name to simple class

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

MainActivity.java

public class MainActivity extends FragmentActivity {

    // Google Map
    private GoogleMap googleMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        try {
            // Loading map
            initializeMap();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * function to load map. If map is not created it will create it for you
     * */
    private void initializeMap() {
        if (googleMap == null) {
            googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
            // check if map is created successfully or not
            if (googleMap == null) {
                Toast.makeText(getApplicationContext(),
                    "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                    .show();
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }
}

Change SDK version

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

Raghunandan is right , you should write <meta>tag code inside the <application>.

Chintan Khetiya
  • 15,962
  • 9
  • 47
  • 85
  • The app is crashing even before GoogleMap object is initialized – Raghunandan Dec 25 '13 at 04:20
  • @Raghunandan How it can be, i am using that same code in my app,and i am initializing if that is `null` in `initializeMap` private function. – Chintan Khetiya Dec 25 '13 at 04:34
  • pls refer https://developers.google.com/maps/documentation/android/start#add_the_google_play_services_version_to_your_apps_manifest. pls check the topic Add a map. check steps 1,2 and 3. So you should see the map. If you want put a marker then you need to initialize map object. – Raghunandan Dec 25 '13 at 04:35
  • the app crashes even before that. **change android:name** to simple class what difference would it make?. again refer the docs step 1 at the bottom of the docs – Raghunandan Dec 25 '13 at 04:37
  • http://developer.android.com/guide/components/fragments.html.Quoting The `android:name` attribute in the `` specifies the `Fragment class` to instantiate in the layout. – Raghunandan Dec 25 '13 at 04:44
  • @Raghunandan Yeah that is right , but here he has use , support fragment , see https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/SupportMapFragment – Chintan Khetiya Dec 25 '13 at 04:48
  • no need to use `SupportFragment` in the first place as op wants to target api level 12 and above. you can use android:name that is not a issue. http://stackoverflow.com/questions/10162983/activity-layout-fragment-class-vs-androidname-attributes and check the comment section below commonsware post – Raghunandan Dec 25 '13 at 04:49
  • 1
    @Raghunandan Yeah, that's true i have test with `android:name="com.google.android.gms.maps.SupportMapFragment"` and that is also working, but i have implement as per [this](https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/SupportMapFragment). – Chintan Khetiya Dec 25 '13 at 04:56
  • i am not saying its wrong. i am only saying this does not help op solve the problem. 1. he wants to target api level 12 and above. 2. you are suggesting `SupportMapFragment` which is for api level 11 and below. 3. App crashes before GoogleMap object is initialized. Atleast op should see the map – Raghunandan Dec 25 '13 at 04:59
1

In my experience formerly API 18/19 must adding in menifest -> application meta-data Q update updated with the version:

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

Yaakov Klein
  • 131
  • 1
  • 5
1

I had the same problem,I solved it by adding permissions as specified in the official doc (Google map doc on permissions)

<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

kalan nawarathne
  • 1,944
  • 27
  • 26