我有一个 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/