Java 变量无法解析
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2585650/
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
Variable cannot be resolved
提问by Devel
I am trying to create an item list, diffrent for each i and j variable. My code is:
我正在尝试创建一个项目列表,每个 i 和 j 变量都不同。我的代码是:
if (i == 0) {
if (j == 0) {
final CharSequence[] items = {"4:45", "5:00"}
} else if (j == 1) {
final CharSequence[] items = {"4:43", "4:58"}
} else if (j == 2) {
final CharSequence[] items = {"4:41", "4:56"}
} else {
final CharSequence[] items = {"4:38", "4:53"}
}
...
...
new AlertDialog.Builder(this)
.setTitle("Hours")
.setItems(items,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialoginterface, int i) {
// getStation(i);
}
})
.show();
}
I get an error in the line .setItems(items,:
我在行中收到错误.setItems(items,:
items cannot be resolved
I think that the compiler thinks that the CharSequence[] itemsmay not be initialised or something...
How can I make this programme run?
我认为编译器认为CharSequence[] items可能没有初始化或什么......我怎样才能让这个程序运行?
采纳答案by polygenelubricants
The problem is variable scoping.
问题是变量范围。
if (someCondition) {
final int i = 666;
} else {
final int i = 42;
}
int j = i + 1; // compile-time error
Here we have two local variables iwho goes out of scope immediately after they're declared and initialized. If jneeds the value of i, then iwould have to be declared in a larger scope.
这里我们有两个局部变量i,它们在声明和初始化后立即超出范围。如果j需要 的值i,则i必须在更大的范围内声明。
final int i;
if (someCondition) {
i = 666;
} else {
i = 42;
}
int j = i + 1; // compiles fine!
(It should be mentioned that this is exactly the kind of scenarios where the ternary operator excels, i.e.)
(需要说明的是,这正是三元运算符擅长的场景,即)
final int i = (someCondition) ? 666 : 42;
In your specific case, unfortunately the array initializer shorthand can only be used to initialize upon declaration. That is:
在您的特定情况下,不幸的是,数组初始值设定项简写只能用于在声明时进行初始化。那是:
int[] arr1 = { 1, 2, 3 }; // compiles fine!
int[] arr2;
arr2 = { 4, 5, 6 }; // doesn't compile!
You can pull out the declaration of itemsoutside the ifand write the verbose code for each case (see Joachim Sauer's answer), but a more concise code is to use array-of-arrays instead.
您可以取出items外部的声明if并为每种情况编写详细代码(请参阅 Joachim Sauer 的回答),但更简洁的代码是使用数组数组代替。
final CharSequence[][] allItems = {
{ "4:45", "5:00" },
{ "4:43", "4:58" },
{ "4:41", "4:56" },
{ "4:38", "4:53" }
};
final CharSequence[] items = allItems[j];
This technique works well in this case, but in the more general case you want to use a Mapor something similar.
这种技术在这种情况下效果很好,但在更一般的情况下,您希望使用 aMap或类似的东西。
Note: It's not explicit in the original code, but this works if jcan either be 0, 1, 2, or 3. If you want the last option to apply when jis any value other than 0, 1, 2, then you have to check for that and set it to 3before this code.
注意:它在原始代码中并不明确,但如果j可以是、、 或0,则此方法有效。如果您希望最后一个选项在, ,以外的任何值时应用,那么您必须检查该选项并将其设置在此代码之前。123j0123
回答by Joachim Sauer
You actually have 4 itemsvariables in your code, each one with a very limited scope (only the code-block of the respective if).
您items的代码中实际上有 4 个变量,每个变量的范围都非常有限(只有各自的代码块if)。
Instead you'll want to create one variable with a bigger scope:
相反,您需要创建一个更大范围的变量:
if (i == 0) {
final CharSequence[] items;
if (j == 0) {
items = new CharSequence[] {"4:45", "5:00"};
} else if (j == 1) {
items = new CharSequence[] {"4:43", "4:58"};
} else if (j == 2) {
items = new CharSequence[] {"4:41", "4:56"};
} else {
items = new CharSequence[] {"4:38", "4:53"};
}
// you can use items here
}
Edit:I forgot that the new CharSequence[]is necessary here. You can leave it out if you initialize the variable during declaration, but here you moved the declaration out and use a simple assignment to set a value. For some reason the short syntax of defining an array is onlyvalid in an initializaton statement (i.e. in an assignment that is in the same statement as the declaration).
编辑:我忘了new CharSequence[]这里是必要的。如果在声明期间初始化变量,则可以省略它,但在这里您将声明移出并使用简单的赋值来设置值。出于某种原因,定义数组的简短语法仅在初始化语句中有效(即在与声明相同的语句中的赋值中)。
回答by Robby Pond
You are only declaring items in local scope. You need to move the
您只是在本地范围内声明项目。你需要移动
final CharSequence[] items
outside the if clauses and the instantiate it inside the if clause.
在 if 子句之外并在 if 子句内实例化它。
回答by AakashM
Because you define(as well as give a value to) itemswithin a block, it is only visible within that block. Pull the definition out of the block to somewhere visible to both the snippets you have given us, and just assign a valuewithin the ifelseconstruct.
因为您在块内定义(并为其赋值)items,所以它仅在该块内可见。将定义从块中拉出到您提供给我们的两个片段都可见的某个地方,然后在构造中分配一个值ifelse。
回答by Thomas L?tzer
Declare itemsbefore the
items在之前声明
if (i == 0) {
The way you are doing it now, itemsis only in scope inside you inner ifs.
你现在这样做的方式,items只在你内部if的范围内。
回答by greim
In Java you have strict block-level scope, so for example:
在 Java 中,您有严格的块级作用域,例如:
if (blah) { int foo = 1; }
// foo is no longer visible here
So once you reach that closing curly brace } your items variable is no longer visible. This is different from JavaScript for example where you have function-level scope.
所以一旦你到达那个右花括号 } 你的 items 变量就不再可见了。这与 JavaScript 不同,例如您拥有函数级作用域。
Hope this helps.
希望这可以帮助。

