【问题标题】:"Cannot find symbol variable" in Android SDKAndroid SDK 中的“找不到符号变量”
【发布时间】:2014-04-27 10:49:21
【问题描述】:

在编译我的新 Android 应用程序 Android Studio 退出时出现此错误: “找不到符号变量 percscon”。

我的代码是

if(ulterioretext.equals("")) {
            ....
        } else {

            String uno =  (String) "1";
            float numbersconto = Float.valueOf(ulterioretext);

            if(numbersconto <= 9){
                String percscont = (String) uno + ".0" + ulterioretext;
            } else {
                String percscont = (String) uno + "." + ulterioretext;
            }

            float percscontfloat = Float.valueOf(percscont); <- The error is here

            ....
        }

如果我把 "String percscont = (String) uno + ".0" + ulterioretext;"在 if 之外一切正常,但如果它在“if”之内,编译器没有找到 percscont 变量。为什么?

谢谢

【问题讨论】:

  • 从您刚刚展示的代码中看不太容易,但我认为您在 if 的第一个分支中使用了 percscont 变量。能否请您发布整个 if 代码?

标签: android variables sdk android-studio


【解决方案1】:

找不到符号变量'''percscon'''。

你已经使用了变量

 float percscontfloat = Float.valueOf(percscont); 

一定是错字。 毫无疑问

所以你已经声明了在 if 和 else 块中具有范围的变量。

 if(numbersconto <= 9){
                String percscont = (String) uno + ".0" + ulterioretext;
            } else {
                String percscont = (String) uno + "." + ulterioretext;
            }

此变量外不可访问。

因此在方法或块的外部或开始处声明它,并在 if-else 之后使其可访问。

【讨论】:

    【解决方案2】:

    我自己解决了这个问题。

    我必须在 if 之外初始化变量,现在可以正常工作了!

    谢谢

    【讨论】:

    • 很高兴看到你接受我的建议并忽略它;)
    • 我没有忽略您的建议。我发布了我的问题,然后我注意到我犯了那个错误并写下了我的解决方案。但我没有看到其他回复。
    【解决方案3】:

    如果您在if 范围内声明percscont 变量,那么您无法从外部访问它,如果您想从if 外部访问,则在上层范围内声明。

    【讨论】:

      猜你喜欢
      • 2015-11-24
      • 1970-01-01
      • 2020-06-30
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多