2013-04-09 28 views
13

我试着在android中显示google maps v2的演示。

的Java代码,

package com.example.gpslocator; 

import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 

public class MainActivity extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
} 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
}  
} 

XML代码是,

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
> 

<fragment 
     android:id="@+id/map" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:name="com.google.android.gms.maps.MapFragment"/> 
</LinearLayout> 

我加入的manifest.xml API密钥,

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.gpslocator" 
android:versionCode="1" 
android:versionName="1.0" > 

<uses-sdk 
    android:minSdkVersion="3" 
    android:targetSdkVersion="17" /> 

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" /> 
<!-- 
The following two permissions are not required to use 
Google Maps Android API v2, but are recommended. 
--> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 

<permission 
    android:name="com.example.mapdemo.permission.MAPS_RECEIVE" 
    android:protectionLevel="signature" /> 

<uses-permission android:name="com.example.mapdemo.permission.MAPS_RECEIVE" /> 

<uses-feature 
    android:glEsVersion="0x00020000" 
    android:required="true" /> 

<application 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name="com.example.gpslocator.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="MY_API_KEY" /> 
</application> 

</manifest> 

当我试图运行我应用程序突然关闭。当我调试时,它显示错误“java.lang.NoClassDefFoundError:com.google.android.gms.R $ styleable”。 我将google-play-services.jar文件添加到了我的应用程序中。

的logcat的是,

04-09 05:33:53.677: E/Trace(1053): error opening trace file: No such file or directory (2) 
04-09 05:33:53.807: W/ActivityThread(1053): Application com.example.gpslocator is waiting for the debugger on port 8100... 
04-09 05:33:53.878: I/System.out(1053): Sending WAIT chunk 
04-09 05:33:54.207: I/dalvikvm(1053): Debugger is active 
04-09 05:33:54.288: I/System.out(1053): Debugger has connected 
04-09 05:33:54.288: I/System.out(1053): waiting for debugger to settle... 
04-09 05:33:54.487: I/System.out(1053): waiting for debugger to settle... 
04-09 05:33:54.697: I/System.out(1053): waiting for debugger to settle... 
04-09 05:33:54.897: I/System.out(1053): waiting for debugger to settle... 
04-09 05:33:55.097: I/System.out(1053): waiting for debugger to settle... 
04-09 05:33:55.297: I/System.out(1053): waiting for debugger to settle... 
04-09 05:33:55.498: I/System.out(1053): waiting for debugger to settle... 
04-09 05:33:55.708: I/System.out(1053): waiting for debugger to settle... 
04-09 05:33:55.907: I/System.out(1053): waiting for debugger to settle... 
04-09 05:33:56.167: I/System.out(1053): waiting for debugger to settle... 
04-09 05:33:56.367: I/System.out(1053): debugger has settled (1344) 
04-09 05:34:06.097: W/dalvikvm(1053): VFY: unable to resolve static field 867 (MapAttrs) in Lcom/google/android/gms/R$styleable; 
04-09 05:34:06.097: D/dalvikvm(1053): VFY: replacing opcode 0x62 at 0x000e 
04-09 05:40:21.278: D/AndroidRuntime(1053): Shutting down VM 
04-09 05:40:21.278: W/dalvikvm(1053): threadid=1: thread exiting with uncaught exception (group=0x40a71930) 
04-09 05:40:21.398: E/AndroidRuntime(1053): FATAL EXCEPTION: main 
04-09 05:40:21.398: E/AndroidRuntime(1053): java.lang.NoClassDefFoundError: com.google.android.gms.R$styleable 
04-09 05:40:21.398: E/AndroidRuntime(1053):  at com.google.android.gms.maps.GoogleMapOptions.createFromAttributes(Unknown Source) 
04-09 05:40:21.398: E/AndroidRuntime(1053):  at com.google.android.gms.maps.MapFragment.onInflate(Unknown Source) 
04-09 05:40:21.398: E/AndroidRuntime(1053):  at android.app.Activity.onCreateView(Activity.java:4716) 
04-09 05:40:21.398: E/AndroidRuntime(1053):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680) 
04-09 05:40:21.398: E/AndroidRuntime(1053):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:746) 
04-09 05:40:21.398: E/AndroidRuntime(1053):  at android.view.LayoutInflater.inflate(LayoutInflater.java:489) 
04-09 05:40:21.398: E/AndroidRuntime(1053):  at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 
04-09 05:40:21.398: E/AndroidRuntime(1053):  at android.view.LayoutInflater.inflate(LayoutInflater.java:352) 
04-09 05:40:21.398: E/AndroidRuntime(1053):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270) 
04-09 05:40:21.398: E/AndroidRuntime(1053):  at android.app.Activity.setContentView(Activity.java:1881) 
04-09 05:40:21.398: E/AndroidRuntime(1053):  at com.example.gpslocator.MainActivity.onCreate(MainActivity.java:12) 
04-09 05:40:21.398: E/AndroidRuntime(1053):  at android.app.Activity.performCreate(Activity.java:5104) 
04-09 05:40:21.398: E/AndroidRuntime(1053):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 
04-09 05:40:21.398: E/AndroidRuntime(1053):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 
04-09 05:40:21.398: E/AndroidRuntime(1053):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
04-09 05:40:21.398: E/AndroidRuntime(1053):  at android.app.ActivityThread.access$600(ActivityThread.java:141) 
04-09 05:40:21.398: E/AndroidRuntime(1053):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
04-09 05:40:21.398: E/AndroidRuntime(1053):  at android.os.Handler.dispatchMessage(Handler.java:99) 
04-09 05:40:21.398: E/AndroidRuntime(1053):  at android.os.Looper.loop(Looper.java:137) 
04-09 05:40:21.398: E/AndroidRuntime(1053):  at android.app.ActivityThread.main(ActivityThread.java:5041) 
04-09 05:40:21.398: E/AndroidRuntime(1053):  at java.lang.reflect.Method.invokeNative(Native Method) 
04-09 05:40:21.398: E/AndroidRuntime(1053):  at java.lang.reflect.Method.invoke(Method.java:511) 
04-09 05:40:21.398: E/AndroidRuntime(1053):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
04-09 05:40:21.398: E/AndroidRuntime(1053):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
04-09 05:40:21.398: E/AndroidRuntime(1053):  at dalvik.system.NativeStart.main(Native Method) 
04-09 05:40:26.487: I/Process(1053): Sending signal. PID: 1053 SIG: 9 

