我无法解决此错误: android.view.ViewRootImpl$CalledFromWrongThreadException: 只有创建视图层次结构的原始线程才能触摸其视图。
TextView score;
private SharedPreferences speicher;
private SharedPreferences.Editor editor;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
score = (TextView) findViewById(R.id.score);
speicher = getApplicationContext().getSharedPreferences("Daten", 0);
editor = speicher.edit();
loadfile("score" , score);
new Timer().scheduleAtFixedRate(new TimerTask() {
public void run() {
Integer scorealt = Integer.parseInt(speicher.getString("score", null));
Integer scorenewe = scorealt + Integer.parseInt(speicher.getString("anz", null));
score.setText(scorenewe.toString());
savefile("score", scorenewe.toString());
}
}, 0, 2000);
}
Run Code Online (Sandbox Code Playgroud)
我不能score. score.setText(scorenewe.toString());在第 45 行改变
android.view.ViewRootImpl$CalledFromWrongThreadException:
Only the original thread that created a view hierarchy can touch its views. …Run Code Online (Sandbox Code Playgroud)