"Error inflating class fragment" with google map
Asked Answered
C

10

16

I tried to make a sample project using Google Map, but I couldn't. Help me please!

Test Device : Android 4.0.4


Error Message :

12-29 23:45:32.605: E/AndroidRuntime(9437): FATAL EXCEPTION: main
12-29 23:45:32.605: E/AndroidRuntime(9437):
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.test_googlemap/com.example.test_googlemap.MainActivity}:
android.view.InflateException: Binary XML file line #2: Error
inflating class fragment 12-29 23:45:32.605: E/AndroidRuntime(9437):
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1968)
12-29 23:45:32.605: E/AndroidRuntime(9437):     at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1993)
12-29 23:45:32.605: E/AndroidRuntime(9437):     at
android.app.ActivityThread.access$600(ActivityThread.java:127) 12-29
23:45:32.605: E/AndroidRuntime(9437):   at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1159)
12-29 23:45:32.605: E/AndroidRuntime(9437):     at
android.os.Handler.dispatchMessage(Handler.java:99) 12-29
23:45:32.605: E/AndroidRuntime(9437):   at
android.os.Looper.loop(Looper.java:137) 12-29 23:45:32.605:
E/AndroidRuntime(9437):     at
android.app.ActivityThread.main(ActivityThread.java:4507) 12-29
23:45:32.605: E/AndroidRuntime(9437):   at
java.lang.reflect.Method.invokeNative(Native Method) 12-29
23:45:32.605: E/AndroidRuntime(9437):   at
java.lang.reflect.Method.invoke(Method.java:511) 12-29 23:45:32.605:
E/AndroidRuntime(9437):     at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
12-29 23:45:32.605: E/AndroidRuntime(9437):     at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557) 12-29
23:45:32.605: E/AndroidRuntime(9437):   at
dalvik.system.NativeStart.main(Native Method) 12-29 23:45:32.605:
E/AndroidRuntime(9437): Caused by: android.view.InflateException:
Binary XML file line #2: Error inflating class fragment 12-29
23:45:32.605: E/AndroidRuntime(9437):   at
android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
12-29 23:45:32.605: E/AndroidRuntime(9437):     at
android.view.LayoutInflater.inflate(LayoutInflater.java:466) 12-29
23:45:32.605: E/AndroidRuntime(9437):   at
android.view.LayoutInflater.inflate(LayoutInflater.java:396) 12-29
23:45:32.605: E/AndroidRuntime(9437):   at
android.view.LayoutInflater.inflate(LayoutInflater.java:352) 12-29
23:45:32.605: E/AndroidRuntime(9437):   at
com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:271)
12-29 23:45:32.605: E/AndroidRuntime(9437):     at
android.app.Activity.setContentView(Activity.java:1835) 12-29
23:45:32.605: E/AndroidRuntime(9437):   at
com.example.test_googlemap.MainActivity.onCreate(MainActivity.java:11)
12-29 23:45:32.605: E/AndroidRuntime(9437):     at
android.app.Activity.performCreate(Activity.java:4465) 12-29
23:45:32.605: E/AndroidRuntime(9437):   at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
12-29 23:45:32.605: E/AndroidRuntime(9437):     at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1932)
12-29 23:45:32.605: E/AndroidRuntime(9437):     ... 11 more 12-29
23:45:32.605: E/AndroidRuntime(9437): Caused by:
java.lang.ClassCastException: com.google.android.gms.maps.MapFragment
cannot be cast to android.support.v4.app.Fragment 12-29 23:45:32.605:
E/AndroidRuntime(9437):     at
android.support.v4.app.Fragment.instantiate(Fragment.java:394) 12-29
23:45:32.605: E/AndroidRuntime(9437):   at
android.support.v4.app.Fragment.instantiate(Fragment.java:369) 12-29
23:45:32.605: E/AndroidRuntime(9437):   at
android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:272)
12-29 23:45:32.605: E/AndroidRuntime(9437):     at
android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:669)
12-29 23:45:32.605: E/AndroidRuntime(9437):     ... 20 more



Source Code :

package com.example.test_googlemap;

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"?>
    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
           android:id="@+id/map"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           class="com.google.android.gms.maps.MapFragment"/>



