无法实例化android.gms.maps.MapFragment

我尝试使用google maps android v2进行演示,使用非常简单的actitiviy,只需从google页面复制代码: https : //developers.google.com/maps/documentation/android/start#adding_the_api_key_to_your_application

活动:

package com.example.mapdemo; import android.app.Activity; import android.os.Bundle; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } } 

布局:

 <?xml version="1.0" encoding="utf-8"?> <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"/> 

我已经申请了一个API页面,并修改我的androidmanifest.xml文件,就像这样:

 <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.wenhai.driverschool" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" /> <uses-permission android:name="android.permission.INTERNET" /> <!-- add for map2 --> <permission android:name="com.example.mapdemo.permission.MAPS_RECEIVE" android:protectionLevel="signature" /> <uses-permission android:name="com.example.mapdemo.permission.MAPS_RECEIVE" /> <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" /> <!-- External storage for caching. --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <!-- Maps API needs OpenGL ES 2.0. --> <uses-feature android:glEsVersion="0x00020000" android:required="true" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="AIzaSyDVAF4WaVSVRDKJx87It8OSFP5txQcPabc" /> <activity android:name=".MainActivity" android:label="@string/title_activity_main" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> 

我也在eclipse中将我的应用引用到google-play-services_lib。

但是每次,logcat中的错误报告都是这样的:

 2-05 16:22:53.609: E/AndroidRuntime(21623): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.wenhai.driverschool/com.wenhai.driverschool.MainActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment 12-05 16:22:53.609: E/AndroidRuntime(21623): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967) 12-05 16:22:53.609: E/AndroidRuntime(21623): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992) 12-05 16:22:53.609: E/AndroidRuntime(21623): at android.app.ActivityThread.access$600(ActivityThread.java:127) 12-05 16:22:53.609: E/AndroidRuntime(21623): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158) 12-05 16:22:53.609: E/AndroidRuntime(21623): at android.os.Handler.dispatchMessage(Handler.java:99) 12-05 16:22:53.609: E/AndroidRuntime(21623): at android.os.Looper.loop(Looper.java:137) 12-05 16:22:53.609: E/AndroidRuntime(21623): at android.app.ActivityThread.main(ActivityThread.java:4441) 12-05 16:22:53.609: E/AndroidRuntime(21623): at java.lang.reflect.Method.invokeNative(Native Method) 12-05 16:22:53.609: E/AndroidRuntime(21623): at java.lang.reflect.Method.invoke(Method.java:511) 12-05 16:22:53.609: E/AndroidRuntime(21623): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823) 12-05 16:22:53.609: E/AndroidRuntime(21623): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590) 12-05 16:22:53.609: E/AndroidRuntime(21623): at dalvik.system.NativeStart.main(Native Method) 12-05 16:22:53.609: E/AndroidRuntime(21623): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment 12-05 16:22:53.609: E/AndroidRuntime(21623): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697) 12-05 16:22:53.609: E/AndroidRuntime(21623): at android.view.LayoutInflater.inflate(LayoutInflater.java:466) 12-05 16:22:53.609: E/AndroidRuntime(21623): at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 12-05 16:22:53.609: E/AndroidRuntime(21623): at android.view.LayoutInflater.inflate(LayoutInflater.java:352) 12-05 16:22:53.609: E/AndroidRuntime(21623): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:255) 12-05 16:22:53.609: E/AndroidRuntime(21623): at android.app.Activity.setContentView(Activity.java:1835) 12-05 16:22:53.609: E/AndroidRuntime(21623): at com.wenhai.driverschool.MainActivity.onCreate(MainActivity.java:11) 12-05 16:22:53.609: E/AndroidRuntime(21623): at android.app.Activity.performCreate(Activity.java:4465) 12-05 16:22:53.609: E/AndroidRuntime(21623): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 12-05 16:22:53.609: E/AndroidRuntime(21623): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931) 12-05 16:22:53.609: E/AndroidRuntime(21623): ... 11 more 12-05 16:22:53.609: E/AndroidRuntime(21623): 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 12-05 16:22:53.609: E/AndroidRuntime(21623): at android.app.Fragment.instantiate(Fragment.java:581) 12-05 16:22:53.609: E/AndroidRuntime(21623): at android.app.Fragment.instantiate(Fragment.java:549) 12-05 16:22:53.609: E/AndroidRuntime(21623): at android.app.Activity.onCreateView(Activity.java:4235) 12-05 16:22:53.609: E/AndroidRuntime(21623): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:673) 12-05 16:22:53.609: E/AndroidRuntime(21623): ... 20 more 12-05 16:22:53.609: E/AndroidRuntime(21623): Caused by: java.lang.ClassNotFoundException: com.google.android.gms.maps.MapFragment 12-05 16:22:53.609: E/AndroidRuntime(21623): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61) 12-05 16:22:53.609: E/AndroidRuntime(21623): at java.lang.ClassLoader.loadClass(ClassLoader.java:501) 12-05 16:22:53.609: E/AndroidRuntime(21623): at java.lang.ClassLoader.loadClass(ClassLoader.java:461) 12-05 16:22:53.609: E/AndroidRuntime(21623): at android.app.Fragment.instantiate(Fragment.java:571) 12-05 16:22:53.609: E/AndroidRuntime(21623): ... 23 more 

