我的应用程序因对话框而崩溃

My app crashed due to Dialog box

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

public void onBackPressed() {
    AlertDialog.Builder builder = new AlertDialog.Builder
            (getApplicationContext(), android.R.style.Theme_DeviceDefault);
    builder.setTitle("Exit").setMessage("Do you really want to Exit ? ")
    .setPositiveButton(" Yes", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    System.exit(1);
                }
            })
    .setNegativeButton("No", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            dialogInterface.dismiss();
        }
    });
    AlertDialog dialog = builder.create();
    dialog.show();
}

当我按下后退按钮时,此应用程序崩溃。

您正在显示带有应用程序上下文的 Dialog。这在 Android 中是不允许的,因为 Dialog 需要一个 Activity 来附加到

更改此行

AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext(), android.R.style.Theme_DeviceDefault);

进入这个

AlertDialog.Builder builder = new AlertDialog.Builder(this, android.R.style.Theme_DeviceDefault);

当您使用 Activity(从代码的外观来看)时,this(第一个参数)指的是您当前的 activity,因此这应该可以解决您的问题

 AlertDialog.Builder builder = new AlertDialog.Builder
                (this, R.style.AppTheme);
        builder.setTitle("Exit").setMessage("Do you really want to Exit ? ").setPositiveButton(" Yes", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                System.exit(1);
            }
        }).setNegativeButton("No", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
            }
        });
        AlertDialog dialog = builder.create();
        dialog.show();

要事第一。 您无法显示带有应用程序上下文的对话框。对话框需要附加到某些 activity,因此需要 Activity 上下文而不是应用程序上下文。

您需要覆盖 Activity's onBackPressed() 方法来处理设备的后退键事件。

你可以这样写:

@Override
public void onBackPressed() {
    confirmBeforeExit();
}

private void confirmBeforeExit() {
    AlertDialog alertDialog = new AlertDialog.Builder(this)
            .setTitle("Exit")
            .setMessage("Do you really want to Exit ? ")
            .setPositiveButton(" Yes", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    finish();
                }
            }).setNegativeButton("No", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    dialogInterface.dismiss();
                }
            }).create();
    alertDialog.show();
}
AlertDialog.Builder builder = new AlertDialog.Builder (this, android.R.style.Theme_DeviceDefault);

通过activity参考