08-24 07:33:48.061: E/AndroidRuntime(763): FATAL EXCEPTION: main
08-24 07:33:48.061: E/AndroidRuntime(763): java.lang.NullPointerException
08-24 07:33:48.061: E/AndroidRuntime(763):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1856)
08-24 07:33:48.061: E/AndroidRuntime(763):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-24 07:33:48.061: E/AndroidRuntime(763):  at android.os.Looper.loop(Looper.java:130)
08-24 07:33:48.061: E/AndroidRuntime(763):  at android.app.ActivityThread.main(ActivityThread.java:3683)
08-24 07:33:48.061: E/AndroidRuntime(763):  at java.lang.reflect.Method.invokeNative(Native Method)
08-24 07:33:48.061: E/AndroidRuntime(763):  at java.lang.reflect.Method.invoke(Method.java:507)
08-24 07:33:48.061: E/AndroidRuntime(763):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
08-24 07:33:48.061: E/AndroidRuntime(763):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
08-24 07:33:48.061: E/AndroidRuntime(763):  at dalvik.system.NativeStart.main(Native Method)
线程exception

解决方案 »

  1.   


    import java.text.SimpleDateFormat;
    import java.util.Date;import android.app.Activity;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    import android.widget.TextView;public class TimeDemoActivity extends Activity {
    TextView clockTime;
    private Handler handler;
    SimpleDateFormat formatter = new SimpleDateFormat(
    "yyyy年MM月dd日  HH:mm:ss");

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    clockTime = (TextView) findViewById(R.id.clockTag);
    handler = new Handler() { @Override
    public void handleMessage(Message msg) {
    super.handleMessage(msg);
    clockTime.setText((String)msg.obj);
    /*当单个数据的时候可以mes0.obj方法,如果多个可以用下列Bundle方式....
    Bundle bun = msg.getData();
    String mesg_text = bun.getString("mesg_text");
    clockTime.setText(mesg_text);*/
    } }; new Thread() {
    public void run() {
    while(true){
    init();
    }
    }
    }.start(); } private void init() {
    Date curDate = new Date(System.currentTimeMillis());// 获取当前时间 String text = formatter.format(curDate); Message mes0 = new Message();
    mes0.obj = text;
    handler.sendMessage(mes0);
    /*当单个数据的时候可以mes0.obj方法,如果多个可以用下列Bundle方式....
    Bundle bun0 = new Bundle();
    bun0.putString("mesg_text",  text);
    mes0.setData(bun0);
    handler.sendMessage(mes0);*/ step();
    } private void step() {
    try {
    Thread.sleep(1000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }}  LZ可以参考下~