使用Google Play服务库时出现IllegalStateException

时间:2014-07-31 18:16:39

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

我在使用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>

onCreate()文件的MainActivity.java方法中,执行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_services_version项目的version.xml文件夹中检查了res->values中的google-play-service-lib,其中包含预期的数字5077000。我只能在2131034114文件的proguard文件夹中找到dump.txt的引用。此文件是从我之前构建的apk for release中创建的,其中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)的手机上进行了测试,在两款手机上我都遇到了同样的错误。

2 个答案:

答案 0 :(得分:0)

Google Play服务版本的值不正确,在res/value/integers.xml内,更改值:

<integer name="google_play_services_version">2131034114</integer>

到:

<integer name="google_play_services_version">5077000</integer>

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

然后Refresh -> Clean -> Build:)

答案 1 :(得分:0)

Wohoooo! android:resource="@xml/global_tracker"/导致了这个问题。

我不记得有关谷歌分析集成的教程,我发现要添加此标记,但当我再次查看Google Analytics Overview时,它没有提及关于它。