如何使用线程在我的Android代码中设置计时器?

时间:2015-03-11 02:04:23

标签: android multithreading timer

我是android新手。 我正在尝试使用线程为我的代码设置计时器,每秒应增加1并将值设置为TextView。但每当我编译我的代码时,它会在“thtim”线程中显示一些例外。

03-11 07:20:41.018: W/dalvikvm(1085): threadid=7: thread exiting with uncaught exception (group=0x4001d800)
03-11 07:20:41.023: E/AndroidRuntime(1085): FATAL EXCEPTION: Thread-8
03-11 07:20:41.023: E/AndroidRuntime(1085): java.lang.NullPointerException
03-11 07:20:41.023: E/AndroidRuntime(1085):     at com.kbt.testing.TestingActivity$1.run(TestingActivity.java:49)
03-11 07:20:44.023: I/Process(1085): Sending signal. PID: 1085 SIG: 9
03-11 07:20:48.610: W/dalvikvm(1095): threadid=7: thread exiting with uncaught exception (group=0x4001d800)
03-11 07:20:48.614: E/AndroidRuntime(1095): FATAL EXCEPTION: Thread-8
03-11 07:20:48.614: E/AndroidRuntime(1095): java.lang.NullPointerException
03-11 07:20:48.614: E/AndroidRuntime(1095):     at com.kbt.testing.TestingActivity$1.run(TestingActivity.java:49)

这些是我面临的错误...请帮我如何设置计时器,并告诉我在我的代码中我做了什么错误.. 非常感谢你的帮助..

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();



}
}

1 个答案:

答案 0 :(得分:0)

问题是您尚未初始化Handler,即您在代码中缺少以下语句:

Handler h=new Handler();

同时查看你的代码我相信如果你使用postDelayed()方法创建线程是不必要的。它会按一定的时间间隔更新文本,用法如下:

 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);