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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:31:57  来源:igfitidea点击:

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

androidstart-activity

提问by CoDe

I am writing an application where Activity Alaunches Activity Busing

我在哪里写的应用程序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.FragmentActivityand not when you use android.app.Activity.

您只会在android.support.v4.app.FragmentActivity使用android.app.Activity.

startActivityForResult()in FragmentActivityrequires the requestCodeto be of 16 bits, meaning the range is from 0 to 65535.

startActivityForResult()inFragmentActivity要求requestCode是 16 位,这意味着范围是从 0 到 65535。

Also, validateRequestPermissionsRequestCodein FragmentActivityrequires requestCodeto be of 16 bits, meaning the range is from 0 to 65535.

此外,validateRequestPermissionsRequestCodeinFragmentActivity要求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 = 65536combinations. 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 将不会让您继续,因为会出现错误。