Handler更新UI时错误
代码:package com.lianliankan.myholder;
import android.app.Activity;
import android.os.Handler;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView textView;
private Handler handler=new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Thread(){
@Override
public void run() {
try {
Thread.sleep(1000);
handler.post(new Runnable() {
@Override
public void run() {
textView.setText("asdfasdfasdfasdf");
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start();
}
}
错误:
04-11 14:09:45.825 2178-2178/com.lianliankan.myholder E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.lianliankan.myholder, PID: 2178
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.lianliankan.myholder.MainActivity$1$1.run(MainActivity.java:24)