Android: Function not working in Thread but works otherwise (trying to add ProgressDialog)
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.
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);
}
};
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.
精彩评论