在设备上连接到Google Play服务崩溃

我制作了一个示例应用程序来尝试如何连接到Google Play服务,当在我的手机或平板电脑上尝试它时,它在打开应用程序时崩溃。

我确定错误是在尝试使用client.connect()进行连接时出现的,因为当被注释时,应用程序不会崩溃。

主要的(也是唯一的)活动如下,我省略了导入,因为可以正确编译。

public class ry extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener {


private GoogleApiClient client;
//GoogleApiClient

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


   client = new GoogleApiClient.Builder(this)
           .addConnectionCallbacks(this)
           .addOnConnectionFailedListener(this)
            .addApi(Games.API).addScope(Games.SCOPE_GAMES)
            .build();

}

@Override
public void onStart() {
    super.onStart();
    client.connect();  //   <- Error line


}

@Override
public void onStop() {
    super.onStop();

}

public void onConnectionSuspended(int cause) {
    // We are not connected anymore!
    Log.i("app", "connectionsuspended");
}

public void onConnected(Bundle connectionHint) {

}

public void onConnectionFailed(ConnectionResult result) {
    // We tried to connect but failed!


    try {
        result.startResolutionForResult(this, 49404);
    } catch (Exception e) {
        Log.i("app", e.toString());
    }

}

}

还有我的gradle.build:

[...]
 dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.0.0'
compile 'com.google.android.gms:play-services-games:9.2.0'
 }
[...]

任何帮助都将非常受欢迎。

更新时,我将发布日志错误的相关行,因为没有错误,只有警告,并且我发现以下内容相关。

07-04 12:20:15.840 5124-5192/com.example.internet.connectiontry W/GooglePlayServicesUtil: Google Play services out of date.  Requires 9256000 but found 9080270

更新2:

看起来,使用

compile 'com.google.android.gms:play-services:4.2.+'

相反,它做到了这一点,但是,由于以下原因,它崩溃了:

--------- beginning of crash
07-04 19:35:14.788 2245-2245/com.example.internet.connectiontry     E/AndroidRuntime: FATAL EXCEPTION: main
[...]
   Caused by: java.lang.IllegalStateException: A required meta-data tag in your app's AndroidManifest.xml does not exist.  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" />

这是我的清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.internet.connectiontry">

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>


    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

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

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

            </intent-filter>
        </activity>

    </application>


</manifest>

根据互联网信息,该元数据标签必须是应用程序标签下的第一个孩子,但是我尝试了几次,位置...并且不起作用。

转载请注明出处:http://www.0591kyj.com/article/20230526/1921623.html