【问题标题】:IllegalStateException when using the Google Play Service Library使用 Google Play 服务库时出现 IllegalStateException
【发布时间】:2014-07-31 18:16:38
【问题描述】:

我在使用 Google Play 服务库时遇到了 IllegalStateException,但我不知道为什么。

我已将 Android SDK 中的 google-play-service-lib 项目复制到我的工作 Git 存储库中,并与我的项目一起导入到工作区中。

我在 Project Proprieties->Android 中签入,google-play-service-lib 已添加到库部分,我看到它附近有一个绿色复选标志。

AndroidManifest.xml 如下所示:

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

    <uses-sdk
        android:minSdkVersion="9"
        android:targetSdkVersion="19" />

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <meta-data android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version"
            android:resource="@xml/global_tracker"/>

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" 
            android:launchMode="singleTop">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
         </activity>

    </application>
</manifest>

在我的MainActivity.java 文件中的onCreate() 方法中,执行int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); 时会引发异常。

     try {
        int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);

        // Google Play services is available
        if (ConnectionResult.SUCCESS == resultCode) {
            LoggerUtils.d(LOG,"Google Play services is available, create Ad");

            // Create an ad.
            adView = new AdView(this);
            adView.setAdSize(AdSize.BANNER);
            adView.setAdUnitId(AD_UNIT_ID);

            // Add the AdView to the view hierarchy. The view will have no size
            // until the ad is loaded.
            FrameLayout layout = (FrameLayout) findViewById(R.id.FragmentContainer);
            layout.addView(adView);

            // Create an ad request. Check logcat output for the hashed device ID to
            // get test ads on a physical device.
            AdRequest adRequest = new AdRequest.Builder()
                .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
                .addTestDevice("INSERT_YOUR_HASHED_DEVICE_ID_HERE")
                .build();

            // Start loading the ad in the background.
            adView.loadAd(adRequest);
        } else {
            // Google Play services was not available, print reason
            LoggerUtils.d(LOG, "Google Play services is not available. " + String.valueOf(resultCode)); 
        }
        } catch (Exception e) {
            LoggerUtils.e(LOG, e.getMessage());
        }

我在 LogCat 中收到此消息,但我无法弄清楚问题所在

07-31 20:54:31.140: E/com.example.test.MainActivity.LOG(3269): The meta-data tag in your app's AndroidManifest.xml does not have the right value.  Expected 5077000 but found 2131034114.  You must have the following declaration within the <application> element:     <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />

我从google-play-service-lib 项目的res-&gt;values 文件夹中检查了version.xml 中的google_play_services_version,它包含预期的编号5077000。我只能在dump.txt 文件的proguard 文件夹中找到2131034114 的引用。这个文件是从我之前构建的用于发布的 apk 中创建的,其中 google_play_services_version 是项目的一部分,但只引入了 Google Analytics 而不是 AdMob。对于我所做的测试,我总是构建一个调试 apk,据我所知,这不是用 proguard 完成的。

我多次尝试从工作区清理项目,然后关闭并再次打开 Eclipse。 我什至卸载了 Google SDK、ADT 并再次安装它们并从工作区中删除了 google-play-service-lib 项目,删除它并在重新安装后从 Google SDK 中复制它。

我在运行 Android 4.3(Galaxy S3 上的三星股票 ROM)和 Android 4.4.4(带有 CyanogenMod 的三星 Galaxy S2)的手机上进行了测试,在两部手机上我都遇到了同样的错误。

【问题讨论】:

  • 会不会是为google-play-service-lib项目设置的构建版本导致了这种行为?

标签: java android eclipse git google-play-services


【解决方案1】:

Google Play Services 版本的值不正确,res/value/integers.xml 里面的值从:

&lt;integer name="google_play_services_version"&gt;2131034114&lt;/integer&gt;

到:

&lt;integer name="google_play_services_version"&gt;5077000&lt;/integer&gt;

您拥有的 Google Play 服务版本是 5077000 而不是 2131034114

然后Refresh -&gt; Clean -&gt; Build :)

【讨论】:

  • 5077000
  • 以上注释包含来自 google-play-service-lib->res->values 的 version.xml 内容
  • 刷新 -> 清理 -> 构建 :)
  • 我在项目的res/value/integers.xml 文件中没有为google_play_services_version 定义值
  • 刷新是指在工作区的每个项目上按F5?
【解决方案2】:

哇哦! android:resource="@xml/global_tracker"/ 导致了问题。

我不记得我发现哪个关于 Google Analytics 集成的教程说要添加这个标签,但是当我再次查看 Google Analytics 的 Overview 时,并没有提到它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-21
    • 1970-01-01
    • 2016-03-14
    • 2013-06-09
    • 2023-04-01
    • 2019-03-31
    • 2016-03-11
    相关资源
    最近更新 更多