我不知道这个原因。

如果我将google-play-services.jar添加到我的项目中,则会报告另一个错误:

 12-05 16:34:23.269: E/AndroidRuntime(22638): FATAL EXCEPTION: main 12-05 16:34:23.269: E/AndroidRuntime(22638): java.lang.NoClassDefFoundError: com.google.android.gms.R$styleable 12-05 16:34:23.269: E/AndroidRuntime(22638): at com.google.android.gms.maps.GoogleMapOptions.createFromAttributes(Unknown Source) 12-05 16:34:23.269: E/AndroidRuntime(22638): at com.google.android.gms.maps.MapFragment.onInflate(Unknown Source) 12-05 16:34:23.269: E/AndroidRuntime(22638): at android.app.Activity.onCreateView(Activity.java:4242) 12-05 16:34:23.269: E/AndroidRuntime(22638): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:673) 12-05 16:34:23.269: E/AndroidRuntime(22638): at android.view.LayoutInflater.inflate(LayoutInflater.java:466) 12-05 16:34:23.269: E/AndroidRuntime(22638): at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 12-05 16:34:23.269: E/AndroidRuntime(22638): at android.view.LayoutInflater.inflate(LayoutInflater.java:352) 12-05 16:34:23.269: E/AndroidRuntime(22638): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:255) 12-05 16:34:23.269: E/AndroidRuntime(22638): at android.app.Activity.setContentView(Activity.java:1835) 12-05 16:34:23.269: E/AndroidRuntime(22638): at com.wenhai.driverschool.MainActivity.onCreate(MainActivity.java:11) 12-05 16:34:23.269: E/AndroidRuntime(22638): at android.app.Activity.performCreate(Activity.java:4465) 12-05 16:34:23.269: E/AndroidRuntime(22638): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 12-05 16:34:23.269: E/AndroidRuntime(22638): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931) 12-05 16:34:23.269: E/AndroidRuntime(22638): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992) 12-05 16:34:23.269: E/AndroidRuntime(22638): at android.app.ActivityThread.access$600(ActivityThread.java:127) 12-05 16:34:23.269: E/AndroidRuntime(22638): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158) 12-05 16:34:23.269: E/AndroidRuntime(22638): at android.os.Handler.dispatchMessage(Handler.java:99) 12-05 16:34:23.269: E/AndroidRuntime(22638): at android.os.Looper.loop(Looper.java:137) 12-05 16:34:23.269: E/AndroidRuntime(22638): at android.app.ActivityThread.main(ActivityThread.java:4441) 12-05 16:34:23.269: E/AndroidRuntime(22638): at java.lang.reflect.Method.invokeNative(Native Method) 12-05 16:34:23.269: E/AndroidRuntime(22638): at java.lang.reflect.Method.invoke(Method.java:511) 12-05 16:34:23.269: E/AndroidRuntime(22638): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823) 12-05 16:34:23.269: E/AndroidRuntime(22638): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590) 12-05 16:34:23.269: E/AndroidRuntime(22638): at dalvik.system.NativeStart.main(Native Method) 

