[英]SetText doesn't change the TextView, how can I solve that?
I know that there is already a question like this, but I couldn't solve my problem with its answer, so I'm gonna ask it here, because my situation is different. 我知道已经有一个这样的问题,但是我无法通过答案解决问题,所以我要在这里提出问题,因为我的情况有所不同。 My TextView doens't change.
我的TextView不变。 How can I solve it?
我该如何解决?
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);
}
}
Do this 做这个
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);
}
}
That being said, you should also look into appropriate naming conventions in android. 话虽如此,您还应该研究android中的适当命名约定。 For example, it should be
TextView finalScore
and R.id.text_view_103
(which should also be reflected in your xml layout. 例如,应为
TextView finalScore
和R.id.text_view_103
(也应反映在您的xml布局中)。
Put your 把你的
TextView final_score = (TextView) findViewById(R.id.textView103);
inside of your onCreate
method. 在您的
onCreate
方法中。 Where is your method for puting SharedPreferences
? 您放置
SharedPreferences
的方法在哪里?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.