Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.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
Java 我的应用程序因对话框故障而崩溃_Java_Android_Dialog_Crash - Fatal编程技术网

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

Java 我的应用程序因对话框故障而崩溃,java,android,dialog,crash,Java,Android,Dialog,Crash,当我按下后退按钮时,此应用程序崩溃。您正在显示一个带有应用程序上下文的对话框。这在Android中是不允许的,因为对话框需要一个活动来连接它自己 换行 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void onBackPresse

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

您正在显示一个带有应用程序上下文的
对话框。这在Android中是不允许的,因为
对话框
需要一个
活动
来连接它自己

换行

@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();
}
进入这个

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

当您使用
活动时(从代码外观来看),
(第一个参数)引用您当前的活动,因此这应该首先解决您的问题。

您不能显示带有应用程序上下文的对话框。对话框需要附加到某些活动,因此需要活动上下文而不是应用程序上下文

AlertDialog.Builder builder = new AlertDialog.Builder(this, android.R.style.Theme_DeviceDefault);
您需要重写
活动的
onBackPressed()
方法来处理设备的后退键事件

你可以这样写:

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

传递活动引用

您可以发布崩溃时收到的错误日志吗?您遇到了什么错误?也在此处发布日志。添加错误跟踪或更多信息您可以显示错误日志吗?使用此日志而不是getApplicationContext()代码已经写入活动范围,因此您可以直接编写
而不是
主活动。此
:)当然可以。在这种情况下,我们可以直接使用此日志。
@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);