“Error inflating class fragment” with google map

…衆ロ難τιáo~ 提交于 2019-11-27 07:53:11

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.

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.

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

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"/>

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" />
Thibault Fig

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;
}

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)

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

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" />
  1. Do u have valid Google Map API key?
  2. try using

    public class MainActivity extends MapActivity{
     ... }
    
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!