【问题标题】:SetText doesn't change the TextView, how can I solve that?SetText 不会更改 TextView,我该如何解决?
【发布时间】:2016-03-31 20:46:17
【问题描述】:

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

    }
}

【问题讨论】:

  • 你在哪里调用 initControls?
  • 我没有在任何地方调用它。我应该吗?
  • 是的!把它放在 onCreate 里面
  • findViewById 不应在 setContentView(ResLayout) 之前调用。永远不要在成员初始化中找到视图。

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


【解决方案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.xml 中查看适当的命名约定。例如,它应该是TextView finalScoreR.id.text_view_103(这也应该反映在您的 xml 布局中。

【讨论】:

    【解决方案2】:

    把你的

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

    在您的 onCreate 方法中。 你在哪里输入SharedPreferences

    【讨论】:

    • 我不能把它放在 onCreate 里面,因为我必须使用一个全局变量来处理 final_score 并使用 setText
    • 使用 public static final String final_score.
    • 不要让你的 textview 静态化。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-15
    • 1970-01-01
    • 1970-01-01
    • 2011-12-27
    • 2021-03-22
    • 2023-01-03
    • 1970-01-01
    相关资源
    最近更新 更多