簡體   English   中英

java.lang.NoClassDefFoundError:com.google.android.gms.R $在Android中可設置樣式

[英]java.lang.NoClassDefFoundError: com.google.android.gms.R$styleable in android

我嘗試了在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->

在此處輸入圖片說明

請任何人幫我。 我會缺少參考嗎?

在這里查看答案:
Google Maps Android API v2-示例代碼崩潰
盡管問題列出了其他例外情況,但答案明確提到了您的確切問題。

具體來說,將google-play-services_lib導入為項目很重要:
選擇File > Import > Android > Existing Android Code Into Workspace ,然后單擊下一步。
選擇瀏覽...,輸入[android-sdk-folder]/extras/google/google_play_services/libproject/google-play-services_lib ,然后單擊完成。
(請參閱“示例代碼”下的https://developers.google.com/maps/documentation/android/intro
然后按照鏈接的答案中的說明進行操作:

  • 導入“ google-play-services_lib”項目的實際來源,並將其鏈接為> Android庫。
    • 通過項目->屬性-> Android->庫,添加-> google-play-services_lib(您可以右鍵單擊您的項目並選擇屬性,然后選擇Android)來執行此操作。
    • 不要通過“Java構建路徑”為您的項目,這並沒有為我工作將其添加為相關項目。

檢查您的google-play-services_lib項目的AndroidManifest.xml文件。 應該:

package="com.google.android.gms"

導入google-play-services_lib時 ,選中“將項目復制到工作區”。 這個對我有用!

我遇到了同樣的問題,我可以通過右鍵單擊google-play-services_lib項目並刷新來解決此問題。 由於某種原因,項目未與Eclipse同步,這導致了錯誤。

我有同樣的問題。

但是我所做的是我使用了com.google.android.gms版本代碼3265130

我不使用最新的4452000這個版本的com.google.android.gms版本代碼,我不知道問題出在哪里,但是它對我有用。

[按照亞歷山大給出的步驟]

可能與新的Updates兼容。

在此處輸入圖片說明

可能與新的Updates兼容。

在此處輸入圖片說明

google-play-services_lib項目的文件路徑必須只有[AZ,az]而不是ÄÖÜ 我已將其移至D:\\google-play-services_lib\\不是將其新導入,並且可以使用!

對不起我的英語不好 ;-)

將google-play-services_lib和演示項目都導入到工作區中; 應對android-support-v4.jar到演示項目下的“ libs”文件夾; 然后:項目屬性-> android-> library”,然后添加google-play-services_lib

查看約翰·奧萊尼克(John Oleynik)的步驟:www.stackoverflow.com/a/15709686/20146。 那對我有用。

將項目google-play-services_lib導入IDE后,別忘了檢查其屬性。 google-play-services_lib應使用與項目所用版本相同的Android SDK構建。 例如,您的項目和google-play-services都是使用Android 4.4.2構建的。 之后,您應該看到該庫已正確構建。

最后,您應該在<application> ... </application>標記內的AndroidManifest.xml中添加以下元標記:

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

該地圖存在很多問題。 但最終成功,建立起來! 我不使用eclipse,僅使用命令行 首先,我認為我只是從google-play-services_lib / libs復制到myProject / libs /文件並執行ant debug ...在android仿真器上安裝后,出現錯誤java.lang.NoClassDefFoundError:com.google.android.gms。 R $樣式

一天后,經過幾個小時的解決方案:

1)首先,我將目錄sdk / extras / google /.../ google-play-services_lib復制到我的myProject目錄中。

2)做一個android update project --target <your target> -p myProject/google-play-services_lib就像用戶dzeikei所說的那樣(檢查目標exec: android list target

3)在文件夾myProject中編輯文件“ project.properties”,並在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 ....但有一些錯誤,例如返回空等(使頭痛)。 但是在那之后,ant在文件夾myProject / google-play-services_lib中創建bin和gen文件夾

5)在尋找許多解決方案很久以前就找出了為什么螞蟻調試返回的錯誤我最終做了總結,只是將目錄bin和gen從myProject / google-play-services_lib /復制到myProject / bin和myProject / gen

6)然后刪除我之前在#android.library.reference.1 = google-play-services_lib上方的第3點添加的行,並將文件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

最后在Android模擬器上的projectApplication中看到了Google Map :)

當然,我在編譯項目之前就完成了其他工作,例如API_KEY或Google Maps教程中的添加和遷移。 要查看沒有提示的地圖,例如 “ Instal Goodle Play服務”與已經安裝的Google服務一起使用模擬器。 我從AVD Manager列表中使用了Google API(Google Inc.)。 希望對您有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM