【发布时间】: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