为什么myThread.handler.sendEmptyMessage(1);这行会报空指针异常啊
package com.example.android_handler_01;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.widget.TextView;
public class MainActivity extends Activity {
private Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
};
};
class MyThread extends Thread{
public Handler handler;
@Override
public void run() {
Looper.prepare();
handler = new Handler(){
@Override
public void handleMessage(Message msg) {
System.out.println("currentThread"+Thread.currentThread());
}
};
Looper.loop();
}
}
private MyThread myThread;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textView = new TextView(this);
textView.setText("hello everyone");
setContentView(textView);
myThread = new MyThread();
myThread.start();
myThread.handler.sendEmptyMessage(1);
}
}