【问题标题】:How to quit the app from AlertDialog如何从 AlertDialog 退出应用程序
【发布时间】:2012-01-03 21:56:54
【问题描述】:

我正在尝试做一件非常简单的事情,仅使用 1 个按钮显示警报,如果单击,我希望对话框关闭并且应用程序退出\finish()

目前我正在设备上获取通用警报:

应用程序意外停止。请再试一次

在 LogCat 中我得到:无法暂停活动

这是日志:

01-03 14:49:00.670: ERROR/AndroidRuntime(22536): Uncaught handler: thread main exiting due to uncaught exception
01-03 14:49:00.680: ERROR/AndroidRuntime(22536): java.lang.RuntimeException: Unable to pause activity {com.SprintTwo/com.SprintTwo.SprintTwo}: java.lang.NullPointerException
01-03 14:49:00.680: ERROR/AndroidRuntime(22536):     at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3162)
01-03 14:49:00.680: ERROR/AndroidRuntime(22536):     at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3119)
01-03 14:49:00.680: ERROR/AndroidRuntime(22536):     at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3102)
01-03 14:49:00.680: ERROR/AndroidRuntime(22536):     at android.app.ActivityThread.access$2400(ActivityThread.java:119)
01-03 14:49:00.680: ERROR/AndroidRuntime(22536):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1874)
01-03 14:49:00.680: ERROR/AndroidRuntime(22536):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-03 14:49:00.680: ERROR/AndroidRuntime(22536):     at android.os.Looper.loop(Looper.java:123)
01-03 14:49:00.680: ERROR/AndroidRuntime(22536):     at android.app.ActivityThread.main(ActivityThread.java:4363)
01-03 14:49:00.680: ERROR/AndroidRuntime(22536):     at java.lang.reflect.Method.invokeNative(Native Method)
01-03 14:49:00.680: ERROR/AndroidRuntime(22536):     at java.lang.reflect.Method.invoke(Method.java:521)
01-03 14:49:00.680: ERROR/AndroidRuntime(22536):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
01-03 14:49:00.680: ERROR/AndroidRuntime(22536):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
01-03 14:49:00.680: ERROR/AndroidRuntime(22536):     at dalvik.system.NativeStart.main(Native Method)
01-03 14:49:00.680: ERROR/AndroidRuntime(22536): Caused by: java.lang.NullPointerException
01-03 14:49:00.680: ERROR/AndroidRuntime(22536):     at com.phonegap.DroidGap.onPause(DroidGap.java:736)
01-03 14:49:00.680: ERROR/AndroidRuntime(22536):     at com.worklight.androidgap.WLDroidGap.onPause(WLDroidGap.java:163)
01-03 14:49:00.680: ERROR/AndroidRuntime(22536):     at android.app.Activity.performPause(Activity.java:3782)
01-03 14:49:00.680: ERROR/AndroidRuntime(22536):     at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1190)
01-03 14:49:00.680: ERROR/AndroidRuntime(22536):     at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3149)

这是我的代码:

buttonClickListener = new AlertDialog.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                            finish();
                        }

AlertDialog.Builder dlg = new AlertDialog.Builder(context);
    dlg.setTitle(title);
    dlg.setMessage(message);
    dlg.setCancelable(false);
    dlg.setPositiveButton(buttonText, buttonClickListener);
    dlg.create();
    dlg.show();

【问题讨论】:

  • buttonClickListener 在 dlg.setPositiveButton 中使用时为 null ...所以当您单击肯定按钮时,您会遇到 NPE
  • at com.phonegap.DroidGap.onPause(DroidGap.java:736)来自您的代码吗?你确定没有什么奇怪的地方吗?
  • 你能把你在 DroidGap.java 的第 736 行写给我们吗?我也很好奇 WLDroidGap.java 第 163 行的内容。那里一定有一些问题。

标签: android android-alertdialog


【解决方案1】:

尝试 System.exit(0) 而不是 finish()

【讨论】:

  • 这不是关闭 Android 应用程序的正确方法,但对于我非常具体的情况,它有效......谢谢
【解决方案2】:

问题是您无法在暂停模式下完成活动。

想法是用标志来处理它。 在带有标志检查的 Resume() 方法中编写退出代码。在 Dialog 中启用标志。

类似的东西(下面的代码是虚拟的),

boolean APP_EXIT_FLAG = false;
Resume(){

if(APP_EXIT_FLAG)
finish();

}

还有你的警报对话框代码,

buttonClickListener = new AlertDialog.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {

                           APP_EXIT_FLAG = true;
                            dialog.dismiss();
                        }

AlertDialog.Builder dlg = new AlertDialog.Builder(context);
    dlg.setTitle(title);
    dlg.setMessage(message);
    dlg.setCancelable(false);
    dlg.setPositiveButton(buttonText, buttonClickListener);
    dlg.create();
    dlg.show();

希望对你有帮助。

【讨论】:

  • 我应该从哪里调用 Resume() ?
  • 如果您的应用处于暂停模式,它将自动调用 resume(生命周期方法)
【解决方案3】:

试试看吧

private void ConfirmAlert() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("CLOSE");
    builder.setMessage("Do You Want to Close the Application").setCancelable(false)
    .setPositiveButton("Yes",
            new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            dialog.dismiss();
            onYesClick();

        }


    }).setNegativeButton("No",
            new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            dialog.cancel();
            onNoClick();
        }
    });
    AlertDialog alert = builder.create();
    alert.show();
}
private void onYesClick() {
    Intent setIntent = new Intent(Intent.ACTION_MAIN);
    setIntent.addCategory(Intent.CATEGORY_HOME);
    setIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(setIntent);

    ActivityName.this.finish();



}private void onNoClick() {


}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-09
    • 2014-01-18
    • 1970-01-01
    • 1970-01-01
    • 2017-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多