有没有其他方法来引用非静态内容?

时间:2015-02-06 05:18:15

标签: java android static non-static

好吧,让我说这个程序有效。由于我试图从静态内容中引用CheckScores,我这样做了:

 ScoringActivity scoringActivity = new ScoringActivity();
 scoringActivity.CheckScores(etEnteredScores.getText().toString());

现在,这位于public static class ScoringFragment extends Fragment内,与.class位于同一Activity

我认为最初工作但是在查看了该方法中的日志数量过多之后,事实证明它没有。

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

CheckScores方法(在ScoringActivity.class内)

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)");
    }
}

您可以看到我在调用方法后立即检查requiredAmountrequiredAmount是允许用户拥有的最大分数。现在我之前检查这个变量,它从SQL表中抓取它没有任何问题。

我认为正在发生的事情是,当我调用new ScoringActivity()时,它会重置所有变量。有没有其他方法可以通过CheckScores方法传递数据?

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

0 个答案:

没有答案