【问题标题】:Android studio keeps setting int[] variable to nullAndroid Studio 不断将 int[] 变量设置为 null
【发布时间】:2015-08-16 08:08:35
【问题描述】:

我有一个包含 6 个元素的 int 变量。代码中的所有时间 android 都将其设置为 null。我什至像这样在调试模式的代码中重新定义了它

if (yeahsort == null) {
    SharedPreferences prefs2 = getSharedPreferences("KARANTÄN", MODE_PRIVATE);
    String savedString = prefs2.getString("string", null);
    StringTokenizer st = new StringTokenizer(savedString, ",");
    int[] yeahsort = new int[6];
    for (int i = 0; i < 6; i++) {
        yeahsort[i] = Integer.parseInt(st.nextToken());
    }
}

在 for 循环期间,变量 yeahsort 变成了 int[],但紧接着它又变成了 null。在 for 循环之后。

那么我该怎么做才能解决这个问题?

【问题讨论】:

  • 您正在声明一个同名的新变量。退出该功能后,该功能将消失。

标签: android variables exception null


【解决方案1】:

不要将int[] yeahsort = new int[6]改为yeashsort = new int[6],这是关于变量作用域http://www.java-made-easy.com/variable-scope.html的课程

【讨论】:

    猜你喜欢
    • 2016-12-31
    • 1970-01-01
    • 1970-01-01
    • 2011-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多