在属性 - > Android的 - >

enter image description here

请任何一个帮助我。我是否缺少任何参考?

回答

29

在这里看到了答案:
Google Maps Android API v2 - Sample Code crashes
虽然问题列出了不同的异常,回答特别提到您的具体问题。
选择File > Import > Android > Existing Android Code Into Workspace,然后单击下一步:

具体来说,导入google-play-services_lib项目是很重要的。
选择浏览...,输入[android-sdk-folder]/extras/google/google_play_services/libproject/google-play-services_lib,然后单击完成。
(见https://developers.google.com/maps/documentation/android/intro下的“样本代码”)
然后按照从链接的答案说明:

  • 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.
+0

试过了。这并没有解决问题。我添加了所有的参考资料,但是,无法获得解决方案.... – Mahe 2013-04-09 05:59:24

+0

我会尝试查看是否可以重现您的错误。 – Alexander 2013-04-09 06:20:51

+0

不,不能让它失败的方式。你说你添加了'google-play-services.jar' - 你有没有试过做“项目属性 - > android->库”,然后添加'google-play-services_lib'项目? – Alexander 2013-04-09 06:50:20

6

检查AndroidManifest.xml您的谷歌,播放services_lib项目的文件。应该是:

package="com.google.android.gms" 
+1

我认为如果您导入Google Play服务库,情况总是如此? – 2013-05-01 18:19:57

+0

这部作品适合我 – Siddharth 2015-08-10 17:14:46

0

的文件路径google-play-services_lib项目必须只有[A-Z,a-z]ÄÖÜ。我已将它移到D:\google-play-services_lib\比导入它新,它的工作原理!

对不起,我的英语;-)

1

当你输入谷歌播放-services_lib,检查 “项目复制到工作区”。这个对我有用!

1

我有同样的问题,我可以通过右键单击google-play-services_lib项目并刷新来解决它。出于某种原因,该项目没有与导致错误的Eclipse同步。

+0

也适合我 – Wayneio 2014-10-18 14:33:46

0

同时导入google-play-services_lib &演示项目到工作区; cope android-support-v4。演示项目下的jar文件夹到“libs”文件夹;然后:项目属性 - > android->库”,然后加入谷歌,播放services_lib

0

看看约翰Oleynik的步骤:a。www.stackoverflow.com/a/15709686/20146 为我工作

0

当您将项目google-play-services_lib导入到您的IDE时,不要忘记检查它的属性,google-play-services_lib应该使用与您的项目相同版本的Android SDK来构建。你的项目和谷歌播放服务都是使用Android 4.4.2构建的,之后你应该看到这个库已经正确构建了。

最后,你应该添加下面的元标记到AndroidManif est.xml里面的<application> ... </application>标签:

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

我有同样的问题。

但我没有什么是我用com.google.android.gms版本代码这个3265130

而不是使用com.google.android.gms版本代码这个4452000,这是最新的一个,我不不知道这个问题是什么,但它的工作对我来说。

[关注亚历山大给出的步骤]

可能是工作做好与新的更新。

enter image description here

可能是工作做好与新的更新。

enter image description here

0

我曾与地图诸多问题。但最终成功,建立! 我不使用日食,只是命令行。 首先,我强调我只是从google-play-services_lib/libs复制到myProject/libs /文件并执行ant调试...在Android模拟器上安装后,我收到错误java.lang.NoClassDefFoundError:com.google.android。 gms.R $设置样式

一天后AFER小时serching解决方案:

1)首先,我复制目录SDK /演员/谷歌/.../的Google Play-services_lib我myProject的目录。

2)做一个android update project --target <your target> -p myProject/google-play-services_lib如用户dzeikei说,(检查目标EXEC:android list target

3)编辑文件 “project.properties” 文件夹中myProject的,并在最后android.library.reference加入这一行.1 = google-play-services_lib或执行android update project --target android-19 --path myProject --library myProject/google-play-services_lib

4)ant cleanant debug ....但有一些错误,例如返回null等(使头痛)。但之后,蚂蚁在文件夹中创建bin和gen文件夹myProject/google-play-services_lib

5)ater寻找许多解决方案的年龄,以避免蚂蚁调试返回错误为什么我最终断开,只是从我的项目复制目录bin和gen/google-play-services_lib/to myProject/bin and myProject/gen

6)然后删除我之前在第3点添加的行。上面#android.library。参考.1 = google-play-services_lib,将myProject/google-play-services_lib/libs/google-play-services.jar复制到myProject/libs/google-play-services.jar

7)执行ant debug

adb install -r bin/myProject-debug.apk 

和finaly看到谷歌地图在我的Android模拟器:)

当然

其他像塞入API_KEY或添加和permisions从谷歌地图教程projectApplication我做之前,我编译项目。 查看没有promt的地图例如。 “安装Goodle Play服务”使用模拟器与谷歌服务已经安装。我使用AVD Manager列表中的Google API(Google Inc.)。 我希望这对你有所帮助。

相关问题