46

遵循官方教程后尝试使用 Google Play Services V2 库时出现以下异常。

java.lang.NoClassDefFoundError: com.google.android.gms.R$string
at com.google.android.gms.common.GooglePlayServicesUtil.b(Unknown Source)
at com.google.android.gms.common.GooglePlayServicesUtil.a(Unknown Source)
at com.google.android.gms.common.GooglePlayServicesUtil.getErrorDialog(Unknown Source)

我哪里做错了?

4

8 回答 8

34

我也遇到过同样的问题,我正确地按照步骤操作并能够解决这个问题

首先(从 Google Developer 控制台设置您的项目) 转到API 控制台 - Google 代码

创建一个项目 如图所示 在此处输入图像描述

单击创建,然后您将要求添加项目名称,如图所示

在此处输入图像描述

创建项目后,是时候选择我们需要使用的服务了,在这种情况下,我们需要 android v2 地图,因此从 Service 中选择Google Maps Android API v2如图所示,在此处输入图像描述

现在转到 Api Access 并创建您的 OAuth 2.0 。通过在相应字段中提供您的包名称和 SHA1 指纹。 在此处输入图像描述

完成 OAuth 2.0 后,我们就可以使用您的 API 密钥了 在此处输入图像描述

现在创建一个与创建 OAuth 2.0 时使用的包名称相同的 Android 项目。并检查您是否在 Android SDK Manager 中 有google play 服务,否则请安装 google play 服务。在此处输入图像描述

安装 Google playservice 后,您会在您的 Android YourSdkpath\extras\google\google_play_services中找到一个 Google play 库。将该项目导入您的工作区,并将其作为您项目的参考库 在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

之后将相应的 java 和 xml 文件放入您的项目中。

MainActivity.java

package yourpackage;//Package name used while creating the Api key


import com.google.android.gms.common.ConnectionResult;
 import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;

import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;

public class MainActivity extends FragmentActivity {
GoogleMap googleMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Getting status
    int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());

    // Showing status
    if(status==ConnectionResult.SUCCESS)
    {
        SupportMapFragment supportMapFragment = (SupportMapFragment) 
                getSupportFragmentManager().findFragmentById(R.id.map);

        // Getting a reference to the map
        googleMap = supportMapFragment.getMap();
    }
    else{

        int requestCode = 10;
        Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode);
        dialog.show();
    }
}

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

}

activity_main.xml

<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" />

AndroidManifest.xml

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

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="16" />
   <permission
    android:name="yourpackage.permission.MAPS_RECEIVE"
    android:protectionLevel="signature"/>

<uses-permission android:name="yourpackage.permission.MAPS_RECEIVE"/>

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="yourpackage.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="YourAPIkey"/>

希望对你有帮助

于 2013-02-04T12:30:40.730 回答
32

版本 2 包括资源,必须作为库项目导入。请参阅http://developer.android.com/google/play-services/setup.html上的说明,了解如何在各种 IDE 中执行此操作。

这些资源用于 GooglePlayServicesUtil.getErrorDialog(),它指导用户安装、更新、启用等 Google Play 服务(如果设备上不可用)。

于 2012-12-03T20:29:28.417 回答
11

对于 Android 工作室:

1) 打开模块设置:

2) 从 SDK 添加(导入)模块 google_play_service_lib

图片

3) 模块 google_play_service_lib 检查为库

4) 从 SDK 添加库 google_play_service.jar

5) 完成

图片

于 2013-06-17T18:02:44.690 回答
2

如果您已经升级 SDK 并收到此类错误,请记住:

  • 如果您在 Eclipse 项目中复制了此文件,请使用新版本的 google-play-services.jar
  • 在project.properties中设置 target=android-19
  • 对于较新的 API(我使用 API 19),您可能需要添加以下代码

AndroidManifest.xml

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

我还使用ourSdkpath\extras\google\google_play_services作为如上所述导入的 Eclipse 库。

于 2013-11-09T19:19:39.227 回答
1

我通过在导入 google play 服务库时选中“将项目复制到工作区”解决了这个错误。
更多信息在这里:http: //developer.android.com/google/play-services/setup.html

于 2013-04-19T17:57:35.243 回答
1

如果你正在使用

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

将其更改为单个的,例如

compile 'com.google.android.gms:play-services-location:9.0.2'

compile 'com.android.support:multidex:1.0.1'如果你有也加 multiDexEnabled=true

于 2016-07-26T16:40:35.407 回答
0

在没有 Gradle 的情况下构建项目时,我对 Android Studio 也有同样的问题。GooglePlayService 项目中 AndroidManifest.xml 中的包名可能有问题。它应该是package="com.google.android.gms"

当我将库项目添加为"New Module"而不是"Import Module"时出现问题。

在此处输入图像描述

当您选择 New Module 时,Android Studio 会显示对话框来指定“Module name”和“Package name”,默认情况下“Package name”看起来像“com.example.MODULE_NAME_YOU_SPECIFED”,这是错误的。很容易错过它,因为项目构建成功。

在此处输入图像描述

当您通过“导入模块”添加库项目时,一切都应该没问题,因为 Android Studio 只是添加项目而不对库项目的源代码进行任何更改。

于 2014-05-12T08:07:48.077 回答
0

我也面临同样的问题。如果您在模拟器中运行您的应用程序,则可能会遇到此错误,因为在大多数模拟器中没有安装 google playService(Play 商店应用程序)。

因此,尝试在安装了 playService 应用程序的 android 设备中运行您的应用程序。

解决方案: - 添加错误部分代码try...catch,然后尝试它也可以在模拟器中工作。

注意:- 谷歌消息服务需要 palyservice。否则抛出错误SERVICE_NOT_AVAILABLE

于 2015-04-24T12:46:09.293 回答