任何人都可以帮助我呢?

非常感谢你。

在IntelliJ IDEA(IntelliJ 12更新)中:

  1. 创build一个包含class dummy {}的文件~/android-sdk/extras/google/google_play_services/libproject/google-play-services_lib/src/dummy.java
  2. File-> Import Module-> ~/android-sdk/extras/google/google_play_services/libproject/google-play-services_lib
  3. 从现有来源创build模块
  4. 下一步 – >下一步 – >下一步 – >下一步 – >完成
  5. 文件 – >项目结构 – >模块 – > YourApp
  6. + – > Module Dependency-> Google-play-services_lib( +button位于对话框的右上angular。)
  7. + – > Jars或目录 – > ~/android-sdk/extras/google/google_play_services/libproject/google-play-services_lib/libs/google-play-services.jar
  8. 使用向上 / 向下箭头将<Module source>移动到列表的底部。

如果你喜欢,你可以删除dummy.java

编辑:使用这一段时间后,我发现有一个小缺陷/错误。 IDEA有时会抱怨无法在google-play-services_lib目录中打开.iml项目文件,尽pipe事实上您从来没有告诉过它那里有一个项目。 如果发生这种情况,重build项目将解决问题,至less在问题恢复之前。

更新

请遵循Commonsware MapV2代码片断,以获得更好的理解。

(以综合版出现)

https://github.com/commonsguy/cw-omnibus/tree/master/MapsV2

以下代码片段工作正常,我select使用SupportMapFragment

不要忘了将 google-play-services.jar 添加 到您的项目中。

MainActivity.java

 package com.example.newmapview; 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(Bundle savedInstanceState) { super.onCreate(savedInstanceState); SupportMapFragment fragment = new SupportMapFragment(); getSupportFragmentManager().beginTransaction() .add(android.R.id.content, fragment).commit(); } } 

的manifest.xml

 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.newmapview" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="16" /> <permission android:name="com.example.newmapview.permission.MAPS_RECEIVE" android:protectionLevel="signature" /> <uses-permission android:name="com.example.newmapview.permission.MAPS_RECEIVE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.newmapview.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="XXXXX" /> </application> <uses-feature android:glEsVersion="0x00020000" android:required="true" /> </manifest> 

结果如下

在这里输入图像描述 希望这会有所帮助。

试着用下面的代码replace你的布局:

 <?xml version="1.0" encoding="utf-8"?> <fragment xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/map" android:name="com.google.android.gms.maps.SupportMapFragment" android:layout_width="wrap_content" android:layout_height="match_parent" /> 

您需要在11下使用SupportMapFragment for API!

斯坦

我面临同样的问题,ant花了我两天的时间才找出一个适合我的解决scheme:

  1. 删除项目google-play-services_lib (右键点击项目删除)
  2. 如果有的话,请删除包含Google地图演示(我的情况下为MainActivity )的项目
  3. 将项目google-play-services_lib(extras \ google \ google_play_services \ libproject \ google-play-services_lib)复制到工作区,然后将其导入为常规项目(File-> import-> existing projects into workspase)
  4. 右键点击你的项目(你要加载地图) – > Android – >添加(在库下)google-play-services_lib

你应该看到这样的东西:

注意

注意:你不应该有这样的事情(项目应该从你的工作区引用):

在这里输入图像描述

我认为问题是两个项目引用同一个库

尝试这个

http://developer.android.com/tools/projects/projects-eclipse.html#ReferencingLibraryProject

我刚刚添加了谷歌服务项目,并在我的项目属性 – > Android中添加了一个引用

  1. 现在,演示可以按照这个链接工作:
  2. 例如演示,它也可以工作:

在项目的libs目录中添加两个jar。 请按照以下信息。 特别是,我认为你需要:

  • 导入“google-play-services_lib”项目的实际源代码,并将其作为Android库链接。
    • 通过Project – > Properties – > Android – > Library,Add – > google-play-services_lib(你可以右键点击你的项目并selectProperties,然后selectAndroid)。
    • 不要通过项目的“Java Build Path”将其作为依赖项目添加,这对我不起作用。
  • 在示例项目的“libs”文件夹中添加google-play-services.jar和android-support-v4.jar,并在“Build Path – > Configure Build Path – > Libraries”中将它们添加为“External External JARs” 。

我发现这第二步是必要的,因为当我尝试使用示例代码时遇到与您完全相同的错误。 第一步是避免com.google.android.gms.R $ style中的NoClassDefFoundError。

在示例代码工作之前,我还需要执行Clean build并从设备上卸载应用程序(从较早的testing尝试)。

非常感谢你的帮助。

也许你必须强调这一点:

注意:Android模拟器不支持Google Play服务 – 要开发使用API​​,您需要提供开发设备,如Android手机或平板电脑。

http://developer.android.com/google/play-services/setup.html

您必须提供一个物理开发设备,您可以在其上运行和debugging您的应用程序。 不要使用模拟器。 不会工作。

在你的MainActivity (或者你想放置v2地图的每个活动中),你必须扩展FragmentActivity或者Activity来使用SupportMapFragment或者MapFragment 。 只有在您定位到API 12及更高版本时才使用MapFragment类。 否则,请使用SupportMapFragment 。 这是一个微妙的差异,但它使错误。

由于Google地图现在被Fragments包装,所以从FragmetActivity扩展您的活动不仅仅是Activity,并确保您使用“ supporrtMapFragment ”,那么您只从支持包导入Fragment。

在我的Intellij Idea项目中,我必须在项目中添加两个:google-play-services_lib和google-play-services.jar。 在google-play-services_lib项目中添加google-play-services.jar。 在那之后,如果你做了所有的事情,那么这里描述的是正确的,你的申请应该是有效的。

这是有点困难,因为它在以前的API不同,但我find了一个解决scheme。 Google说这里要做什么; 根据这个问题,我们需要com.google.android.gms类,所以有必要设置google play services ,这只是一个库,我们必须添加到我们的项目像这个链接 。 导入项目库google-play-services_lib的副本,而不是sdk文件夹中的副本非常重要。 一旦完成,来自Google的教程就完美了。

涉及到这个build议: https : //stackoverflow.com/a/20215481/3080835

我也不得不将这添加到清单中的Application元素:

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

它完美的工作。

在build.gradle中添加这个依赖项

compile 'com.google.android.gms:play-services:6.5.87 '

现在这个工作

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <fragment android:id="@+id/map" android:name="com.google.android.gms.maps.MapFragment" android:layout_width="match_parent" android:layout_height="388dp" android:layout_weight="0.40" /> </LinearLayout> 

要处理与云端硬盘相关的导入操作,您需要从Google Drive SDK获得几个jar子。 有几个大的,所以你可能需要单独添加它们以适应您的应用程序。

如果这样不能解决com.google.android.gms.* importsthe Google Play Services add-on needs to be installed (from Extras -> Google Play Services in the SDK Manager) and google-play-services.jar needs to be added to your build path.

编辑:

在这种情况下,需要以下jar子:

google-api-services-drive-v2-rev1-1.7.2-beta.jar

google-http-client-1.10.3-beta.jar

google-http-client-android2-1.10.3-beta.jar

google-oauth-client-1.10.1-beta.jar

google-api-client-android2-1.10.3-beta.jar

google-api-client-1.10.3-beta.jar

我的LG-E730在2.3.4 Android上遇到同样的问题。 在我更新手机上的Google Play服务之前,出现此错误。

