This Handler class should be static or leaks might occur

当代码中出现“”的异常, 之所以出现这个异常往往是我们在handler里面引用了外部类的变量或者实例,我们可以用下面的方法解决:


static class  StartHander extends Handler {
		WeakReference<Activity> mActivityRef;  

		StartHander (Activity activity) {
			mActivityRef = new WeakReference<Activity>(activity);
		}

		@Override
		public void handleMessage(Message msg) {
			if(msg.what == MSG_START_ACTIVITY) {
				Activity activity = mActivityRef.get();  
				activity.startActivity(new Intent(activity, MainActivity.class));
			}
		}
	};

这样使用 new StartHander(this).sendEmptyMessageDelayed(MSG_START_ACTIVITY, TIME_START_DELAYED); 就可以了, 很简单吧。

你可能感兴趣的:(This Handler class should be static or leaks might occur)