Android:函数在线程中不起作用,但在其他情况下可以工作(尝试添加 ProgressDialog)

发布于 2024-10-06 22:08:50 字数 2229 浏览 4 评论 0原文

所以我正在开发一个小应用程序,它可以从网络上抓取漫画并将其显示在我的手机上。没什么花哨的,只是用它来学习诀窍。函数名为comicReload();完成大部分工作,并且在应用程序中多次调用它(第一次运行时,以及当用户单击某些按钮时)。以下是该函数:

public void comicReload () throws IOException {
    //clears some Vectors that have things in them from the last run
    imagetitles.clear();
    imagetext.clear();
    imagelinks.clear();

    //forms the link it will connect to, and connects
    connector = siteBase + Integer.toString(comicNumber);
    doc = Jsoup.connect(connector).get();
    media = doc.select("[src]");

    //gets the required media from the site
    for (Element src : media) {
        if (src.tagName().equals("img"));
            imagetitles.add(src.attr("alt"));
            imagetext.add(src.attr("title"));
            imagelinks.add(src.attr("src"));
    }

    //some variables I could probably get rid of, but it's easy to read
    Integer titlesize = imagetitles.size();
    Integer textsize = imagetext.size();
    Integer linkssize = imagelinks.size();

    comicTitle = (imagetitles.get(titlesize-4));
    hoverText = (imagetext.get(textsize-4));
    comicLink = (imagelinks.get(linkssize-4));

    //gets the picture I am looking for along with the associated text
    URL url = new URL(comicLink);
    InputStream is = (InputStream) url.getContent();
    image = Drawable.createFromStream(is, "src name");

    //finally puts the scraped information into the layout
    comicView.setImageDrawable(image);
    title.setText(comicTitle);
    htext.setText(hoverText);
}

调用时它工作 100% 正常,但在 3G 上有点慢,所以我尝试添加一个 ProgressDialog 以在加载时显示。这是我的尝试(这是我以前只使用 ComicReload(); 运行的代码)

pd = ProgressDialog.show(this, "Getting Comic", "Loading", true, false);
    new Thread(new Runnable() {  
           public void run() {
               try {
                   comicReload();
               } catch (IOException e) {
                   Log.i("ComicReloadFail","Sam");
               }
               pd.dismiss();
               return;
           }
    }).start();

线程本身运行良好,当我放入一些随机代码来执行时try-catch 块与 ComicReload(); 的位置在其中,一切都进展顺利,但是,运行应用程序会导致 ProgressDialog 旋转器在应用程序强制之前旋转几秒钟。是什么导致了这个?为什么在线程中时,comicReload() 停止工作,我只是想要一种在旋转器工作时执行该方法的方法

,我知道这有很多 。读。

So I have a little app I am working on that scrapes comics from the web and displays them on my phone. Nothing fancy, just using it to learn the ropes. The function called comicReload(); does most of the work and it is called many times in the application (on first run, and when the user clicks certain buttons. Here is the function:

public void comicReload () throws IOException {
    //clears some Vectors that have things in them from the last run
    imagetitles.clear();
    imagetext.clear();
    imagelinks.clear();

    //forms the link it will connect to, and connects
    connector = siteBase + Integer.toString(comicNumber);
    doc = Jsoup.connect(connector).get();
    media = doc.select("[src]");

    //gets the required media from the site
    for (Element src : media) {
        if (src.tagName().equals("img"));
            imagetitles.add(src.attr("alt"));
            imagetext.add(src.attr("title"));
            imagelinks.add(src.attr("src"));
    }

    //some variables I could probably get rid of, but it's easy to read
    Integer titlesize = imagetitles.size();
    Integer textsize = imagetext.size();
    Integer linkssize = imagelinks.size();

    comicTitle = (imagetitles.get(titlesize-4));
    hoverText = (imagetext.get(textsize-4));
    comicLink = (imagelinks.get(linkssize-4));

    //gets the picture I am looking for along with the associated text
    URL url = new URL(comicLink);
    InputStream is = (InputStream) url.getContent();
    image = Drawable.createFromStream(is, "src name");

    //finally puts the scraped information into the layout
    comicView.setImageDrawable(image);
    title.setText(comicTitle);
    htext.setText(hoverText);
}

It works 100% fine when it is called, but it's a little slow over 3G so I tried to add a ProgressDialog to show while it loads. This is my attempt (this is the code that runs where I used to just have comicReload();)

pd = ProgressDialog.show(this, "Getting Comic", "Loading", true, false);
    new Thread(new Runnable() {  
           public void run() {
               try {
                   comicReload();
               } catch (IOException e) {
                   Log.i("ComicReloadFail","Sam");
               }
               pd.dismiss();
               return;
           }
    }).start();

The Thread itself runs fine, and when I put some random code to execute in place of the try-catch block with the comicReload(); in it everything goes dandy. The second I put the comicReload() back in there, however, running the app causes the ProgressDialog spinner to spin for a few seconds before the app force closes. What is causing this? And why does comicReload() stop working when it's in a thread. I just want a way to execute that method with a spinner going while it works.

Thanks in advance guys, I know that was a lot to read.

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

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

发布评论

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

评论(2

猫烠⑼条掵仅有一顆心 2024-10-13 22:08:50

您不能在与创建 UI 的线程不同的线程中执行 UI 相关更新。

尝试使用处理程序

public static final Handler handlerVisibility = new Handler() {
    public void handleMessage(Message msg) {
        int visibility = msg.getData().getInt("visibility");
        view.setVisibility(visibility);
    }
};

you can not do UI related updates in a different thread than the one that created the UI.

try using Handler

public static final Handler handlerVisibility = new Handler() {
    public void handleMessage(Message msg) {
        int visibility = msg.getData().getInt("visibility");
        view.setVisibility(visibility);
    }
};
伴随着你 2024-10-13 22:08:50

您不能在工作线程中执行任何与 UI 相关的操作。您需要在 UI 线程上执行此操作。有一些简单的方法可以做到这一点 - 例如,您可以调用 Activity.runOnUiThread()

You can't do anything UI-related in a worker thread. You'll need to do that on the UI thread. There are easy ways to do that - you can call Activity.runOnUiThread(), for example.

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