Google map API v2 is not working

2019-01-22 23:17发布

问题:

I am trying to run sample code mention here : Google Maps Android API V2 Sample code

but i am getting error NoClassDefFound. Below is my logcat.

Can any one help me to solve it. Any help is highly appricated Thanks

12-04 18:04:18.854: E/AndroidRuntime(1801): java.lang.ExceptionInInitializerError
12-04 18:04:18.854: E/AndroidRuntime(1801):     at java.lang.Class.newInstanceImpl(Native Method)
12-04 18:04:18.854: E/AndroidRuntime(1801):     at java.lang.Class.newInstance(Class.java:1319)
12-04 18:04:18.854: E/AndroidRuntime(1801):     at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
12-04 18:04:18.854: E/AndroidRuntime(1801):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
12-04 18:04:18.854: E/AndroidRuntime(1801):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
12-04 18:04:18.854: E/AndroidRuntime(1801):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
12-04 18:04:18.854: E/AndroidRuntime(1801):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
12-04 18:04:18.854: E/AndroidRuntime(1801):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-04 18:04:18.854: E/AndroidRuntime(1801):     at android.os.Looper.loop(Looper.java:137)
12-04 18:04:18.854: E/AndroidRuntime(1801):     at android.app.ActivityThread.main(ActivityThread.java:4745)
12-04 18:04:18.854: E/AndroidRuntime(1801):     at java.lang.reflect.Method.invokeNative(Native Method)
12-04 18:04:18.854: E/AndroidRuntime(1801):     at java.lang.reflect.Method.invoke(Method.java:511)
12-04 18:04:18.854: E/AndroidRuntime(1801):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-04 18:04:18.854: E/AndroidRuntime(1801):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-04 18:04:18.854: E/AndroidRuntime(1801):     at dalvik.system.NativeStart.main(Native Method)
12-04 18:04:18.854: E/AndroidRuntime(1801): Caused by: java.lang.NoClassDefFoundError: com.example.mapdemo.BasicMapActivity
12-04 18:04:18.854: E/AndroidRuntime(1801):     at com.example.mapdemo.MainActivity.<clinit>(MainActivity.java:98)
12-04 18:04:18.854: E/AndroidRuntime(1801):     ... 15 more

回答1:

I had the same problem and got resolved by doing the following,

  • Remove the android-support-v4.jar that you added manually
  • Right click on the project and select Android Tools -> Add Support Library ...

After this everything worked for me.



回答2:

This worked for me.

Once you have imported the Android-Support-V4.jar and the Google_Play_Services_lib.jar, go to:

Project Properties > Java Build Path > Order and Export.

Then ensure you the check boxes beside Android-Support-V4.jar and the Google_Play_Services_lib.jar are selected.

Click ok, then run. All should be well.



回答3:

Following up on NewUserSOF's answer (and I am new to this):

I implemented Jairo's answer and the Google Maps V2 "MainActivity" ran and displayed the list of options.

But now when I click on any of them, my Emulator says I need to Update Google Play services. When I hit the "Update" button, the app crashes.

From LogCat:

12-19 16:02:44.902: W/GooglePlayServicesUtil(2431): Google Play services out of date. Requires 2012100 but found 1015

12-19 16:02:51.136: E/AndroidRuntime(2431): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=http://play.google.com/store/apps/details?id=com.google.android.gms flg=0x80000 pkg=com.android.vending }

Is there a way to Update the Google Play Services on the Emulator. (Emulator is Nexus S running Google API's 17).



回答4:

Multiple reference of android-support-v4.jar

This is worked for me.
If your project and goolge play services library project both contain android-support-v4.jar then it will conflict.
Remove android-support-v4.jar from any one project.



回答5:

Sounds like you are missing something important ;) Did you do these steps?

  • Select Project > Properties, select Java Build Path, and navigate to Libraries.
  • Select Add External Jars, include the following jar files, and click OK:

/extras/android/compatibility/v4/android-support-v4.jar

  • Add your own Google Maps Android API key.


回答6:

Is the Google Play Services library project referenced as a library project from your Sampleproject?



回答7:

Edit: Jairo's answer worked for me. Edit2: It runs but no map shown. I have the API key in place. It's a new one just got it. Used package name com.example.mapdemo. Edit3: Refreshed API key (they dont last long). Still no map shown. Edit4: Had wrong SHA1 from using wrong keystore. When I searched for it, search did not find it in .android folder (ubuntu not searching hidden folder), instead showed me keystores I got from downloading android source code.

I'm having the exact same error.

MainActivity starts, refers to BasicMapActivity which is in the same package. BasicMapActivity extends FragmentActivity which is in the android-support-v4.jar.

On this page: https://developers.google.com/maps/documentation/android/intro See Step 4: "Select Add External Jars, include the following jar files, and click OK: /extras/android/compatibility/v4/android-support-v4.jar"

My path was to the jar was /support/ instead of /compatibility/ - I don't have a /compatibility/ folder. Maybe that's a clue?

My android-support-v4.jar has a Modified date of Mon 19 Nov 2012 09:14:47 AM EST. Makes me wonder if I am missing the latest update. SDK Manager has been run and rerun.



回答8:

1) Verify that the debug keystore eclipse is using to sign your app is actually the one you have extracted the SHA1 hash to generate the api key

Go to:

Window > Android > Build > "Default debug keystore" and copy the path to the keystore

Run

keytool -list -v -keystore YOUR_KEYSTORE_PATH -alias androiddebugkey

2) Verify that you have entered the package of your application after the SHA1 hash in the API console configuration

Android apps: XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX;com.yourapp.package

3) I experienced this error at the beginning of my tests and i remember that an error was logged on Logcat. Something like "Authentication error" or "API key error" something like that. Just pay attention to the logcat on the first run of the app to check that you are not having issues with the API key. If you experience this error, you will have to kill/reinstall the app. Fixing the api key while the app is still running did not entirely solve the issue.

4) I had to add the support jar into the libs folder inside the example app. Linking it as an external jar in the eclipse buildpath didn't work for me.



回答9:

Depending on how your source is set up, you may need to set the android-support-library to be exported (right click->properties->java build path->order and export) Also, you need to be using SupportMapFragment rather than MapFragment for the support library. Make sure you're also using getSupportFragmentManager rather than getFragmentManager.



回答10:

For me the issue was Google Maps was not installed. I had wiped my tablet and forgot to reinstall it.