不要忘了实际上build立谷歌播放services_lib! 这就是为什么它显示“无法find谷歌播放…. apk”。 对于我来说,在Eclipse上,不需要其他黑客,而是从Android子菜单引用项目,而不是从Java构buildpath或Project引用或任何其他引用。 没有手动放置的jar子,没有什么是我真正需要的。

在Eclipse中,有必要为Google Play服务创build一个单独的项目,并将其作为Android项目的库引用,而不是简单地将jar添加到它。 在我的电脑中,我直接从D:\ adt-bundle-windows-x86-20130219 \ sdk \ extras \ google \ google_play_services \ libproject \ google-play-services_lib中导入了Google play服务Eclipse项目,并将其设置为Lib项目,可以从我的Android项目中引用它。

这是在同一个教程上说的

请注意,下面的代码仅用于在针对Android API 12或更高版本的应用程序中testing您的设置

只需将您的最小SDK版本更改为12即可使用

 <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" /> 

避风港的尝试Aurel的解决scheme旧版本的API呢。

我认为这是值得一提的(我花了一个多小时拔出我的头发),如果你使用MapFragment你不能使用FragmentActivity(SupportMapFragment将在这个环境中运行良好)。 我正准备放弃。

第一步http://developer.android.com/tools/projects/projects-eclipse.html#ReferencingLibraryProject第二步http://developer.android.com/google/play-services/setup.html在该页面上有一个注意,但我们没有注意;

注意:您应该引用复制到源代码树的库的副本 – 不应该从Android SDK目录引用库。

但我认为这不是一个好的文件的方式:)

这可能对一些人有帮助。 我有两个项目,一个是来自Google的演示的副本,工作正常。 另一个我复制到一个现有的项目,我根本无法得到它运行。 而在第二个失败的我得到了上述的错误信息。

我的问题是由于库没有启用,即使我重build,导入多次等。右键单击项目 – >属性 – > Java构buildpath – >订单和导出选项卡。 在失败的项目“Android私人图书馆”选项卡未选中。

一旦我启用它的项目工作正常。

除了之前提到的许多事情之外,在我和“订单和出口”中标记.jar也很重要。 据我所知,通常不会提及,对我来说这是至关重要的。

在订单和导出中标记库

我拥有了上面所有人所说的一切,并通过简单地调用super.onCreate(savedInstanceState);解决错误super.onCreate(savedInstanceState); 作为创造方法的第一条指示; 在方法的最后一行之前。 :| 整天浪费了。

我有这个问题,我只是更新谷歌播放服务,并确保你添加的google-play-service-lib项目作为依赖项,它现在工作,没有任何代码更改,但我仍然“没有findGoogle Play服务资源。检查你的项目configuration,确保包含资源。“ 但是这只发生在你有setMyLocationEnabled(true)时,任何人都知道为什么?

我没有得到真正的解决scheme,但是我在做这些事情之后解决了它:
– 在导入google-play-services_lib时勾选“将项目复制到工作区”
– 不要将minSdkVersion设置为13以下
– 如果您遇到主题错误,请尝试将布局主题更改为任何系统主题
– 从头开始​​重新创build项目,或者如果从某个地方获取它,则还原所有项目

我得到了同样的问题,并从SDK安装的播放服务,所有的问题飞走了。

我在活动中写道
import com.google.android.gms.maps.SupportMapFragment;

和Eclipse给我红色图标>点击>修复项目设置..>添加档案谷歌播放服务.jar

而Finnaly它的工作!

将Google Play服务添加到您的项目

要使Google Play服务API可用于您的应用,请执行以下操作:

请按照以下链接中的步骤操作: http : //developer.android.com/google/play-services/setup.html#Setup

在Windows 7 64位机器上使用Android SDK for x86时遇到此问题。 我下载了Android SDK 64位版本,让Eclipse在Window> Preferences> Android> SDK location中看到它,问题就停止了。

请仔细阅读

如果一切都与谷歌代码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"/>