在我第一次从干净状态进入应用程序后,在处理程序内部,处理程序处理 MSG_PULLED 操作,但是对 main 的引用为空。弱引用不为空。这怎么可能发生?
受这篇文章的启发:这个 Handler 类应该是静态的,否则可能会发生泄漏:IncomingHandler
static class MainHandler extends Handler {
private final WeakReference<MainActivity> wMain;
static int angle=0;
public MainHandler(MainActivity main) {
super();
this.wMain = new WeakReference<MainActivity>(main);
}
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
MainActivity main = wMain.get();
int what = msg.what;
if(what == MSG_PULLED) {
main.startAnim();
}
}
}
以及我如何启动处理程序:
static MainHandler mainHandler;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainHandler = new MainHandler(this);
mainHandler.sendEmptyMessageDelayed(MSG_PULLED,500);
}