android - AlertDialog 的问题

标签 android

我在创建 AlertDialog 时遇到问题。

我希望当我单击一个按钮时 AlertDialog 出现,所以我将所有用于创建对话框的代码放在该按钮监听器中......相同的代码在该监听器外部工作但在监听器内部不起作用......它不起作用'调用任何错误但是当我启动模拟器时写的是关于这个的:“Aplication has terminated unexpectedly”......

这是我的代码:

btsenddata.setOnClickListener(new Button.OnClickListener() { 

      public void onClick (View v){ 

        AlertDialog.Builder alt_bld = new AlertDialog.Builder(getApplicationContext() ); // I've tried with "activity1.this" instead "getApplicationContext()" but it is the same result
        alt_bld.setMessage("Do you want to close this window ?")
        .setCancelable(false)
        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
        // Action for 'Yes' Button
        }
        })
        .setNegativeButton("No", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
        //  Action for 'NO' Button
        dialog.cancel();
        }
        });
        AlertDialog alert = alt_bld.create();
        // Title for AlertDialog
        alert.setTitle("Title");
        // Icon for AlertDialog
        alert.setIcon(R.drawable.icon);
        alert.show();
      }});
}

....我听说 getApplicationContext() 方法会产生一些问题,但我已尝试用 activity.this 替换它,但无论如何它都不起作用... 如果有人知道这个或更多关于 alertdialogs 的原因,请回答。 谢谢。

最佳答案

"Aplication has terminated unexpectedly"

可能意味着您可以在 LogCat 中查看堆栈跟踪,它告诉您错误的行号。看看那个堆栈跟踪。如果您不明白,请通过编辑您的原始问题将其发布到此处。

关于android - AlertDialog 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2307034/

相关文章:

android - Android 应用程序中自定义 ImageView 的 ClassNotFoundException

android - 检查 GPS 信号

java - 由于相同的删除而导致名称冲突

android - 我的 android 应用程序总是在启动器列表中

android - 无法在android中添加 ScrollView indide tabhost

java - 如何将字符串 "R.drawable.someimage"更改为 id?

android - 安装的 Build Tools 版本 28.0.3 已损坏。如何修复此错误

android - 增加 viewpager smoothscroll 持续时间

android - 如何强制重新安装APK

java - BouncyCaSTLe Provider Jar 导致 Java 堆空间错误