我试图让Google Maps API v2为项目工作。我已通过Android SDK Manager下载了最新版本的Google Play服务,并将Google Play服务作为库项目导入Eclipse。
我还通过Project-> Properties-> Android-> Library为我的应用添加了对库项目的引用。
我测试Google Maps API的项目只有一个MainActivity和相关的布局文件。
MainActivity.java
package com.example.googlemapstest;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import com.google.android.gms.maps.SupportMapFragment;
public class MainActivity extends FragmentActivity
{
@Override
protected void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState != null)
{
return;
}
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, new SupportMapFragment()).addToBackStack(null).commit();
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.googlemapstest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<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.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="my key" />
</application>
</manifest>
LogCat stackTrace
08-27 14:26:22.138: E/dalvikvm(3408): Could not find class 'com.google.android.gms.maps.SupportMapFragment', referenced from method com.example.googlemapstest.MainActivity.onCreate
08-27 14:26:22.138: W/dalvikvm(3408): VFY: unable to resolve new-instance 922 (Lcom/google/android/gms/maps/SupportMapFragment;) in Lcom/example/googlemapstest/MainActivity;
08-27 14:26:22.428: W/dalvikvm(3408): threadid=1: thread exiting with uncaught exception (group=0x41332a08)
08-27 14:26:22.433: E/AndroidRuntime(3408): FATAL EXCEPTION: main
08-27 14:26:22.433: E/AndroidRuntime(3408): java.lang.NoClassDefFoundError: com.google.android.gms.maps.SupportMapFragment
08-27 14:26:22.433: E/AndroidRuntime(3408): at com.example.googlemapstest.MainActivity.onCreate(MainActivity.java:22)
08-27 14:26:22.433: E/AndroidRuntime(3408): at android.app.Activity.performCreate(Activity.java:5165)
08-27 14:26:22.433: E/AndroidRuntime(3408): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1103)
08-27 14:26:22.433: E/AndroidRuntime(3408): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2419)
08-27 14:26:22.433: E/AndroidRuntime(3408): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2520)
08-27 14:26:22.433: E/AndroidRuntime(3408): at android.app.ActivityThread.access$600(ActivityThread.java:162)
08-27 14:26:22.433: E/AndroidRuntime(3408): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1366)
08-27 14:26:22.433: E/AndroidRuntime(3408): at android.os.Handler.dispatchMessage(Handler.java:99)
08-27 14:26:22.433: E/AndroidRuntime(3408): at android.os.Looper.loop(Looper.java:158)
08-27 14:26:22.433: E/AndroidRuntime(3408): at android.app.ActivityThread.main(ActivityThread.java:5751)
08-27 14:26:22.433: E/AndroidRuntime(3408): at java.lang.reflect.Method.invokeNative(Native Method)
08-27 14:26:22.433: E/AndroidRuntime(3408): at java.lang.reflect.Method.invoke(Method.java:511)
08-27 14:26:22.433: E/AndroidRuntime(3408): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1083)
08-27 14:26:22.433: E/AndroidRuntime(3408): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:850)
08-27 14:26:22.433: E/AndroidRuntime(3408): at dalvik.system.NativeStart.main(Native Method)
我已经重新安装了这个库。 ADT和SDK都有最新版本。我也查看过具有相同问题的现有主题,但没有一个回答有效(例如Error java.lang.ClassNotFoundException: com.google.android.gms.maps.MapFragment in Google Map V2)
的问候,
BrickTop