Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/214.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用警报对话框关闭应用程序时出错_Android_Android Dialog - Fatal编程技术网

Android 使用警报对话框关闭应用程序时出错

Android 使用警报对话框关闭应用程序时出错,android,android-dialog,Android,Android Dialog,日志: 这是我用来退出带有警报对话框的应用程序的代码。但结果却是错误的。我不明白我哪里出错了。我正在模拟器上运行它。谁能解决这个问题?提前谢谢 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

日志:

这是我用来退出带有警报对话框的应用程序的代码。但结果却是错误的。我不明白我哪里出错了。我正在模拟器上运行它。谁能解决这个问题?提前谢谢

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

System.exit0不是关闭活动的好主意,而是调用finish;System.exit调用垃圾回收器,然后,除了垃圾回收器之外,其他什么都不会在系统中运行。所有其他正在运行的进程将在gc期间暂停。这种方法只能用于非常必要的原因

但是,请发布完整的Logcat输出,以便这里的用户能够准确地看到发生了什么。也许我们需要更多地查看您的代码,以了解问题所在

无论如何,发生此错误的原因可能是,您没有在完成之前关闭对话框。因此,您必须执行以下操作:

@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();
}

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

如果需要拦截用户发起的退出,请覆盖并提示其中的对话框


请记住,活动可能因各种原因而暂停或停止,包括来电。您当然不希望用户必须确认您的提示才能接听他的电话……

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

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();
    }
});

请发布日志:System.exit0;太可怕了。不要这样做。@323go kk,我将用Finish替换。有人能建议如何在此处发布日志cat数据吗?复制重要部分,编辑您的问题,并将其放入代码块中。并且永远不要使用系统。exit0;,这只会弄乱你的应用程序。这不是对问题的回答,而是一条评论。这已经被评论过了。那么给出-1的原因是什么?评论不应该像我写的那样长,请在评判meNobody评判您之前完整阅读我的答案。然而,你的答案并不能回答这个问题。是的,不正确的答案是否决投票的理由。还有什么别的原因吗?我明白了伙计。谢谢你。我这里还有一只虫子。现在对话框弹出,但是的,对话框中没有按钮。移动AlertDialog alertDialogr=alertDialogBuilde.create;要在下面设置***按钮调用和之前的alertDialogr.showyes,yes我修复了它!非常感谢。
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();
    }
});