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

北战南征 提交于 2019-11-27 14:16:52
Stephane Mathis

You need to pass a positive number to startActivityForResult.

Henry

You get this exception only in android.support.v4.app.FragmentActivity and not when you use android.app.Activity.

startActivityForResult() in FragmentActivity requires the requestCode to be of 16 bits, meaning the range is from 0 to 65535.

Also, validateRequestPermissionsRequestCode in FragmentActivity requires requestCode to be of 16 bits, meaning the range is from 0 to 65535.

For more info(if you want to see the source code) : https://stackoverflow.com/a/33331459/4747587

It is also good to mention that this may happen if you use a number greater than 2^16 / 2 (which is 32768), so there's basically 2^15 options to not to screw this up.

Explanation: 16 bits can represent one of 65536 numbers, however, half of them are negative.

ONE

You can only use lower 16 bits for requestCode means -- in binary terms -- you can use

0000000000000000 (16 bits) to 1111111111111111 (16 bits).

In decimal ("number") terms, this allows for 2^16 = 65536 combinations. Therefore, you can only use the numbers 0 all the way through 65535.

You also cannot use negative numbers.

The right answer is that you should use a 16 bits number for this purpose. The most safe solution for that is to always set your request code as short. If programmer attempts to write number more than 16 bits then the IDE won't let you to proceed because there will be an error.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!