永远不会读取局部变量xxx
这是我的代码:
boolean startGameBoolean; Bundle extras = getIntent().getExtras(); extras.getInt("startGameBoolean"); if (startGameBoolean = true){ counter.start(); }
Eclipse发出警告:“永远不会读取局部变量startGameBoolean”; 但它是。 我从另一个意图中得到布尔值。
我编辑了我的代码,我错过了一些,对不起!
不应该是startGameBoolean = extras.getBoolean("startGameBoolean");
?
在你给的代码中, boolean startGameBoolean;
没有在任何地方使用。 警告意味着虽然您声明了它,但它不会在它所居住的区块中使用,因此可以(应该)被删除。
编辑:
看到你的添加后,你使用startGameBoolean
,但你分配给它,你应该比较它:
if (startGameBoolean == true){ // ^ counter.start(); }
另外,将getBoolean()
的结果赋值给我在第一个语句中写的变量。
我冒昧地同意Eclipse,它没有被使用。 您向我们展示的代码并没有在任何地方使用它。
更新
该变量仍未使用,因为您在if
条件中分配它; 你永远不会比较它(这就是你想要做的事情)。
更改
if (startGameBoolean = true){
至
if (startGameBoolean){
if (startGameBoolean = true){
应该..
if (startGameBoolean == true){
..或者更好..
if (startGameBoolean){
Bundle extras = getIntent().getExtras(); boolean startGameBoolean = extras.getBoolean("startGameBoolean");
像这样的东西? 你不能用getInt()
获取boolean
。 也许,如果你正在处理int但值为0
和1
。 然后你应该将startGameBoolean
的类型startGameBoolean
为int
并使用getInt()
获取它,人们有时会这样做。
我有同样的问题为Android开发。
你看到那些警告说“变量永远不会读”的原因是因为你在创建它之后正确分配它。 尝试在类的开头创建变量,然后在OnCreate()或在首次创建类时调用的其他适当方法中分配它。
我有一个类似的错误 ……对于这一行
JLabel label = new JLabel("You have $" + amountInAccount + " in your account.");
我使用快速修复(Ctrl + 1)并选择删除’标签’,保留副作用分配…
并且代码变成了
new JLabel("You have $" + amountInAccount + " in your account.");