简体   繁体   English

使用if / else语句时“无法解析符号”

[英]'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.

相关问题 使用 URL 时无法解决符号错误 - Cannot Resolve Symbol error when using URL 在排球中使用缓存时无法解析符号cacheEntry.lastModified - Cannot resolve symbol cacheEntry.lastModified when using cache in volley 使用Google Analytics(分析)时无法解析符号“分析” - Cannot Resolve Symbol 'analytics' When Using Google Analytics 在带有for循环的if语句中使用数组时找不到符号 - Cannot find symbol when using a array in an if statement with for loop 错误:“找不到符号”使用 if + else - Error: "cannot find symbol" using if + else 使用公共类的“无法解析符号”错误 - “Cannot Resolve Symbol” error using a public class 无法使用Json为我的列表解析符号 - Cannot resolve symbol for my List using Json AndroidStudio:无法解析符号“MainActivity”。使用 AndroidAnnotations - AndroidStudio :Cannot resolve symbol 'MainActivity'.Using AndroidAnnotations 无法使用TarsosDSP为Android应用程序解析符号 - Cannot resolve symbol using TarsosDSP for an android application 错误-尝试在RecyclerView中使用Glide加载图像时出现“无法解析符号'上下文” - ERROR - “Cannot resolve symbol 'context” when trying to load images using Glide in RecyclerView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM