0

I am trying to add google maps to my android application, and I followed this documentation: https://developers.google.com/maps/documentation/android/start

First off, is my Manifest file correct?

Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="where.is.the.action"
          android:versionCode="1"
          android:versionName="1.0">
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true"/>
    <permission
        android:name="where.is.the.action.permission.MAPS_RECEIVE"
        android:protectionLevel="signature"/>
    <uses-permission android:name="where.is.the.action.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:label="@string/app_name" android:icon="@drawable/ic_launcher">
        <activity android:name="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="xxxxxxxxxxxxxxxxxxxxxxxxxxxx"/>
    </application>
</manifest>

Then here is my layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent">
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="My Location"
        android:onClick="getLocation"
        android:layout_weight="0"
    />
    <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"/>
</LinearLayout>

It works fine if I remove the fragment tag, but once I add it back in the app closes and displays: "Unfortunately, MainActivity has stopped."

Does anyone see what is wrong?

Get Off My Lawn
  • 34,175
  • 38
  • 176
  • 338
  • Check logcat for errors. – MrZander Jan 23 '13 at 22:48
  • `Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.google.android.gms.maps.MapFragment: make sure class name exists, is public, and has an empty constructor that is public` and `Caused by: java.lang.ClassNotFoundException: com.google.android.gms.maps.MapFragment` – Get Off My Lawn Jan 23 '13 at 22:58
  • you haven't declared your class in the manifest, check this out also http://stackoverflow.com/questions/13719263/unable-instantiate-android-gms-maps-mapfragment/13744765#13744765 – v0d1ch Jan 23 '13 at 23:09

1 Answers1

1

Perhaps you are trying to use MapFragment on a device that is not running Android 3.0+. MapFragment is for use with an Activity and the native fragment implementation from API Level 11. SupportMapFragment is for use with a FragmentActivity and the fragment backport supplied by the Android Support Library.


UPDATE

Upon further review, the problem is probably two-fold:

  1. You attempted to attach google-play-services.jar to your project, instead of following the instructions and attaching the Play Services Android library project to your project.

  2. You did #1 above by messing with your build path directly, instead of copying the JAR to libs/.

Undo what you did originally. Then, follow the instructions to attach the Android library project to your project (which, as a side effect, will properly set up that JAR for you).

Since it appears that you are doing a command-line build, you will need to create the command-line build files for the Android library project, which Google elected not to ship (grrrrrrrrrrrrrrr). Run:

android update project -p /home/ryan/android-sdk-linux/extras/google/google_play_services/libproject/googl‌​e-play-services_lib

to create the necessary files.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Huh... I am using a Samsung GS3 to do my testing. – Get Off My Lawn Jan 23 '13 at 23:46
  • OK. You *did* add the Maps V2 Android library project to your project, right? – CommonsWare Jan 24 '13 at 00:14
  • @RyanNaddy: No, that is a JAR. It is not an Android library project. Google Play Services' Android library project *contains* a JAR, but you do not add the JAR directly to your project. – CommonsWare Jan 24 '13 at 00:18
  • now I get `Building Libraries with 'debug'... /home/ryan/android-sdk-linux/tools/ant/build.xml:595: Invalid file: /home/ryan/android-sdk-linux/extras/google/google_play_services/libproject/google-play-services_lib/build.xml BUILD FAILED (total time: 0 seconds)` – Get Off My Lawn Jan 24 '13 at 00:19
  • The error is now gone, but I am still getting: `Caused by: java.lang.ClassNotFoundException: com.google.android.gms.maps.SupportMapFragment` – Get Off My Lawn Jan 24 '13 at 00:41
  • @RyanNaddy: You are not referring to `SupportMapFragment` in the source code in your question. – CommonsWare Jan 24 '13 at 00:41
  • It was in an example so I used it, I changed it back and it works! Thanks tons! – Get Off My Lawn Jan 24 '13 at 00:43