SetText没有改变TextView,我该如何解决?

时间:2016-03-31 20:46:18

标签: android android-layout android-fragments android-studio text

我知道已经存在这样的问题了,但是我的答案无法解决我的问题,所以我会在这里问一下,因为我的情况不同。我的TextView不会改变。我该如何解决?

package com.example.moresche.englishqigame;

public class scoreActivity extends AppCompatActivity {

    int finalscorex;
    int x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15,x16,x17,x18,x19,x20,x21,x22,x23,x24,x25,x26,x27,x28,x29,x30;
    int x31,x32,x33,x34,x35,x36,x37,x38,x39,x40,x41,x42,x43,x44,x45,x46,x47,x48,x49,x50;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_score);
    }

    public void OnStart(){
        super.onStart();
    }

    TextView final_score = (TextView) findViewById(R.id.textView103);

    public void initControls() {

        final SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(this);

        int x1 = app_preferences.getInt("score1", 0);
        int x2 = app_preferences.getInt("score2", 0);
        int x3 = app_preferences.getInt("score3", 0);
        ...
        int x48 = app_preferences.getInt("score4", 0);
        int x49 = app_preferences.getInt("score49", 0);
        int x50 = app_preferences.getInt("score50", 0);

        finalscorex = x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 + x10 + x11 + x12 + x13 + x14 + x15 + x16 + x17 + x18 + x19 + x20
            + x21 + x22 + x23 + x24 + x25 + x26 + x27 + x28 + x29 + x30 + x31 + x32 + x33 + x34 + x35 + x36 + x37 + x38 + x38 + x40
            + x41 + x42 + x43 + x44 + x45 + x46 + x47 + x48 + x49 + x50;

        final_score.setText(finalscorex);

    }
}

2 个答案:

答案 0 :(得分:1)

这样做

package com.example.moresche.englishqigame;

public class scoreActivity extends AppCompatActivity {

    int finalscorex;
    int[] x = new int[50];

    TextView final_score;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_score);
        final_score = (TextView) findViewById(R.id.textView103);
        initControls();
    }

    public void initControls() {

        final SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(this);

        String key;
        for(int i = 0; i <= x.length; i++){
            key = "score" + (i+1);
            x[i] = app_preferences.getInt(key, 0);
            finalscorex = finalScore + x[i];
        }

        final_score.setText(finalscorex);

    }
}

话虽如此,您还应该在android中查看适当的命名约定。例如,它应该是TextView finalScoreR.id.text_view_103(这也应该反映在您的xml布局中。

答案 1 :(得分:0)

点亮你的

TextView final_score = (TextView) findViewById(R.id.textView103);

onCreate方法内部。 您放置SharedPreferences的方法在哪里?