gpt4 book ai didi

java - 当线程完成并调用 dialog.dismiss() 时出现 WindowLeaked 异常;

转载 作者:行者123 更新时间:2023-11-30 11:55:21 26 4
gpt4 key购买 nike

我有一个选项卡布局应用程序,它需要在启动时立即下载一些文件,因此在我调用 onCreate 方法的 Main.java 文件中:

  myProgressDialog = ProgressDialog.show(Controller.this,
"Please wait...", "Doing Extreme Calculations...", true);
downloadFile(NAME_LOCAL, NAME_SERVER, true);

downloadFile 是一个单独的线程,它看起来像这样:

 protected void downloadFile(final String localFilePath, final String remoteFileName, final boolean ASCII) {

// Fire off a thread to do some work that we shouldn't do directly in the UI thread

Thread a = new Thread() {
public void run() {


Logger.setLevel(Level.DEBUG);


try{
//create client
log.info("Creating Client");
ftp = new FileTransferClient();
log.info("Setting Remote Host");
ftp.setRemoteHost(host);
ftp.setUserName(username);
ftp.setPassword(password);
//connect. . .We hope
log.info("Connecting to server " + host);
ftp.connect();
ftp.getAdvancedFTPSettings().setConnectMode(FTPConnectMode.PASV);
if(ASCII){
ftp.setContentType(FTPTransferType.ASCII);
Log.d("ASCII", "USING ASCII");
}else if(!ASCII){
Log.d("BINARY", "USING BINARY");
ftp.setContentType(FTPTransferType.BINARY);

}

ftp.downloadFile(cache + localFilePath , remoteFileName);

}catch (Exception e){
e.printStackTrace();

}





mHandler.post(mUpdateResults);
Handler handler=new Handler();
handler.post(new Runnable(){public void run(){myProgressDialog.dismiss();}});

}
};
a.start();

}

在 log cat 中,它开始下载,然后突然完成,它给了我这个:

02-17 17:27:51.175: ERROR/WindowManager(2523): Activity org.IRE.toolbox.Controller has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@46eba2f0 that was originally added here

02-17 17:27:51.175: ERROR/WindowManager(2523): android.view.WindowLeaked: Activity org.IRE.toolbox.Controller has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@46eba2f0 that was originally added here

这是怎么回事?!提前致谢!

最佳答案

发布的代码将在 Thread.run() 中引发异常:

Handler handler=new Handler();

原因是线程还没有调用Looper.prepare。您可能会看到“强制关闭”提示,如果您深入了解日志,您可能会发现类似以下内容:

ERROR/AndroidRuntime(1161): Uncaught handler: thread Thread-9 exiting due to uncaught exception
ERROR/AndroidRuntime(1161): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
ERROR/AndroidRuntime(1161): at android.os.Handler.<init>(Handler.java:121)

关于java - 当线程完成并调用 dialog.dismiss() 时出现 WindowLeaked 异常;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5036024/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com