Manifest.xml :

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

    <uses-sdk
        android:minSdkVersion="3"
        android:targetSdkVersion="15" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.test_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="I removed it!"/>
        </application>    
    <permission
        android:name="com.example.Test_GoogleMap.permission.MAPS_RECEIVE"
        android:protectionLevel="signature"/>
    <uses-permission android:name="com.example.Test_GoogleMap.permission.MAPS_RECEIVE"/>

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

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

</manifest>
Coucher answered 29/12, 2012 at 14:58 Comment(0)
S
44

You are extending FragmentActivity, indicating that you are trying to use the Android Support package backport of fragments. However, your <fragment> element refers to MapFragment, which is for the native API Level 11 edition of fragments.

Replace MapFragment with SupportMapFragment, and that should clear up this specific crash.

Silicify answered 29/12, 2012 at 15:54 Comment(7)
@user1875940: Then you have a problem with your API key or something. Check LogCat for messages.Silicify
Thank you for your help, Log cat show some messages that 'server return 3...'.Coucher
Yes this was exactly my problem. I copied and pasted directly from the API and it uses MapFragment. For anyone having this problem, this is a nice quick start guide to Maps API V2 docs.google.com/document/…Hughey
Thanks @RyanR I have been breaking my head over this issue for the past several hours. Finally your guide, fixed it for me. Thanks a lot :)Incomprehension
@VisheshJoshi It's not my guide, but I'm glad it helped you as it helped me :DHughey
I have followed same but i am not able to resolve it. So please provide detail step so i can resolve it.Nannie
This doesn't work into Fragment instead of Activity. Have a crash with both MapFragment or SupportMapFragment while trying create my fragment with entire map fragment at second time. At first map showing normally.Brittaneybrittani
A
6

I had the same problem and I did the mistake to only add one of the 2 following tags. Note that you are also missing one of these two

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

The actual error is really misleading, as you might be thinking of some API level UI issue.

Alcaide answered 9/8, 2014 at 16:0 Comment(0)
H
4

Some times you are using both -

<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="<YOUR VALUE>"
/>
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="<YOUR VALUE>"
/>

Make sure , Don't use both ... if you need Location , Places and maps then use geo.API_KEY and if you need places and maps then use maps.v2.API_KEY

Harlot answered 30/4, 2016 at 20:8 Comment(0)
S
2

Accepted answer is correct but meaningful information i share with you, may be anyone facing same issue what i face

If everything is woking same as google code then please check manifest file in my case i added geo key and map key that's why exception occurs,

Note - do not add two keys in manifest file remove map key

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

above code and add this code.

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

 <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version"/>
Standush answered 10/1, 2017 at 6:37 Comment(0)
B
1

This problem also occurs when it is not able to inflate the Fragment class mentioned in the activity_main.xml(or the XML file present under res/layout), due to a SDK version incompatibility in the andriod_manifest.xml file.

The correct versions for SDK levels to get the map on an emulator is this:

     <uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="21" />
Blink answered 2/9, 2014 at 5:26 Comment(0)
A
1

Even if it's an already answered question this error can also appear if you call the super.onCreateViewin your fragment. It will crash at run time.

Be sure you overridden the onCreateView method and inflated your layout:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.my_fragment, container, false);
    return view;
}
Audile answered 8/1, 2015 at 10:3 Comment(0)
N
1

In my case, I had to do two corrections to make this exception go away.

  1. The activity should extend FragmentActivity and not Activity
  2. Manifest file needs a uses-permission for ACCESS_NETWORK_STATE (my file already had INTERNET permission)
Nicolas answered 20/4, 2015 at 6:29 Comment(0)
H
1

This defect has been resolved in play services library v9.0.0. https://code.google.com/p/gmaps-api-issues/issues/detail?id=9021#makechanges

Hydrophobic answered 27/5, 2016 at 13:53 Comment(0)
L
0

Use the internet permission to be direct child of manifest file.. Like below and try..

Also You should have the following for using map:

1.Should extend Map activity in your activity file

2.Should have API key i didn't see any key in your code.

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


    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-sdk
        android:minSdkVersion="3"
        android:targetSdkVersion="15" />
Leptophyllous answered 29/12, 2012 at 15:3 Comment(1)
The OP is using Maps V2, where there is no MapActivity.Silicify
L
-1
  1. Do u have valid Google Map API key?
  2. try using

    public class MainActivity extends MapActivity{
     ... }
    
Lucia answered 29/12, 2012 at 15:5 Comment(1)
The OP is using Maps V2, where there is no MapActivity.Silicify

© 2022 - 2024 — McMap. All rights reserved.