2

I'm trying to display map using map api v2. I searched for the answer similar to my problem. On Jul 23, "Android Error: Binary XML file line #16: Error inflating class fragment when adding map fragment." And I changed my sources according to the answer. But still I have a problem when using map fragment. Any help is appreciated. Thank you.

Here is my xml code:

<RelativeLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<fragment 
    class="com.google.android.gms.maps.MapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</RelativeLayout>

Here is Code in activity:

package com.example.googlemap;

import android.os.Bundle;
import android.app.Activity;
import android.support.v4.app.*;
import android.view.Menu;

public class MainActivity extends FragmentActivity {

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
}

Here's manifest file:

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

  <uses-sdk android:minSdkVersion="12"
          android:targetSdkVersion="16" />
  <uses-feature android:required="true"
             android:glEsVersion="0x00020000"/>
  <permission android:name="com.example.googlemap.permission.MAPS_RECEIVE" 
    android:protectionLevel="signature"/>
  <uses-permission android:name="com.example.googlemap.permission.MAPS_RECEIVE"/>
  <uses-permission android:name="android.permission.INTERNET"/>
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
  <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
  <uses-permission android:name ="com.google.android.providers.gsf.permission.READ_GSERVICES"/>

  <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.googlemap.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>
    <meta-data android:name="com.google.android.maps.v2.API_KEY"
               android:value="API key" />
  </application>
</manifest>

I'm trying to run it on my device (Andorid 4.1.2 which is api 16). I've added google-play-services-lib in my procject. When I run app, it just crashes. Here is full logcat log:

11-15 17:53:35.785: E/AndroidRuntime(23708): FATAL EXCEPTION: main
11-15 17:53:35.785: E/AndroidRuntime(23708): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.googlemap/com.example.googlemap.MainActivity}: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
11-15 17:53:35.785: E/AndroidRuntime(23708):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2081)
11-15 17:53:35.785: E/AndroidRuntime(23708):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2106)
11-15 17:53:35.785: E/AndroidRuntime(23708):    at android.app.ActivityThread.access$600(ActivityThread.java:138)
11-15 17:53:35.785: E/AndroidRuntime(23708):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1204)
11-15 17:53:35.785: E/AndroidRuntime(23708):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-15 17:53:35.785: E/AndroidRuntime(23708):    at android.os.Looper.loop(Looper.java:137)
11-15 17:53:35.785: E/AndroidRuntime(23708):    at android.app.ActivityThread.main(ActivityThread.java:4911)
11-15 17:53:35.785: E/AndroidRuntime(23708):    at java.lang.reflect.Method.invokeNative(Native Method)
11-15 17:53:35.785: E/AndroidRuntime(23708):    at java.lang.reflect.Method.invoke(Method.java:511)
11-15 17:53:35.785: E/AndroidRuntime(23708):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
11-15 17:53:35.785: E/AndroidRuntime(23708):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
11-15 17:53:35.785: E/AndroidRuntime(23708):    at dalvik.system.NativeStart.main(Native Method)
11-15 17:53:35.785: E/AndroidRuntime(23708): Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
11-15 17:53:35.785: E/AndroidRuntime(23708):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
11-15 17:53:35.785: E/AndroidRuntime(23708):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
11-15 17:53:35.785: E/AndroidRuntime(23708):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
11-15 17:53:35.785: E/AndroidRuntime(23708):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
11-15 17:53:35.785: E/AndroidRuntime(23708):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
11-15 17:53:35.785: E/AndroidRuntime(23708):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:408)
11-15 17:53:35.785: E/AndroidRuntime(23708):    at android.app.Activity.setContentView(Activity.java:2094)
11-15 17:53:35.785: E/AndroidRuntime(23708):    at com.example.googlemap.MainActivity.onCreate(MainActivity.java:13)
11-15 17:53:35.785: E/AndroidRuntime(23708):    at android.app.Activity.performCreate(Activity.java:5253)
11-15 17:53:35.785: E/AndroidRuntime(23708):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1082)
11-15 17:53:35.785: E/AndroidRuntime(23708):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2045)
11-15 17:53:35.785: E/AndroidRuntime(23708):    ... 11 more
11-15 17:53:35.785: E/AndroidRuntime(23708): Caused by: java.lang.IllegalArgumentException: Binary XML file line #11: Must specify unique android:id, android:tag, or have a parent with an id for com.google.android.gms.maps.MapFragment
11-15 17:53:35.785: E/AndroidRuntime(23708):    at android.app.Activity.onCreateView(Activity.java:4882)
11-15 17:53:35.785: E/AndroidRuntime(23708):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
11-15 17:53:35.785: E/AndroidRuntime(23708):    ... 21 more
laalto
  • 150,114
  • 66
  • 286
  • 303
user2995602
  • 21
  • 1
  • 1
  • 2

7 Answers7

0

Try replacing the following snippets:

class="com.google.android.gms.maps.MapFragment"

with:

name="com.google.android.gms.maps.MapFragment"

Edit:

Take also a look at this snippet from your stacktrace:

Caused by: java.lang.IllegalArgumentException: Binary XML file line #11: Must specify unique android:id, android:tag, or have a parent with an id for com.google.android.gms.maps.MapFragment
Benjamin W.
  • 121
  • 4
0

You have to add two meta-data tags just before the </application> tag.

<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="**Replace with your api_key**" />
Kaveesh Kanwal
  • 1,753
  • 17
  • 16
0

Take a look at google maps demos for proper usage: https://github.com/googlemaps/android-samples

or define an android:id and start map fragment as in the following link: https://developers.google.com/maps/documentation/android-api/

ugur
  • 3,604
  • 3
  • 26
  • 57
0

Can you replace below xml code:

<fragment 
class="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />

With

 <fragment
android:id="@+id/map"
class="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
RobinHood
  • 10,897
  • 4
  • 48
  • 97
0

In activity_main.xml behalf of this code you should use

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

//name="Package name.that fragment class"

Peter
  • 587
  • 6
  • 16
0

I have the same issue and here is how I fix it

I make sure that I added this meta data to my manifest

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

    <meta-date 
      android:name="com.google.android.maps.v2.API_KEY"   
      android:value="@string/google_maps_key"/>

   <meta-data 
      android:name="com.google.android.geo.API_KEY"  
      android:value="@string/google_maps_key"/>

and make sure you add id to my fragment

<fragment
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
Mina Fawzy
  • 20,852
  • 17
  • 133
  • 156
0

From your error log: Binary XML file line #11: Must specify unique android:id, android:tag, or have a parent with an id for com.google.android.gms.maps.MapFragment

From the official documentation:

https://developer.android.com/guide/components/fragments.html

You should specify ID and TAG for your fragment.

So change your layout from :

<fragment 
    class="com.google.android.gms.maps.MapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
</RelativeLayout>

to:

<fragment 
    android:id="@+id/your_desired_id"
    android:tag="tag"
    class="com.google.android.gms.maps.MapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
</RelativeLayout>
zoom
  • 160
  • 1
  • 1
  • 11