Java.Lang.Illegalargumentexception: Can Only Use Lower 16 Bits for Requestcode

java.lang.IllegalArgumentException: Can only use lower 16 bits for requestCode

You need to pass a positive number to startActivityForResult.

androidx requestPermissionLauncher causes java.lang.IllegalArgumentException: Can only use lower 16 bits for requestCode

Add this dependency to fix the issue

implementation 'androidx.fragment:fragment:1.3.4'

Android app crash: java.lang.IllegalArgumentException: Can only use lower 16 bits for requestCode

I found some others questions in the SO with the same problem, seems for some Reason your request code is greater than the maximum you can.

The problem occurs in only few devices may be because the problem is solved in later versions.

This code is from the android.support.v4.app.FragmentActivity:

/**
* Modifies the standard behavior to allow results to be delivered to fragments.
* This imposes a restriction that requestCode be <= 0xffff.
*/
@Override
public void startActivityForResult(Intent intent, int requestCode) {
if (requestCode != -1 && (requestCode&0xffff0000) != 0) {
throw new IllegalArgumentException("Can only use lower 16 bits for requestCode");
}
super.startActivityForResult(intent, requestCode);
}

The code above throw a exception if your problem will occurs, so you can test in any device, just override the method on your activity.

I have seem in a comment that the problem may not occurs if you use an Integer beside an int

Another workaround may be use some of the APP Compat Libraries.

Link to the post with the answer above.

Can only use lower 16 bits for requestCode (Google play services)

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.



Related Topics



Leave a reply



Submit