Android GoogleApiClient.Builder().enableAutoManage 导致 IllegalArgumentException:RequestCode 只能使用低 16 位

Android GoogleApiClient.Builder().enableAutoManage causes IllegalArgumentException: Can only use lower 16 bits for requestCode

我得到一个 非法参数异常: 当尝试按照 link 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()

我知道错误是由于 App 在某处调用 startActivityWithResult() 并且 requestCode 大于 16 位,但我不明白它到底发生在哪里以及如何修复它?

如果您

  1. 限制 google 播放服务应用程序和
  2. 的权限
  3. 同时针对旧的播放服务版本和较新的支持库版本。

您使用哪个版本的 google 播放服务和哪个 android 支持库?

我遇到了同样的问题,已解决 this google+ post:

While developing an app which uses google fit feature, we ran into one problem. When we updated the support libraries to 24.0.0-beta1 version it is throwing an exception for requestCode when building client for google fit. IllegalArgumentException: Can only use lower 16 bits for requestCode

...

found the cause, updating play services to 9.0.1 fixed it.

因此,更新您的播放服务库或降级支持库可能会有帮助。

如果您执行 update/downgrade,但仍未授予播放服务应用程序的全部权限,您将看到由 enableAutoManage() 方法引起的权限请求对话框。