6分50秒的这个例子老师的能运行,我的运行就报错了
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView= (TextView) findViewById(R.id.id_txt_one);
new Thread(){
public void run(){
try {
Thread.sleep(1000);
handler.post(new Runnable() {
@Override
public void run() {
textView.setText("测试Handler");
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start();
}
照着敲得代码老师的能运行,我的直接停止运行了,报错如下:
E/AndroidRuntime: FATAL EXCEPTION: Thread-174
Process: org.wetao.com.handlerapplication, PID: 3382
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.os.Handler.post(java.lang.Runnable)' on a null object reference
at org.wetao.com.handlerapplication.MainActivity$1.run(MainActivity.java:25)
报错的25行就是 handler.post(new Runnable() 这行