收到错误:android.view.ViewRootImpl$CalledFromWrongThreadException:只有创建视图层次结构的原始线程才能触摸其视图。
我真的不知道为什么。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button generate = (Button) findViewById(R.id.gen);
final TextView dice1 = (TextView) findViewById(R.id.dice1);
final TextView dice2 = (TextView) findViewById(R.id.dice2);
final TextView dice3 = (TextView) findViewById(R.id.dice3);
generate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
dice1.setText(" ");
dice2.setText(" ");
dice3.setText(" ");
Thread thread = new Thread()
{
@Override
public void run() {
try {
while(true) {
sleep(2000);
setText("lol", "lol", "lol");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
thread.start();
}
});
}
public void setText(String d1, String d2, String d3){
dice1.setText(d1);
dice2.setText(d2);
dice3.setText(d3);
}
感谢您的帮助。