[英]'Cannot resolve symbol' when using an if/else statement
I'm just wondering why I get a "cannot resolve symbol initialText" with the following code. 我只是想知道为什么我用下面的代码得到“无法解析符号initialText”。 isItA is a boolean which the user selects earlier depending on which type of text (A or B) they are about to paste into the text area. isItA是一个布尔值,用户可以根据要粘贴到文本区域中的文本类型(A或B)提前选择。 There are 2 potential strings for initialText, and the one which is outputted depends on the boolean isItA. initialText有2个可能的字符串,输出的字符串取决于布尔值isItA。 I dont get why this error pops when i use initialText outside the if/else statement; 我不明白为什么在if / else语句之外使用initialText时会弹出此错误; if is inside the 'if/else' statement, the compiler should be able to resolve the string outside right? 如果在'if / else'语句中,则编译器应该能够在外部解析字符串? I'm currently using GWT in IntelliJ. 我目前在IntelliJ中使用GWT。 At this point I am still sort of noob at java, so a basic explanation of why this is happening would be greatly appreciated :) Thanks in advance. 在这一点上,我仍然对Java不满意,因此,非常感谢您了解为什么发生这种情况:)预先感谢。 A snippet of the code is below. 下面的代码片段。
protected TextArea getNewTextArea() {
if ( newTextArea == null ) {
if (isItA){
final String initialText = "Please paste valid text A here, and then
press" + "the \"" + Labels.ADD_A_BUTTON_TEXT + "\" button.";
}else{
final String initialText = "Please paste valid text B here, and then press " + "the \"" + Labels.ADD_B_BUTTON_TEXT + "\" button.";
}
newTextArea = new TextArea();
newTextArea.setText( initialText );
newTextArea.addClickHandler( new ClickHandler() {
public void onClick( ClickEvent clickEvent ) {
// If the text is still the original text, then clear it.
if ( newTextArea.getText().equals( initialText ) ) {
newTextArea.setText( "" );
}
}
});
}
}
initialText is unresovlable because it is not within the given code block scope. initialText不可取消,因为它不在给定的代码块范围内。
To fix this, declare intitalText before the if/else statement like so: 要解决此问题,请在if / else语句之前声明intitalText,如下所示:
protected TextArea getNewTextArea() {
if ( newTextArea == null ) {
final String initialText;
if (isItA){
initialText = "Please paste valid text A here, and then
press" + "the \"" + Labels.ADD_A_BUTTON_TEXT + "\" button.";
}else{
initialText = "Please paste valid text B here, and then press " + "the \"" + Labels.ADD_B_BUTTON_TEXT + "\" button.";
}
newTextArea = new TextArea();
newTextArea.setText( initialText );
newTextArea.addClickHandler( new ClickHandler() {
public void onClick( ClickEvent clickEvent ) {
// If the text is still the original text, then clear it.
if ( newTextArea.getText().equals( initialText ) ) {
newTextArea.setText( "" );
}
}
} );
}
因为你的final String initialText
是本地if-else
block.So,外面声明它的if-else块,比初始化里面。
initialText仅存在于curlies块内。
You need to move initialText to right below the beginning of function block 您需要将initialText移到功能块开始下方的右边
protected TextArea getNewTextArea() { 受保护的TextArea getNewTextArea(){
String initialText ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.