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

匿名 (未验证) 提交于 2019-12-03 02:14:01

问题:

I am writing an application where Activity A launches Activity B using

startActivityForResult(intent, -101); 

but when called, it responded back with following error log:

E/AndroidRuntime( 1708): java.lang.IllegalArgumentException: Can only use lower 16 bits for requestCode E/AndroidRuntime( 1708):    at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:837) 

Probably it could be -101 but I am not sure. Does any one have any idea on this?

回答1:

You need to pass a positive number to startActivityForResult.



回答2:

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



回答3:

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.



回答4:

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.



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