java - 触发 AlertDialog.Builder 时出现窗口泄漏问题

标签 java android android-alertdialog

我有一个 AlertDialog.Builder,当用户第一次打开应用程序(如果没有网络连接)时,它会触发。 AlertDialogh 作为 NeutralButton,一旦单击就会跳转到 startActivity

我收到泄漏窗口错误。不确定如何解决此问题,因为我无法 .dismiss().cancel() AlertDialog

还有其他方法可以显示此警报吗?

这是在onCreate内部

        if(isNetworkAvailable()){
        checkVersion();
    }else{
         AlertDialog.Builder warning = new AlertDialog.Builder(this);
         warning.setCancelable(false);
         warning.setTitle("Network Connection Not Established");
         warning.setMessage("Data will not be Synchronized. To have data Synchronized close the application and connect to WiFi or Cell Sevice Provider then Reopen.");
         warning.setNeutralButton("OK",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface arg0, int arg1) {
                            findView();
                        }
                    });
         warning.show();
    }

checkVersion() 没有问题,是 findView() 给出了错误

    private void findView(){
    locationId = datasource.selectSettingsLocation();
    if(locationId != 0){
        Intent intent = new Intent(Startup.this, Return.class);
        startActivity(intent);
        this.finish();
    } else {
        Intent intent = new Intent(Startup.this, Main.class);
        startActivity(intent);
        this.finish();
    }
}

else 是本例中触发的内容 Intent Intent = new Intent(Startup.this, Main.class);

最佳答案

I get a leaked window error

您没有发布相关代码,但在进入下一个 Activity 之前,您需要在对话框上调用 dismiss() - 此时当前 Activity 的上下文将 null 这就是你收到这种异常的原因。

因此,就您的情况而言,您可以这样做:

final AlertDialog.Builder warning = new AlertDialog.Builder(this);
...
public void onClick(DialogInterface arg0, int arg1) {
   warning.dismiss();
   findView();
}

只需将对话框设置为最终字段即可在匿名类的方法中使用它。

关于java - 触发 AlertDialog.Builder 时出现窗口泄漏问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23070153/

相关文章:

android - 日期选择器对话框与所选日期不匹配

java - 为什么使用Java Audiosystem的glissando频率会过高

java - 从 php 执行 jar 文件时无法连接到 X11 服务器

android - 获取连接网络的 WiFi 信号强度

android - 如何在onPrepareDialog中设置setSingleChoiceItems的内容?

android - android单声道的消息框

java - 使用文本字段搜索/过滤 TreeView

java - SonarQube 鱿鱼中的误报 :S2583

android - 获取特定类型的所有文件的列表

android - Google 的 Android 应用颜色工具