Android - Google Maps API v2 - SupportMapFragment Errors
Asked Answered
F

10

13

I am trying to get one of my apps back up and running with the Google Maps API v2 for the first time. I created a key for my app in my keystore, extracted the SHA1 hash, acquired an API key, then did the following in-app... I included:

google-play-services.jar

as well as importing the GooglePlayServices libraryand adding it as a reference to the project. In my Java code I simply just load the layout resource.

public class Times extends Activity{
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map);
    }
}

In the layout (res/layout/map.xml) that I am trying to instantiate I have:

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

I also have the following declared in my manifest:

<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="tinyTech.us.ua.busschedule.permission.MAPS_RECEIVE" />

<permission
    android:name="tinyTech.us.ua.busschedule.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

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

And declared in the Application tag of the manifest:

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

When the activity is loaded, it crashes with the following errors in LogCat:

FATAL EXCEPTION: main
java.lang.NoClassDefFoundError: com.google.android.gms.R$styleable
    at com.google.android.gms.maps.GoogleMapOptions.createFromAttributes(Unknown Source)

I have attempted to research the problem, however I have been unable to find what I am looking for. Any and all help is greatly appreciated.

Forwardlooking answered 5/12, 2012 at 15:7 Comment(2)
What is your activity code? You're trying to cast the SupportMapFragment to the non-support version of Fragment as evidenced by the log Caused by: java.lang.ClassCastException: com.google.android.gms.maps.SupportMapFragment cannot be cast to android.app.FragmentArmor
yes, it because he is using Activity not FragmentActivity as base class for his activities and you can find this in my answer :)Metalworking
G
23

You need to extend FragmentActivity if you are using SupportMapFragment. If you are using the MapFragment you can extend Activity.

Galligaskins answered 10/12, 2012 at 20:52 Comment(1)
I have the same problem but I'm extending FragmentActivity instead of Activity.Orangewood
M
11

In your layout, use

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

instead of

...    
android:name="com.google.android.gms.maps.SupportMapFragment"
...
Mikkel answered 10/5, 2013 at 13:13 Comment(0)
P
7

You must do this:

public class MainActivity extends android.support.v4.app.FragmentActivity{

}
Parrett answered 18/12, 2012 at 19:4 Comment(1)
If you can get MapFragment but not SuppoortMapfragment this is why!Flattish
O
5

This worked for me: https://mcmap.net/q/95197/-unable-instantiate-android-gms-maps-mapfragment

Seems like you have to add Google Play services as module, not just as .jar

Orangewood answered 11/12, 2012 at 13:31 Comment(0)
H
5

I solved this error by checking "Copy projects into workspace" while importing the Google Play Services lib.
More info here: http://developer.android.com/google/play-services/setup.html

Hindgut answered 19/4, 2013 at 18:0 Comment(0)
P
1

i had the same problem with Google Play Services and everything.. i included as a library and all, and the problem still persisted ("could not find com.google.android.gms.maps.suportmapfragment")..

i was able to fix it by: Going to Project Properties. Then select Java Build Path. Then Select Order and Export tab. then, Make sure you check the Android Dependencies and Android Private Libraries

Hope this helps :)

Profundity answered 21/6, 2013 at 13:47 Comment(0)
C
1

I think i had the same problem it seems that newer versions you have to add this line at the manifest

<meta-data
    android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />
Cremate answered 3/11, 2014 at 8:18 Comment(0)
V
0

I've added <uses-library android:name="com.google.android.maps" /> in the manifest file. This worked for me!

Vandavandal answered 13/6, 2014 at 9:40 Comment(0)
C
0

My case, I add:

File -> New -> Google -> Google Maps Activity

And i follow this tutorial.

Capillary answered 4/8, 2017 at 16:14 Comment(0)
E
0

I tried evrything shown above but, still i was getting error. So, downgrade version from 17.1.0:

implementation 'com.google.android.gms:play-services-maps:17.1.0'

to 16.1.0

implementation 'com.google.android.gms:play-services-maps:16.1.0'

worked for me!

Elizaelizabet answered 24/11, 2020 at 10:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.