[英]Variable cannot be resolved
I am trying to create an item list, diffrent for each i and j variable.我正在尝试创建一个项目列表,每个 i 和 j 变量都不同。 My code is:
我的代码是:
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[] items
may not be initialised or something... How can I make this programme run?我认为编译器认为
CharSequence[] items
可能没有初始化或什么的......我怎样才能让这个程序运行?
You actually have 4 items
variables in your code, each one with a very limited scope (only the code-block of the respective if
).您的代码中实际上有 4 个
items
变量,每个变量的范围都非常有限(只有相应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.编辑:我忘了这里需要
new CharSequence[]
。 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 only valid in an initializaton statement (ie in an assignment that is in the same statement as the declaration).
出于某种原因,定义数组的简短语法仅在初始化语句中有效(即在与声明相同的语句中的赋值中)。
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 i
who goes out of scope immediately after they're declared and initialized.这里我们有两个局部变量
i
,它们在声明和初始化后立即超出范围。 If j
needs the value of i
, then i
would have to be declared in a larger scope.如果
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, ie) (需要说明的是,这正是三元运算符所擅长的那种场景,即)
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 items
outside the if
and write the verbose code for each case (see Joachim Sauer's answer), but a more concise code is to use array-of-arrays instead.您可以取出
if
之外的items
声明并为每种情况编写详细代码(请参阅 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 Map
or something similar.这种技术在这种情况下效果很好,但在更一般的情况下,您希望使用
Map
或类似的东西。
Note: It's not explicit in the original code, but this works if j
can either be 0
, 1
, 2
, or 3
.注意:原始代码中没有明确说明,但是如果
j
可以是0
、 1
、 2
或3
。 If you want the last option to apply when j
is any value other than 0
, 1
, 2
, then you have to check for that and set it to 3
before this code.如果您希望在
j
是0
、 1
、 2
以外的任何值时应用最后一个选项,那么您必须检查该值并将其设置为3
在此代码之前。
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.所以一旦你到达那个右花括号 } 你的 items 变量就不再可见了。 This is different from JavaScript for example where you have function-level scope.
这与 JavaScript 不同,例如您拥有函数级作用域。
Hope this helps.希望这可以帮助。
Because you define (as well as give a value to) items
within a block, it is only visible within that block.因为您在块内定义(并为其赋值)
items
,所以它仅在该块内可见。 Pull the definition out of the block to somewhere visible to both the snippets you have given us, and just assign a value within the if
else
construct.将定义从块中拉出到您提供给我们的两个片段都可见的某个地方,然后在
if
else
构造中分配一个值。
Declare items
before the在之前声明
items
if (i == 0) {
The way you are doing it now, items
is only in scope inside you inner if
s.你现在这样做的方式,
items
只在你内部if
s 的范围内。
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 子句内实例化它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.