Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 是否有其他方法引用非静态内容?_Java_Android_Static_Non Static - Fatal编程技术网

Java 是否有其他方法引用非静态内容?

Java 是否有其他方法引用非静态内容?,java,android,static,non-static,Java,Android,Static,Non Static,好吧,让我说,这个程序在某种程度上是有效的。因为我试图从静态内容中引用检查分数,所以我这样做了: ScoringActivity scoringActivity = new ScoringActivity(); scoringActivity.CheckScores(etEnteredScores.getText().toString()); 现在这是公共静态类ScoringFragment extensed Fragment的内部,它与活动位于同一.class中 一开始我认为这是可行的,

好吧,让我说,这个程序在某种程度上是有效的。因为我试图从静态内容中引用
检查分数
,所以我这样做了:

 ScoringActivity scoringActivity = new ScoringActivity();
 scoringActivity.CheckScores(etEnteredScores.getText().toString());
现在这是
公共静态类ScoringFragment extensed Fragment
的内部,它与
活动
位于同一
.class

一开始我认为这是可行的,但在查看了我在该方法中使用的过量日志后,结果证明并非如此

requiredAmount
是由用户设置并存储在SQL数据库中的最大分数

这是
logcat

02-05 23:58:42.720    3492-3492/com.test.app V/required Count﹕ 6 <-Straight from SQL table
02-05 23:58:42.720    3492-3492/com.test.app V/required Count﹕ 6 <-From the variable after I assign it.
02-05 23:58:54.606    3492-3492/com.test.app V/requiredAmount﹕ 0 <-From the first Log.v in CheckScores after calling new
02-05 23:58:54.606    3492-3492/com.test.app V/userScores﹕ 5 <-Just me spliting it into an array
02-05 23:58:54.606    3492-3492/com.test.app V/userScores﹕ [5] <-Checking if it split right
02-05 23:58:54.606    3492-3492/com.test.app V/Mismatch﹕ You have 1 scores. Acceptable amount is 0 <-As figured its 0 instead of 6
您可以看到,只要调用该方法,我就会检查
requiredAmount
requiredAmount
是允许用户获得的最大分数数。现在我在前面检查了这个变量,它从SQL表中获取它,没有问题

我认为正在发生的是,当我调用
newscoringactivity()
时,它会重置所有变量。是否有其他方法通过
CheckScores
方法传递数据


如果您需要更多信息,请随时询问。我可能不会马上回答,但我会尽快回来。

new ScoringActivity()
不会重置任何变量。。。它只是创建了另一个带有默认变量的ScoringActivity。旧的仍然存在,但您正在访问新的,当然还没有任何设置,因为您刚刚创建了它。顺便说一句,您真的不应该创建这样的活动。你不应该
new
一个活动,而应该只使用
startActivity()
启动它。天哪,你不应该自己创建(
new
)活动,Android会为你做的。这就是为什么您有onCreate()。如果我不
new
活动,它就不能调用
CheckScores
,因为它是静态类中的非静态内容
public void CheckScores(String userScores) {
    String[] scoresArray;

    Log.v("requiredAmount", Integer.toString(requiredCount));

    // ^ Match with beginning of line | [0-9] Allow 0-9 | , Allow comma | + Match one or more | $ Match End of line
    if (userScores.matches("^[0-9,]+$")) {

        if (userScores.charAt(0) == ',') {
            // If it does parse and substring to remove them
            // otherwise the following regex leaves one , behind
            int i = 0;
            while (!Character.isDigit(userScores.charAt(i))) i++;
            int j = userScores.length();
            userScores = userScores.substring(i, j);
        }

        // (.) Match any character) | \1 If it is followed by itself | + Match one or more | $1 replace by the first captured char.
        userScores = userScores.replaceAll("(.)\\1+", "$1");

        Log.v("userScores", userScores);

        // Split at the ',' and put each number in it's own cell in the array
        scoresArray = userScores.split(",");
        Log.v("userScores", Arrays.toString(scoresArray));

        // Check if scoresArray is equal to maxScores
        if (scoresArray.length == requiredCount) {
            int[] uiArray = new int[scoresArray.length];

            // Parse String[] into int[]
            for (int i = 0; i < scoresArray.length; i++) {
                try {
                    uiArray[i] = Integer.parseInt(scoresArray[i]);
                } catch (NumberFormatException nfe) { // If triple checking isn't enough...
                }
            }
            Log.v("uiArray(int)", Arrays.toString(uiArray));

            // Add up all elements in uiArray
            for (int j = 0; j < uiArray.length; j++) {
                sum += uiArray[j];
            }
            comformedScores = uiArray;
            Log.v("Accepted","Scores sum:" + sum + " Number of scores:" + uiArray.length + " Number of ends:" + uiArray.length / 6);
        } else {
            Log.v("Mismatch", "You have " + scoresArray.length + " scores. Acceptable amount is " + requiredCount);
        }
    } else {
        Log.v("Mismatch","Invalid Input. (Only #'s and ,'s allowed)");
    }
}