0

I am trying to run the sample Google Maps Android API v2 found on this page in Android Studio.

My question is why am I getting the android.view.InflateException: Binary XML file line #2: Error inflating class fragment shown in the logcat below?

Environment: IDE: Android Studio 1.0.1 Device: Galaxy Nexus, Android version 4.2.1

I have at looked at the related questions and tried the suggested solutions, but nothing has worked.

Most of the other question were using a FragmentActivity and needed a SupportMapFragment but I am using a regular Activity and MapFragment as shown in the example on the web page. I am using a minimum API level of 15 so I thought the FragmentActivity and SupportMapFragment were not necessary.

I have copied the example code directly from the web page.

The error from the logcat. (Only included the first bit.)

02-12 19:59:49.496  23891-23891/edu.utexas.scottm.googlemapsapi_2_demo E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{edu.utexas.scottm.googlemapsapi_2_demo/edu.utexas.scottm.googlemapsapi_2_demo.SimpleMapActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)

The layout file activity_simple_map.xml (copied from the web page cited)

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

The Activity (copied from the web page cited)

package edu.utexas.scottm.googlemapsapi_2_demo;

import android.app.Activity;
import android.os.Bundle;


public class SimpleMapActivity extends Activity {

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

The manifest file (with my MAPS API key removed.)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="edu.utexas.scottm.googlemapsapi_2_demo" >

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true"/>

    <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"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".SimpleMapActivity"
            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.MY_API_KEY"
            android:value="MY_API_KEY"/>
    </application>

</manifest>

The build.gradle file for the Module: app

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "edu.utexas.scottm.googlemapsapi_2_demo"
        minSdkVersion 15
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

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

I have created an Android Studio project from the Google Play Services sample apps found at \android-sdk\extras\google\google_play_services\samples\maps, and it works on the device, but this uses FragmentActivity and SupportMapFragment.

Any help would be greatly appreciated.

Whole logcat in case needed:

02-12 19:59:49.496  23891-23891/edu.utexas.scottm.googlemapsapi_2_demo E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{edu.utexas.scottm.googlemapsapi_2_demo/edu.utexas.scottm.googlemapsapi_2_demo.SimpleMapActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5039)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
            at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
            at android.app.Activity.setContentView(Activity.java:1881)
            at edu.utexas.scottm.googlemapsapi_2_demo.SimpleMapActivity.onCreate(SimpleMapActivity.java:12)
            at android.app.Activity.performCreate(Activity.java:5104)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5039)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.RuntimeException: API key not found.  Check that <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="your API key"/> is in the <application> element of AndroidManifest.xml
            at com.google.maps.api.android.lib6.gmm6.c.g.a(Unknown Source)
            at com.google.maps.api.android.lib6.c.i.a(Unknown Source)
            at com.google.maps.api.android.lib6.c.el.a(Unknown Source)
            at com.google.maps.api.android.lib6.c.ab.a(Unknown Source)
            at com.google.maps.api.android.lib6.c.aa.a(Unknown Source)
            at com.google.android.gms.maps.internal.x.onTransact(SourceFile:107)
            at android.os.Binder.transact(Binder.java:310)
            at com.google.android.gms.maps.internal.IMapFragmentDelegate$a$a.onCreateView(Unknown Source)
            at com.google.android.gms.maps.MapFragment$a.onCreateView(Unknown Source)
            at com.google.android.gms.dynamic.a$4.b(Unknown Source)
            at com.google.android.gms.dynamic.a.a(Unknown Source)
            at com.google.android.gms.dynamic.a.onCreateView(Unknown Source)
            at com.google.android.gms.maps.MapFragment.onCreateView(Unknown Source)
            at android.app.Fragment.performCreateView(Fragment.java:1695)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:861)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
            at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1137)
            at android.app.Activity.onCreateView(Activity.java:4717)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
            at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
            at android.app.Activity.setContentView(Activity.java:1881)
            at edu.utexas.scottm.googlemapsapi_2_demo.SimpleMapActivity.onCreate(SimpleMapActivity.java:12)
            at android.app.Activity.performCreate(Activity.java:5104)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5039)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
            at dalvik.system.NativeStart.main(Native Method)
Mike Scott
  • 296
  • 3
  • 12

4 Answers4

2

Took a closer look at the logcat and found this:

 Caused by: java.lang.RuntimeException: API key not found.  Check that <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="your API key"/> is in the <application> element of AndroidManifest.xml

The problem was in the manifest.

<meta-data
    android:name="com.google.android.maps.v2.MY_API_KEY"
    android:value="MY_API_KEY"/>

I put my API key at the end of com.google.android.maps.v2. instead of leaving it as

 android:name="com.google.android.maps.v2.API_KEY"

Did not read the instructions on the web page closely enough. They clearly state:

Substitute your API key for API_KEY in the value attribute.

Doh.

Mike Scott
  • 296
  • 3
  • 12
1
<?xml version="1.0" encoding="utf-8"?>
    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/iqamah_map"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:name="com.google.android.gms.maps.SupportMapFragment" />

Use FragmentActivity instead Activity

public class SimpleMapActivity extends FragmentActivity {

GoogleMap map;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_simple_map);
        map =((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.iqamah_map)).getMap();

    }
}
casillas
  • 16,351
  • 19
  • 115
  • 215
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
  • 1
    Sorry, I don't want to use the support library classes. My min API level is 15, so I don't need the FragmentActivity, – Mike Scott Feb 13 '15 at 14:35
0

I'm not sure if i can help with the error, but this link seems to fit your needs. I used it recently for an app i created and it explains everything very well. It uses a fragment from an Activity. Hope it helps!

http://www.vogella.com/tutorials/AndroidGoogleMaps/article.html

Noodelz
  • 143
  • 1
  • 11
  • Thanks, but that example uses Eclipse not Android Studio. I have looked over the example code and files on that site and can't find any significant differences. – Mike Scott Feb 13 '15 at 02:43
0

You have three problems in the code that your logcat suggest:

  1. First Get your API_KEY. Follow this link to get your API_KEY. Replace it in android:value="MY_API_KEY" in the manifest.xml file.
  2. Change your java file to:

    public class SimpleMapActivity extends FragmentActivity {
    
    GoogleMap map;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_simple_map);
        map =((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.iqamah_map)).getMap();
     }
    }
    
  3. Change your .xml file to:

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

Hope this Helps!!

casillas
  • 16,351
  • 19
  • 115
  • 215
AniV
  • 3,997
  • 1
  • 12
  • 17