Android线程不工作
在这个代码段中,应用程序会在一段时间内休眠,但它不会将文本追加到textStatus(TextView变量),而是显示一个错误,即出现了一些错误,应用程序正在关闭Android线程不工作,android,multithreading,Android,Multithreading,在这个代码段中,应用程序会在一段时间内休眠,但它不会将文本追加到textStatus(TextView变量),而是显示一个错误,即出现了一些错误,应用程序正在关闭 Thread time_manager = new Thread(){ public void run(){ int interval = 2000; try{ sleep(interval);
Thread time_manager = new Thread(){
public void run(){
int interval = 2000;
try{
sleep(interval);
textStatus.append("\nTEXT\n");
}
catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
}
}
};
我做错了什么部分?对UI部分的所有更改都应该在UIThread上完成,您应该使用类似于
post
或runOnUithread
函数的方法来更新UI。要更新UI,您也可以使用如下处理程序,它将更新您的UI,使计数器的值每秒钟递增1
int response = 0;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView)findViewById(R.id.myid);
thread t = new thread();
t.start();
}
public class thread extends Thread {
public void run() {
while (response < 100) {
handler.sendEmptyMessage(0);
response++;
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
tv.setText("helloworld " + response);
}
};
int响应=0;
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv=(TextView)findViewById(R.id.myid);
螺纹t=新螺纹();
t、 start();
}
公共类线程扩展线程{
公开募捐{
while(响应<100){
handler.sendEmptyMessage(0);
响应++;
试一试{
睡眠(1000);
}捕捉(中断异常e){
e、 printStackTrace();
}
}
}
私有处理程序=新处理程序(){
公共无效handleMessage(消息消息消息){
tv.setText(“helloworld”+响应);
}
};
如果我更改TRY块中语句的顺序,它会显示一次文本,然后休眠,然后给出错误信息