2013-04-09 31 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.)。 我希望這對你有所幫助。

相關問題