Android java.lang.IllegalArgumentException:请求代码只能使用低 16 位
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25529865/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.lang.IllegalArgumentException: Can only use lower 16 bits for requestCode
提问by CoDe
I am writing an application where Activity A
launches Activity B
using
我在哪里写的应用程序Activity A
启动Activity B
使用
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 -101but I am not sure. Does any one have any idea on this?
可能是-101,但我不确定。有没有人对此有任何想法?
采纳答案by Stephane Mathis
You need to pass a positive number to startActivityForResult
.
您需要将一个正数传递给startActivityForResult
.
回答by Henry
You get this exception only in android.support.v4.app.FragmentActivity
and not when you use android.app.Activity
.
您只会在android.support.v4.app.FragmentActivity
使用android.app.Activity
.
startActivityForResult()
in FragmentActivity
requires the requestCode
to be of 16 bits, meaning the range is from 0 to 65535.
startActivityForResult()
inFragmentActivity
要求requestCode
是 16 位,这意味着范围是从 0 到 65535。
Also, validateRequestPermissionsRequestCode
in FragmentActivity
requires requestCode
to be of 16 bits, meaning the range is from 0 to 65535.
此外,validateRequestPermissionsRequestCode
inFragmentActivity
要求requestCode
为 16 位,这意味着范围是从 0 到 65535。
For more info(if you want to see the source code) : https://stackoverflow.com/a/33331459/4747587
有关更多信息(如果您想查看源代码):https: //stackoverflow.com/a/33331459/4747587
回答by vanomart
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.
值得一提的是,如果您使用的数字大于 2^16 / 2(即 32768),则可能会发生这种情况,因此基本上有 2^15 个选项可以避免搞砸。
Explanation: 16 bits can represent one of 65536 numbers, however, half of them are negative.
说明:16 位可以表示 65536 个数字中的一个,但有一半是负数。
回答by ONE
You can only use lower 16 bits for requestCode means -- in binary terms -- you can use
您只能使用较低的 16 位来表示 requestCode 意味着——在二进制方面——您可以使用
0000000000000000 (16 bits) to 1111111111111111 (16 bits)
.
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.
在十进制(“数字”)术语中,这允许2^16 = 65536
组合。因此,您只能使用数字 0 到 65535。
You also cannot use negative numbers.
您也不能使用负数。
回答by Tzegenos
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.
正确的答案是您应该为此使用 16 位数字。最安全的解决方案是始终将您的请求代码设置为short
. 如果程序员尝试写入超过 16 位的数字,则 IDE 将不会让您继续,因为会出现错误。