我得到了 抛出:IllegalArgumentException: 在尝试初始化此链接here中提到的GoogleApiClient时。
下面是我用来初始化ApiClient的代码,后跟错误日志
mApiClient = new GoogleApiClient.Builder(getActivity())
.addApi(Fitness.HISTORY_API)
.addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ))
.addConnectionCallbacks(
new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(Bundle bundle) {
Log.i(TAG, "Connected!!!");
// Now you can make calls to the Fitness APIs.
}
@Override
public void onConnectionSuspended(int i) {
// If your connection to the sensor gets lost at some point,
// you'll be able to determine the reason and react to it here.
if (i == GoogleApiClient.ConnectionCallbacks.CAUSE_NETWORK_LOST) {
Log.i(TAG, "Connection lost. Cause: Network Lost.");
} else if (i
== GoogleApiClient.ConnectionCallbacks.CAUSE_SERVICE_DISCONNECTED) {
Log.i(TAG,
"Connection lost. Reason: Service Disconnected");
}
}
}
)
.enableAutoManage(getActivity(), 1, new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(ConnectionResult result) {
Log.i(TAG, "Google Play services connection failed. Cause: " +
result.toString());
}
})
.build();
错误日志
Process: fitbark.com.android, PID: 3299
java.lang.IllegalArgumentException: Can only use lower 16 bits for requestCode
at android.support.v4.app.BaseFragmentActivityEclair.checkForValidRequestCode(BaseFragmentActivityEclair.java:64)
at android.support.v4.app.BaseFragmentActivityEclair.startIntentSenderForResult(BaseFragmentActivityEclair.java:45)
at android.support.v4.app.FragmentActivity.startIntentSenderForResult(FragmentActivity.java:75)
at com.google.android.gms.common.ConnectionResult.startResolutionForResult(Unknown Source)
at com.google.android.gms.common.api.internal.zzw$zzb.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5696)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
当我评论以下方法时,我没有收到任何错误
enableAutoManage()
据我所知,错误是由于某个应用程序调用startActivityWithResult()而requestCode大于16位,但我不明白的是它到底发生了什么以及如何修复它?
答案 0 :(得分:2)
如果您
,确实会发生此错误您使用的是哪个版本的Google Play服务以及哪个Android支持库?
我遇到了同样的问题,通过this google+ post解决了问题:
在开发使用google fit功能的应用时,我们遇到了一个问题。当我们将支持库更新到24.0.0-beta1版本时,它会在构建google fit客户端时为requestCode抛出异常。 IllegalArgumentException:对于requestCode
,只能使用低16位
...
找到原因,将播放服务更新为9.0.1修复它。
因此,更新播放服务库或降级支持库可能会有所帮助。
如果您执行更新/降级,但仍未授予播放服务应用程序的完全权限,您将看到由enableAutoManage()方法引起的权限请求对话框。