如何使用线程在我的android代码中设置计时器?
我是android新手。 我正在尝试使用线程为我的代码设置计时器,该线程应每秒递增1,并将值设置为TextView。但每当我编译代码时,它都会在“thtim”线程中显示一些异常 这些都是我面临的错误。。。请帮助我如何设置计时器,并告诉我我在代码中犯了什么错误。。 非常感谢你的帮助如何使用线程在我的android代码中设置计时器?,android,multithreading,timer,Android,Multithreading,Timer,我是android新手。 我正在尝试使用线程为我的代码设置计时器,该线程应每秒递增1,并将值设置为TextView。但每当我编译代码时,它都会在“thtim”线程中显示一些异常 这些都是我面临的错误。。。请帮助我如何设置计时器,并告诉我我在代码中犯了什么错误。。 非常感谢你的帮助 import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import a
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
public class TestingActivity extends Activity {
Button b;int time=0;
ProgressBar pb,pbo;int clicks=0,i=0,j;
TextView timer;Handler h;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
timer=(TextView) findViewById(R.id.timer);
pb = (ProgressBar) findViewById(R.id.pb);
pbo = (ProgressBar) findViewById(R.id.pbo);
pb.setProgress(0);
pb.setMax(300);
pb.setVisibility(ProgressBar.VISIBLE);
pbo.setProgress(0);
pbo.setMax(300);
pbo.setVisibility(ProgressBar.VISIBLE);
b=(Button) findViewById(R.id.b);
Thread thtim=new Thread(){
public void run()
{
// TODO Auto-generated method stub
for(j=1;j<30;j++){
try {
sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
h.post(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
timer.setText("Timer :"+j);
Log.i("kbt", "inside for new");
}
});
}
}
};
thtim.start();
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
clicks++;
pbo.setProgress(clicks);
}
});
Thread t=new Thread(){
@Override
public void run() {
for(i=-5;i<300;i++)
{
pb.setProgress(i);
try {
sleep(600);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
t.start();
}
}
导入android.app.Activity;
导入android.app.AlertDialog;
导入android.content.DialogInterface;
导入android.content.Intent;
导入android.os.Bundle;
导入android.os.Handler;
导入android.util.Log;
导入android.view.view;
导入android.widget.Button;
导入android.widget.ProgressBar;
导入android.widget.TextView;
公共类测试活动扩展了活动{
按钮b;int time=0;
ProgressBar pb,pbo;int clicks=0,i=0,j;
文本视图定时器;
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
timer=(TextView)findViewById(R.id.timer);
pb=(ProgressBar)findviewbyd(R.id.pb);
pbo=(ProgressBar)findviewbyd(R.id.pbo);
pb.setProgress(0);
pb.setMax(300);
pb.setVisibility(ProgressBar.VISIBLE);
pbo.setProgress(0);
pbo.setMax(300);
pbo.setVisibility(ProgressBar.VISIBLE);
b=(按钮)findViewById(R.id.b);
Thread thtim=新线程(){
公开募捐
{
//TODO自动生成的方法存根
对于(j=1;j而言,问题在于您尚未初始化处理程序,即代码中缺少以下语句:
Handler h=new Handler();
此外,在查看您的代码时,我认为如果使用postdayed()方法,则不必创建线程。它将以一定的间隔更新文本,用法如下:
int j=0;
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
j++;
timer.setText("Timer :"+j);
Log.i("kbt", "inside for new");
}
}, 1000);
你的代码应该做什么?它的哪个部分会产生错误?
int j=0;
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
j++;
timer.setText("Timer :"+j);
Log.i("kbt", "inside for new");
}
}, 1000);