我制作了一个示例应用,尝试如何连接到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>
根据互联网信息,元数据标签必须是应用标签下的第一个子标签,但我尝试了几次,位置......并且不起作用。
答案 0 :(得分:0)
试试这个:
@Override
public void onStart() {
super.onStart();
if(client != null){
client.connect();
}
}
检查设备上是否安装了Google Play服务:
GoogleApiAvailability googleApiAvailability = GoogleApiAvailability.getInstance();
int resultCode = googleApiAvailability.isGooglePlayServicesAvailable(this);
if (resultCode == ConnectionResult.SUCCESS) {
// do your task
} else{
Toast.makeText(this, "Google Play Services required!!", Toast.LENGTH_LONG).show();
}
答案 1 :(得分:0)
将此添加到您的onCreate
方法
if (client == null) {
client = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(Games.API).addScope(Games.SCOPE_GAMES)
.build();
}
并将@Override
添加到您的onConnectionFailed
方法