当线程完成并调用dialog.dissmiss()时,WindowLeaked异常;

发布于 2024-10-17 20:22:52 字数 2733 浏览 7 评论 0原文

我有一个选项卡布局应用程序,需要在启动时下载一些文件,因此在 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

发生了什么事?! 提前致谢!

I have a tab layout app that needs to download a few files right when it starts, so in the Main.java file in the onCreate method I call:

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

downloadFile is a separate thread it looks likes this:

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

        }

In log cat it starts the download, and it finishes then all the sudden it gives me this:

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

Whats going on?!
Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

度的依靠╰つ 2024-10-24 20:22:52

发布的代码将在 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)

The code posted is going to raise an exception in the Thread.run() at:

Handler handler=new Handler();

The reason is that the thread has not called Looper.prepare. You probably saw a "Force Close" prompt and if you dig through your logs, you'll probably find something like:

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文