Google Maps Android API v2 - Sample Code crashes
Asked Answered
M

8

43

I'm trying to get the sample code of Android 'Google Maps Android API v2' working. I get the project built without errors. However, when I try to run the app on my Galaxy Nexus (connected with usb to my laptop), the app crashes immediately.

  • I filled in my own Maps API Key at the AndroidManifest.xml
  • I built against Android 4.1.2

This is the logging:

Unable to resolve superclass of Lcom/example/mapdemo/BasicMapActivity; (66) 
Link of class 'Lcom/example/mapdemo/BasicMapActivity;' failed 
Could not find class 'com.example.mapdemo.BasicMapActivity', referenced from method com.example.mapdemo.MainActivity.<clinit> 
VFY: unable to resolve const-class 108 (Lcom/example/mapdemo/BasicMapActivity;) in Lcom/example/mapdemo/MainActivity; 
VFY: replacing opcode 0x1c at 0x000d 
Exception Ljava/lang/NoClassDefFoundError; thrown while initializing Lcom/example/mapdemo/MainActivity; 
Class init failed in newInstance call (Lcom/example/mapdemo/MainActivity;) 
Shutting down VM 
threadid=1: thread exiting with uncaught exception (group=0x41ac9930) 
FATAL EXCEPTION: main 
java.lang.ExceptionInInitializerError
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1319)
at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5039)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method) 
Caused by: java.lang.NoClassDefFoundError: com.example.mapdemo.BasicMapActivity
at com.example.mapdemo.MainActivity.<clinit>(MainActivity.java:97)
... 15 more

Does anybody have a hint how to solve this or what I might be doing wrong?

Maker answered 5/12, 2012 at 22:46 Comment(1)
First line in the posted stacktrace: Unable to resolve superclass of Lcom/example/mapdemo/BasicMapActivity. Looks like you let BasicMapActivity extend the wrong class.Tymon
C
77

Follow the crib sheet very, very carefully:
https://docs.google.com/document/pub?id=19nQzvKP-CVLd7_VrpwnHfl-AE9fjbJySowONZZtNHzw

In particular, I think you need to:

  • Import the actual source for the "google-play-services_lib" project and link it as an Android library.
    • Do this through Project -> Properties -> Android -> Library, Add -> google-play-services_lib (you can right click on your project and choose Properties, then select Android).
    • Do not add it as a dependent Project through the "Java Build Path" for your project, that didn't work for me.
  • Add the google-play-services.jar and android-support-v4.jar into a "libs" folder in the sample project, and add them as "External External JARs" in "Build Path -> Configure Build Path -> Libraries".

I found this second step was necessary as I was hitting the exact same error as you when trying to use the sample code. The first step was necessary to avoid a NoClassDefFoundError in com.google.android.gms.R$styleable in my real project.

I also needed to do a Clean build and Uninstall the app from the device (from an earlier test attempt) before the sample code worked.

Condensation answered 5/12, 2012 at 23:41 Comment(4)
Thanks! I indeed had to put the android-support-v4.jar in a new "libs" folder. (Before I added this jar as an external jar dependency.) Now the app doesn't crashes anymore.Maker
Thanks, it worked for me. BTW: Is there something on Android that really works or do you need workaround for everything?Bremser
Unfortunately those steps didn't work for me. I then followed John Oleynik's steps (below: https://mcmap.net/q/95375/-google-maps-android-api-v2-sample-code-crashes), and that worked.Foulness
Worked for me aslong only when I removed the google-play-services.jar from the libs dir. All your other steps worked.Marmalade
E
26

Follow next sample code instructions by google.

Skip on "4" section, and follow below instructions:

  • Add new folder to your project named "libs"
  • Copy google-play-services.jar form:

    "android-sdk-folder\extras\google\google_play_services\libproject\google-play-services_lib\libs"

    to "libs" folder --> right click on this jar in this folder --> Build path --> Add to build path.

  • Copy android-support-v4.jar form:

    "android-sdk-folder\extras\android\support\v4"

    to "libs" folder --> right click on this jar in this folder --> Build path --> Add to build path.

Where "android-sdk-folder" usually looks like: "C:\Users\User_Name\android-sdks"

Emogeneemollient answered 10/12, 2012 at 22:45 Comment(0)
O
14

I had the same issue, I did the following to fix the problem.

  1. Go to "Properties" of the project.
  2. Select "Java Build Path"
  3. Select "Order and Export" Tab
  4. You should see the selected project's "src" and "gen" paths and dependencies here.

The order how they listed were first "src" and then "gen" path I switch them, so that "gen" folder is build before the "src"

THEN "Check" each one of the items listed

see: NoClassDefFoundError for code in an Java library on Android

Overtask answered 29/3, 2013 at 18:56 Comment(0)
E
2

This happens because of a lot of confusion over how to import projects. The docs https://developers.google.com/maps/documentation/android/intro#sample_code say you should not select "Copy projects into workspace" at any point and for getting the samples running this is what you need to stick to. Do not select this option for the play services and samples - leave them where they are.

After you import both check the following:

  1. Right click the MainActivity project and choose properties
  2. Go to Android and make sure you see a green tick next to google-play-services-lib (The play services lib should never be added as a jar - instead it should be added as an Android library)
  3. Go to Java Build Path and import external jar ( android-support-v4.jar) in the libraries section
  4. [IMPORTANT] Go to Order and Export in the Java Build Path and make sure the android-support-v4.jar has a tick next to it (it won't happen by default and this is why you are seeing the error that you mention at the top)
  5. Make sure you clean and build the project after making changes, so that you don't end up trying to fix things that work, but were simply not pushed.
Emendation answered 9/6, 2013 at 17:42 Comment(0)
B
1
  1. Follow sample code instructions by google.
  2. And set Java build path like this :enter image description here
Bedlamite answered 20/9, 2013 at 7:14 Comment(0)
B
0

I faced the same problem and couldn't resolve. Finally I took the sample projects folder from another machine, copied it to mine and loaded the new project I took from the other machine. Then everything is working fine. I tried to track the issue by comparing 2 sample projects but couldn't. Anyway the new sample projects I took from the other machine is working fine.

Bassinet answered 23/4, 2013 at 11:50 Comment(0)
D
0

I had much the same problem. The file 'import-summary.txt' had the answer. In my case I had imported the eclipse project into a path with a space in the pathname. This causes a problem with the wizard. Created new path. Re-imported project. Then Build/Rebuild Project. No problems.

Dorthydortmund answered 17/3, 2015 at 5:46 Comment(0)
F
0
  1. Try out the same sample code in the new workspace.

  2. Even you can copy and paste the sample map project and google play services-lib project (/extras/google/google_play_services/libproject/google-play-services_lib,) and paste that in the new fresh workspace.

  3. After donot forget to add the REQUIRED JAR FILE FROM SDK. ("android-support-v4.jar" is located in "/extras/android/compatibility/v4/android-support-v4.jar" under your "android-sdk" drectory.)
Fondue answered 23/6, 2015 at 11:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.