0

I'm trying to run simple demo app of Google Maps. I've referred standard article.

But I'm getting Error inflating class fragment exception.

I've updated mt manifest properly. Registered for Map API key. Install google-play-services and linked it to my current project. Extended my activity from FragmentActivity, still it doesn't work.

Am I doing anything wrong?

I also referred Error inflating class fragment but to no avail. Any help appreciated.

My code is as follows: activity_main.xml

<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"
    tools:context=".MainActivity" >

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

</RelativeLayout> 

MainActivity.java

package com.android.googlemapsdemo;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

public class MainActivity extends FragmentActivity {
    static final LatLng HAMBURG = new LatLng(53.558, 9.927);
    static final LatLng KIEL = new LatLng(53.551, 9.993);
    private GoogleMap map;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
                .getMap();
        if (map != null) {
            Marker hamburg = map.addMarker(new MarkerOptions()
                    .position(HAMBURG).title("Hamburg"));
            Marker kiel = map.addMarker(new MarkerOptions()
                    .position(KIEL)
                    .title("Kiel")
                    .snippet("Kiel is cool")
                    .icon(BitmapDescriptorFactory
                            .fromResource(R.drawable.ic_launcher)));

            // Move the camera instantly to hamburg with a zoom of 15.
            map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));

            // Zoom in, animating the camera.
            map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
        } else {
            map = ((MapFragment) getFragmentManager()
                    .findFragmentById(R.id.map)).getMap();
            if (map == null) {
                Toast.makeText(MainActivity.this, "Unable to generate map",
                        Toast.LENGTH_SHORT).show();
            }
        }
    }
}

AndroidManifest.xml

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

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="19" />

    <permission
        android:name="com.android.googlemapsdemo.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.android.googlemapsdemo.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="AIzaSyAT9_V5YI-7CMf_Yta0Y_zeDfPZeH5XFL0" />
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
    </application>

</manifest>
Community
  • 1
  • 1
GAMA
  • 5,958
  • 14
  • 79
  • 126

2 Answers2

1

I think you should change this

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

to

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

Because your minsdk="11"

also change this

 map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
            .getMap();

to

 map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
            .getMap();
M D
  • 47,665
  • 9
  • 93
  • 114
1

Change this

 <uses-sdk
    android:minSdkVersion="11"

to

 <uses-sdk
    android:minSdkVersion="12"

If you have 11 you will be using SupportMapFragment. Consider this

https://developer.android.com/about/dashboards/index.html?utm_source=ausdroid.net

Looking at the link it is better to change to 12

Change this

public class MainActivity extends FragmentActivity 

to

public class MainActivity extends Activity  

Also getMap() can return null. Better check the availability of google map services.

You are also missing

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

in manifest file

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

You can also remove

  <permission
    android:name="com.android.googlemapsdemo.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

and this

 <uses-permission android:name="com.android.googlemapsdemo.permission.MAPS_RECEIVE" />
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • Made minSdkVersion as 15, extended from `FragmentActivity`, imported `import android.support.v4.app.FragmentActivity;`, removed those lined from manifest, still same error :( – GAMA May 19 '14 at 05:43
  • @GAMA extend `Activity`. No need for `FragmentActivity`. `FragmentActivity` is the base class for support based fragments. Since you will be not using `SupportMapFragment` there is no need to extend `FragmentActivity`. – Raghunandan May 19 '14 at 05:43
  • So should I make 1)minSdkVersion as 15 & 2)remove those lined from manifest FROM ABOVE MENTIONED CODE IN QUESTION, that's it? – GAMA May 19 '14 at 05:49
  • @GAMA Deleted the answer. You have choices. If you want to support gingerbreads and below, consider using `FragmentActivity` and `SupportMapFragment`. – Glenn May 19 '14 at 05:50
  • yes make it above 11. coz there are not many devices in the market that run on 11. yes those lines are not required. You need to extend Activity – Raghunandan May 19 '14 at 05:50
  • @GAMA did you even try to read the docs the link i posted in my post?? – Raghunandan May 19 '14 at 05:51
  • After adding access_network_state permission and making minSdkVersion 12, it worked. Now, it's throwing map key authorization failure but that's a different issue, Thanks a ton! – GAMA May 19 '14 at 05:57
  • I'm going through 2nd article to set-up Map correctly, Cheers! – GAMA May 19 '14 at 06:00
  • @GAMA you need to check the key in the google api console for that – Raghunandan May 19 '14 at 06:26