对话框导致应用程序崩溃

时间:2013-12-13 20:29:47

标签: java android dialog memory-leaks

这就是问题,我有一个对话框,只有在用户第一次安装我的应用时才会出现。在使用我自己的设备进行测试后,我发现在弹出框后,什么都不做会导致应用程序崩溃。我的目的是让这个对话框成为用户在安装应用程序后看到的第一件事。它确实出现,但如果用户在大约3秒内没有按下按钮,则会导致应用程序崩溃。但是,如果我在崩溃后重新启动应用程序,那么该框将永久显示,直到用户按下一个按钮,这是我打算进入该框的目标。

这是logcat:

12-13 15:25:28.625: ERROR/WindowManager(15315): Activity com.nick.simplequiz.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{41506ff0 V.E..... R....... 0,0-684,679} that was originally added here
    android.view.WindowLeaked: Activity com.nick.simplequiz.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{41506ff0 V.E..... R....... 0,0-684,679} that was originally added here
    at android.view.ViewRootImpl.<init>(ViewRootImpl.java:458)
    at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:216)
    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:94)
    at android.app.Dialog.show(Dialog.java:286)
    at com.nick.simplequiz.MainActivity.onCreate(MainActivity.java:216)
    at android.app.Activity.performCreate(Activity.java:5165)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1103)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2419)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2520)
    at android.app.ActivityThread.access$600(ActivityThread.java:162)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1366)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:158)
    at android.app.ActivityThread.main(ActivityThread.java:5751)
    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:1083)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:850)
    at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:115)
    at dalvik.system.NativeStart.main(Native Method)

这是根据logcat发生错误的行:

alertDialog.show();

可能导致这种情况发生的原因是什么?

这是onCreate方法中的代码:

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                context);

    alertDialogBuilder.setCancelable(false);
    alertDialogBuilder.setTitle("...."); //Set the title of the box
    alertDialogBuilder.setMessage("....");
    alertDialogBuilder.setPositiveButton("Dismiss", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            dialog.cancel(); //when they click dismiss we will dismiss the box
            SharedPreferences.Editor edit2 = sp.edit();
            edit2.putInt("SHOW", 1);
            edit2.commit();
        }
    });
    alertDialogBuilder.setNegativeButton("", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            .......
        }
    });
    AlertDialog alertDialog = alertDialogBuilder.create(); //create the box
    alertDialog.show(); //*************error happens here

2 个答案:

答案 0 :(得分:3)

错误表示您在退出活动后尝试显示对话框。您需要在活动的dismiss()中的对话框上调用onPause()方法来解决问题

答案 1 :(得分:0)

当您尝试将窗口添加到非现有引用或已杀死的引用时,会发生此问题。泄露的窗口错误是指您已经杀死了显示此弹出窗口的活动,但弹出窗口仍在设备上可见。你必须在杀死活动之前删除弹出窗口。