Unable to update a view from within a new Thread

Put your problem here if it does not fit any of the other categories.

Unable to update a view from within a new Thread

Postby ryank » Thu Jan 31, 2008 7:27 pm

I create a new thread to update an ImageSwitcher but I get this message. "Only the original thread that created a view hierarchy can touch its view."

Do I need to send a message to the parent thread to update the imageswitcher? If so, do you have any code samles on how to do that?

Here is a piece of my code.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. new Thread(r).start();
  3.  
  4. .....
  5.  
  6. ......
  7.  
  8. .....          
  9.  
  10. Runnable r = new Runnable() {
  11.  
  12. public void run() {
  13.  
  14.                         while (!done) {
  15.  
  16.                                 updateImage();
  17.  
  18.                         }
  19.  
  20.                 }
  21.  
  22.         };
  23.  
  24.        
  25.  
  26.         public void updateImage()
  27.  
  28.         {
  29.  
  30.                 try {
  31.  
  32.                         URL url = new URL(mImageURLIds[num]);
  33.  
  34.                         URLConnection conn = url.openConnection();
  35.  
  36.                         conn.setAllowUserInteraction(false);
  37.  
  38.                         conn.setConnectTimeout(1000);
  39.  
  40.                         conn.setReadTimeout(1000);
  41.  
  42.                         conn.connect();
  43.  
  44.                         InputStream is = conn.getInputStream();
  45.  
  46.                         BufferedInputStream bis = new BufferedInputStream(is);
  47.  
  48.                         Bitmap bm = BitmapFactory.decodeStream(bis);
  49.  
  50.                         bis.close();
  51.  
  52.                         is.close();
  53.  
  54.                         BitmapDrawable bmd = new BitmapDrawable(bm);
  55.  
  56.                         mSwitcher.setImageDrawable(bmd);
  57.  
  58.                 } catch (IOException e) {
  59.  
  60.                         Log.e("UpdateImage:::", e.getMessage(), e);
  61.  
  62.                 }              
  63.  
  64.         }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


Thanks for your help!
User avatar
ryank
Developer
Developer
 
Posts: 43
Joined: Mon Jan 28, 2008 3:23 am

Top

Postby plusminus » Thu Jan 31, 2008 9:19 pm

Hello ryank,

:idea: Solution: Taken from the :src: PizzaTimer-Tutorial: (Important is that you get the idea behind :!: ):
:idea: The "problem" is, that for security reasons, only the Thread that created the View is allowed to do sth. with it, like redrawing it with an invalidate();-call. So we need an Object 'in' the GUI-Thread that receives messages that mean like: "Hey 'Main-Thread' update your gui!". We will use a so called Handler for that.

  • There will be a Thread, that sends a message that means "UPDATEYOURGUI" to the Handler.
  • In the GUI-Thread, when the Handler receives the Message("UPDATEYOURGUI") it causes an invalidate(); to all participating Views.
Image

:arrow: If this is still not clear, feel free to ask your question :!:


See the :src: PizzaTimer-Tutorial for actual code :)

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot] and 26 guests