我在Handler中创建AlertDialog时遇到问题,因为它会导致内存泄漏或其他错误。
我有一个活动,线程和处理程序,其中包含对活动的WeakReference。
在开始我的线程之前,我在我的activity类中创建了ProgressDialog dialog
。
线程任务具有对MyHandler对象的引用。
当用户在我的活动中解除dialog
时,onCancelListener会调用线程的中断()。我的线程安全地完成任务并将Message DOWNLOAD_STATE.CANCELLED发送到主活动线程。然后我用MyHandler
问题是用户在解除alert
(取消线程)之后和创建新AlertDialog
之前立即按下后退按钮(在MyHandler类内)。花了大约一秒钟。当用户在这段时间没有按回时,一切正常。
它在这里泄漏(在MyHandler类中):
new AlertDialog.Builder(activity).setMessage("update cancelled").setPositiveButton("OK", null).show();
正常情况是当用户按下后退按钮时,活动被破坏(活动onBackKeyPressed的效果),但为什么handleMessage没有停止并创建AlertDialog
?
我的处理程序类:
static class MyHandler extends Handler {
WeakReference<MainActivity> activityRef;
MyHandler(MainActivity activity)
{
this.activityRef=new WeakReference<MainActivity>(activity);
}
@Override
public void handleMessage(Message msg) {
MainActivity activity=activityRef.get();
if(activity==null)
return;
if (msg.arg1==DownloadTask.DOWNLOAD_STATE.FINISHED.ordinal())
{
activity.dialog.dismiss();
Toast.makeText(activity, "updated", Toast.LENGTH_LONG).show();
}
else if( msg.arg1== DownloadTask.DOWNLOAD_STATE.CANCELLED.ordinal())
{
new AlertDialog.Builder(activity).setMessage("update cancelled").setPositiveButton("OK", null).show();
}
else if(msg.arg1==DownloadTask.DOWNLOAD_STATE.ERROR.ordinal())
{
activity.dialog.dismiss();
new AlertDialog.Builder(activity).setMessage(activity.getString(R.string.no_connection_info)).setPositiveButton("OK", null).show();
}
}
}
内部线程的可运行:
if(Thread.interrupted())
{
Message msg=handler.obtainMessage();
msg.arg1=DOWNLOAD_STATE.CANCELLED.ordinal();
msg.arg2=counter;
msg.obj=cities.length;
handler.sendMessage(msg);
return;
}
错误堆栈trace1:
android.view.WindowLeaked: Activity com.mycompany.mooz.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@42723848 that was originally added here
at android.view.ViewRootImpl.<init>(ViewRootImpl.java:403)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:311)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
at android.view.Window$LocalWindowManager.addView(Window.java:554)
at android.app.Dialog.show(Dialog.java:277)
at android.support.v7.app.AlertDialog$Builder.show(AlertDialog.java:902)
at com.mycompany.mooz.MainActivity$MyHandler.handleMessage(MainActivity.java:55)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4921)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
at dalvik.system.NativeStart.main(Native Method)
错误堆栈跟踪2:
android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@427329e0 is not valid; is your activity running?
at android.view.ViewRootImpl.setView(ViewRootImpl.java:700)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:345)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
at android.view.Window$LocalWindowManager.addView(Window.java:554)
at android.app.Dialog.show(Dialog.java:277)
at android.support.v7.app.AlertDialog$Builder.show(AlertDialog.java:902)
at com.mycompany.mooz.MainActivity`enter code here`$MyHandler.handleMessage(MainActivity.java:55)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4921)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
活动com.mycompany.mooz.MainActivity已泄露窗口 com.android.internal.policy.impl.PhoneWindow$DecorView@42723848那个 最初是在这里添加的
通常在Activity
暂停且对话框仍在屏幕上时发生。保留对Dialog的引用,如果引用不为null,则在onPause
中调用dismiss并且对话框为isShowing
if (mDialog != null && mDialog.isShowing()) {
mDialog.dismiss();
}
android.view.WindowManager $ BadTokenException:无法添加窗口 - 令牌android.os.BinderProxy@427329e0无效;是你的活动 运行
当您尝试在暂停活动时显示对话框时,通常会发生这种情况。避免检查isFinishing
标志
if (activity != null && !activity.isFinishing()) {
// show dialog
}