6

I'm getting the error

IllegalArgumentException: Can only use lower 16 bits for requestCode

When I'm pressing Enable Google Play Services in my app.

The code I have is

private boolean checkPlayServices() {
    int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
    Log.d("resultCode", String.valueOf(resultCode));
    if (resultCode != ConnectionResult.SUCCESS) {
        if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
            GooglePlayServicesUtil.getErrorDialog(resultCode, this, GooglePlayServicesUtil.GOOGLE_PLAY_SERVICES_VERSION_CODE).show();
        } else {
            Log.i("FUApp", "This device is not supported.");
            finish();
        }
        return false;
    }
    return true;
}

Here's the StackTrace of the error

01-29 14:15:30.714  10725-10725/com.example.fitnessunlimited.android E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.fitnessunlimited.android, PID: 10725
java.lang.IllegalArgumentException: Can only use lower 16 bits for requestCode
        at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:837)
        at com.google.android.gms.internal.i.onClick(Unknown Source)
        at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5017)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
        at dalvik.system.NativeStart.main(Native Method)

How can I fix this error? Pressing the enable button works on Google Now, just not in the app I'm building.

TMH
  • 6,096
  • 7
  • 51
  • 88

1 Answers1

4

My problem was

GooglePlayServicesUtil.getErrorDialog(resultCode, this, GooglePlayServicesUtil.GOOGLE_PLAY_SERVICES_VERSION_CODE)

Changing that line to

GooglePlayServicesUtil.getErrorDialog(resultCode, this, 9000)

made the button work correctly and open up the settings page.

TMH
  • 6,096
  • 7
  • 51
  • 88
  • K good votes up. Define that literal in this way private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000; because CONNECTION_FAILURE_RESOLUTION_REQUEST can be used at some other location. – Zohra Khan Jan 29 '14 at 14:52
  • Yeah, that's where I got the number from, I've moved it into that variable now I've seen it works – TMH Jan 29 '14 at 14:53
  • Sorry, It was easy to crack but I never read the full line of getErrorDialog. Just by seeing logcat I always assume its something with startAcitivityResult. – Zohra Khan Jan 29 '14 at 14:57
  • 2
    Why 9000 and not 10 or 1. What is the difference here? I have seen different numbers out there. – Jorge Feb 11 '14 at 23:21
  • 1
    9000 is entirely arbitrary. It just needs to be something you're not already using elsewhere as an activity request code (as in onActivityResult's requestCode) and needs to be below 65536 for arcane Android reasons. –  Jun 30 '15 at 21:29