繁体   English   中英

SetText不会更改TextView,我该如何解决?

[英]SetText doesn't change the TextView, how can I solve that?

我知道已经有一个这样的问题,但是我无法通过答案解决问题,所以我要在这里提出问题,因为我的情况有所不同。 我的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);

    }
}

做这个

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布局中)。

把你的

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM