【问题标题】:Error while closing application with alert dialog [closed]使用警报对话框关闭应用程序时出错[关闭]
【发布时间】:2014-06-13 13:23:06
【问题描述】:

Logcat:

06-13 18:25:37.534: E/WindowManager(420): Activity com.dimensionsco.thankbunny.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@43ef3c98 that was originally added here

这是我用来退出带有警报对话框的应用程序的代码。但它最终会出现错误。我不明白我哪里出错了。我在模拟器上运行它。任何人都可以解决这个问题吗?提前致谢

@Override
public void onStop() {
    super.onStop();
    Toast.makeText(getBaseContext(), "stop", Toast.LENGTH_LONG).show();
    AlertDialog.Builder alertDialogBuilde = new AlertDialog.Builder(MainActivity.this);
    alertDialogBuilde.setTitle(this.getTitle() + "EXIT");
    alertDialogBuilde.setMessage("DO you want to exit?");
    AlertDialog alertDialogr = alertDialogBuilde.create();

    alertDialogBuilde.setPositiveButton("Yes", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int id) {

            // go to a new activity of the app
            dialog.cancel();
            // finish();
        }

    });

    alertDialogBuilde.setNegativeButton("No", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int id) {
            dialog.cancel();

        }
    });

    // set neutral button: Exit the app message
    alertDialogBuilde.setNeutralButton("Exit the app", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int id) {
            // exit the app and go to the HOME
            System.exit(0);
            // MainActivity.this.finish();
        }
    });

    alertDialogr.show();
}

【问题讨论】:

  • 请发布logcat
  • 这:System.exit(0); 太可怕了。不要那样做。
  • @323go kk,我会用finish()替换
  • 有人能建议如何在这里发布日志猫数据吗?
  • 复制重要部分,编辑您的问题并将其放入代码块中。并且永远不要使用System.exit(0);,它只会弄乱你的应用程序。

标签: android android-dialog


【解决方案1】:

您无法在onStop() 中创建 UI 或与用户交互。请参考Activity Lifecycle documentation。在执行onStop() 时,该活动已经不可见并且正在被释放,因此无论如何您都不能中断该过程。更糟糕的是,如果您可以在这里进行交互,那么您的finish() 会再次调用onStop()...

如果您需要拦截用户启动的退出,请覆盖 onBackPressed() 并在其中提示您的对话框。

请注意,活动可能会因各种原因(包括来电)而暂停和停止。您当然不希望用户必须确认您的提示才能接听他的电话...

【讨论】:

  • 我明白了,伙计。谢谢。
  • 我这里有另一个错误。现在对话框正在弹出,但对话框中没有“YES”、“No”按钮。
  • AlertDialog alertDialogr = alertDialogBuilde.create();移动到set***Button调用下方和alertDialogr.show()之前
  • 是的,是的,我修好了!谢谢
【解决方案2】:

您从此处的对话框中完成了活动,但警报并未被破坏,因此它会泄漏。您需要先关闭对话框,然后再完成活动。

alertDialogBuilde.setNeutralButton("Exit the app", new DialogInterface.OnClickListener() {

    public void onClick(DialogInterface dialog, int id) {
        // exit the app and go to the HOME
        System.exit(0);
        // MainActivity.this.finish();
    }
});

【讨论】:

    【解决方案3】:

    System.exit(0) 不是关闭活动的好主意,而是调用finish(); System.exit 调用垃圾收集器,然后在您的系统中运行垃圾收集器。所有其他正在运行的进程将在 gc 期间暂停。仅应出于非常必要的原因使用此方法。

    但是,请发布完整的 Logcat 输出,以便这里的人们可以确切地看到发生了什么。也许我们需要查看更多你的代码,才能知道问题出在哪里。

    无论如何,发生此错误的原因可能是您没有在完成之前关闭对话框。所以你必须做到以下几点:

    alertDialogBuilde.setNeutralButton("Exit the app", new DialogInterface.OnClickListener() {
    
        public void onClick(DialogInterface dialog, int id) {
            // exit the app and go to the HOME
            dialog.cancel();
            finish();
            // MainActivity.this.finish();
        }
    });
    

    【讨论】:

    • 这不是对“问题”的回答,而是评论。并且已经被评论了。
    • 那给-1的理由是什么? cmets 不应该与我写的一样长,请在判断我之前通读我的答案
    • 没有人在“评判”。但是,您的回答并没有回答问题。是的,不正确的答案是投反对票的理由。还有什么其他原因?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-11
    • 1970-01-01
    相关资源
    最近更新 更多