错误java.lang.ClassNotFoundException:Google Map V2中的com.google.android.gms.maps.MapFragment

我正在使用Google Map V2 API作为地图。 我已经在libs文件夹中复制了google-play-services.jar并设置了eclipse的构buildpath。

我收到exception,因为我添加了logcat。

请帮忙解决这个问题。

home_map_view.xml

<fragment android:id="@+id/map" android:layout_width="fill_parent" android:layout_height="fill_parent" android:name="com.google.android.gms.maps.MapFragment" android:layout_marginBottom="60dp"/> 

AndroidManifest.xml中

 <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.app" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <permission android:name="com.example.app.permission.MAPS_RECEIVE" android:protectionLevel="signature"/> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.CALL_PHONE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="com.example.app.permission.MAPS_RECEIVE"/> <uses-feature android:glEsVersion="0x00020000" android:required="true"/> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name"> <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="KEY"/> <activity android:name=".MainActivity" android:label="@string/app_name" android:clearTaskOnLaunch="true" android:configChanges="orientation" android:screenOrientation="portrait"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".HomeMapView" android:label="@string/title_home" android:configChanges="orientation" android:launchMode="singleTop" /> </application> </manifest> 

HomeMapView.java

 public class HomeMapView extends FragmentActivity implements OnTabChangeListener { private GoogleMap mapView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.home_map_view); // Getting reference to SupportMapFragment of the activity_main SupportMapFragment fragment = new SupportMapFragment(); getSupportFragmentManager().beginTransaction() .add(R.id.map, fragment).commit(); // Getting Map for the SupportMapFragment mapView = fragment.getMap(); mapView.setMyLocationEnabled(true); } } 

logcat的:

 05-15 23:17:52.843: E/AndroidRuntime(19782): FATAL EXCEPTION: main 05-15 23:17:52.843: E/AndroidRuntime(19782): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app/com.example.app.HomeMapView}: android.view.InflateException: Binary XML file line #13: Error inflating class fragment 05-15 23:17:52.843: E/AndroidRuntime(19782): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651) 05-15 23:17:52.843: E/AndroidRuntime(19782): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667) 05-15 23:17:52.843: E/AndroidRuntime(19782): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 05-15 23:17:52.843: E/AndroidRuntime(19782): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935) 05-15 23:17:52.843: E/AndroidRuntime(19782): at android.os.Handler.dispatchMessage(Handler.java:99) 05-15 23:17:52.843: E/AndroidRuntime(19782): at android.os.Looper.loop(Looper.java:130) 05-15 23:17:52.843: E/AndroidRuntime(19782): at android.app.ActivityThread.main(ActivityThread.java:3687) 05-15 23:17:52.843: E/AndroidRuntime(19782): at java.lang.reflect.Method.invokeNative(Native Method) 05-15 23:17:52.843: E/AndroidRuntime(19782): at java.lang.reflect.Method.invoke(Method.java:507) 05-15 23:17:52.843: E/AndroidRuntime(19782): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 05-15 23:17:52.843: E/AndroidRuntime(19782): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625) 05-15 23:17:52.843: E/AndroidRuntime(19782): at dalvik.system.NativeStart.main(Native Method) 05-15 23:17:52.843: E/AndroidRuntime(19782): Caused by: android.view.InflateException: Binary XML file line #13: Error inflating class fragment 05-15 23:17:52.843: E/AndroidRuntime(19782): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:587) 05-15 23:17:52.843: E/AndroidRuntime(19782): at android.view.LayoutInflater.rInflate(LayoutInflater.java:623) 05-15 23:17:52.843: E/AndroidRuntime(19782): at android.view.LayoutInflater.inflate(LayoutInflater.java:408) 05-15 23:17:52.843: E/AndroidRuntime(19782): at android.view.LayoutInflater.inflate(LayoutInflater.java:320) 05-15 23:17:52.843: E/AndroidRuntime(19782): at android.view.LayoutInflater.inflate(LayoutInflater.java:276) 05-15 23:17:52.843: E/AndroidRuntime(19782): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:216) 05-15 23:17:52.843: E/AndroidRuntime(19782): at android.app.Activity.setContentView(Activity.java:1660) 05-15 23:17:52.843: E/AndroidRuntime(19782): at com.example.app.HomeMapView.onCreate(HomeMapView.java:61) 05-15 23:17:52.843: E/AndroidRuntime(19782): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 05-15 23:17:52.843: E/AndroidRuntime(19782): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615) 05-15 23:17:52.843: E/AndroidRuntime(19782): ... 11 more 05-15 23:17:52.843: E/AndroidRuntime(19782): Caused by: android.support.v4.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 05-15 23:17:52.843: E/AndroidRuntime(19782): at android.support.v4.app.Fragment.instantiate(Fragment.java:395) 05-15 23:17:52.843: E/AndroidRuntime(19782): at android.support.v4.app.Fragment.instantiate(Fragment.java:363) 05-15 23:17:52.843: E/AndroidRuntime(19782): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:264) 05-15 23:17:52.843: E/AndroidRuntime(19782): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563) 05-15 23:17:52.843: E/AndroidRuntime(19782): ... 20 more 05-15 23:17:52.843: E/AndroidRuntime(19782): Caused by: java.lang.ClassNotFoundException: com.google.android.gms.maps.MapFragment in loader dalvik.system.PathClassLoader[/system/framework/com.google.android.maps.jar:/data/app/com.example.app-2.apk] 05-15 23:17:52.843: E/AndroidRuntime(19782): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240) 05-15 23:17:52.843: E/AndroidRuntime(19782): at java.lang.ClassLoader.loadClass(ClassLoader.java:551) 05-15 23:17:52.843: E/AndroidRuntime(19782): at java.lang.ClassLoader.loadClass(ClassLoader.java:511) 05-15 23:17:52.843: E/AndroidRuntime(19782): at android.support.v4.app.Fragment.instantiate(Fragment.java:385) 05-15 23:17:52.843: E/AndroidRuntime(19782): ... 23 more 

你的问题是你添加google-play-services到你的项目的方式:

我已经在libs文件夹中复制了google-play-services.jar并设置了eclipse的构buildpath。

这是错误的! 阅读我撰写的这篇博文的前三个步骤,以了解如何正确执行此操作:

Google Maps API V2

简而言之,您应该将google-play-services作为工作区中的项目导入。 然后从您的项目中引用它。

这应该是结果:

在这里输入图像描述

使用支持地图片段代替地图片段。 确保你已经添加了支持库

 <fragment android:id="@+id/map" android:layout_width="fill_parent" android:layout_height="fill_parent" android:name="com.google.android.gms.maps.SupportMapFragment " android:layout_marginBottom="60dp"/> 

其次,你不应该将jar文件复制到你的构buildpath。

您应该在地图项目中引用google play服务库项目。

将您的图书馆项目导入您的工作区。 导入相同的日食。

右键点击你的项目。 转到属性。 selectandroid。 点击android。 浏览并添加库项目。

在这里输入图像描述

如果一切都与谷歌代码woking相同,那么请检查清单文件在我的情况下我添加了地理键和映射键,这就是为什么发生exception,

注 – 不要在清单文件中添加两个键删除映射键

 meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="@string/google_maps_key"/> 

添加此代码。

  <meta-data android:name="com.google.android.geo.API_KEY" android:value="@string/auto_location"/> <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version"/>