java 永远不会读取局部变量 xxx
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6802561/
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
The local variable xxx is never read
提问by Badr Hari
This is my code:
这是我的代码:
boolean startGameBoolean;
Bundle extras = getIntent().getExtras();
extras.getInt("startGameBoolean");
if (startGameBoolean = true){
counter.start();
}
Eclipse gives a warning that "The local variable startGameBoolean is never read"; but it is. I get the boolean from an another intent.
Eclipse 给出了“永远不会读取局部变量 startGameBoolean”的警告;但它是。我从另一个意图中得到布尔值。
I edited my code, I missed some of it, sorry!
我编辑了我的代码,我错过了一些,抱歉!
回答by MByD
Shouldn't it be startGameBoolean = extras.getBoolean("startGameBoolean");
?
不应该startGameBoolean = extras.getBoolean("startGameBoolean");
吗?
in the code you gave, boolean startGameBoolean;
is not used anywhere. the warning means that although you declared it, it is not used in the block where it lives, therefore could (should) be removed.
在您提供的代码中,boolean startGameBoolean;
未在任何地方使用。警告意味着虽然你声明了它,但它并没有在它所在的块中使用,因此可以(应该)被删除。
Edit:
编辑:
After seeing your addition, you use startGameBoolean
, but you assign to it, while you should compare to it:
看到您的添加后,您使用startGameBoolean
,但您分配给它,而您应该与它进行比较:
if (startGameBoolean == true){
// ^
counter.start();
}
Also, assign the result of getBoolean()
to the variable as I wrote in the first statement.
另外,将 的结果分配给getBoolean()
我在第一条语句中写的变量。
回答by Rob Hruska
I would venture to agree with Eclipse, it's not being used. The code you've show us doesn't have it being used anywhere.
我敢于同意 Eclipse,它没有被使用。您向我们展示的代码并未在任何地方使用。
Update
更新
The variable is still never used because you assignit in your if
condition; you never compare it (which is what it looks like you're trying to do).
该变量仍然从未使用过,因为您在条件中分配了它if
;你从不比较它(这就是你想要做的事情)。
Change
改变
if (startGameBoolean = true){
to
到
if (startGameBoolean){
回答by Andrew Thompson
if (startGameBoolean = true){
Should be..
应该..
if (startGameBoolean == true){
..or better still..
..或者更好..
if (startGameBoolean){
回答by Kyle Clegg
I had this same problem developing for Android.
我在为 Android 开发时遇到了同样的问题。
The reason you're seeing those warnings saying the "variable is never read" is because you're assigning it RIGHT AFTERcreating it. Try creating the variable at the start of the class, then assigning it in an OnCreate() or other appropriate method that gets called when the class is first created.
您看到这些警告说“永远不会读取变量”的原因是因为您在创建它之后立即分配它。尝试在类的开头创建变量,然后在首次创建类时调用的 OnCreate() 或其他适当的方法中分配它。
回答by Prakash Kaushal
I had a similar error... for this line
我有一个类似的错误......对于这一行
JLabel label = new JLabel("You have $" + amountInAccount + " in your account.");
JLabel label = new JLabel("You have $" + amountInAccount + " in your account.");
I used Quick fix (Ctrl+1)and chose Remove 'label', keep side-effect assignments...
我使用了快速修复 (Ctrl+1)并选择了删除“标签”,保留副作用分配...
and the code became
代码变成了
new JLabel("You have $" + amountInAccount + " in your account.");
new JLabel("You have $" + amountInAccount + " in your account.");
回答by Nikola Despotoski
Bundle extras = getIntent().getExtras();
boolean startGameBoolean = extras.getBoolean("startGameBoolean");
Something like this? You cant fetch boolean
with getInt()
. Perhaps, if you are dealing with int but with values 0
and 1
. Then you should change the type of startGameBoolean
to int
and fetch it with getInt()
, people do this sometimes.
像这样的东西?你不能boolean
用getInt()
. 也许,如果您正在处理 int 但处理 values0
和1
. 然后你应该改变startGameBoolean
to的类型int
并使用 获取它getInt()
,人们有时会这样做。