java - Calling from wrong thread exception -


i trying develop application, uses threads implement slideshow. retrieving image path sqlite , displaying them on imageview. problem, got struck is, got confused , unable understand, thread calling images() method, implementing slideshow.

i got logcat follows -

    09-03 13:47:00.248: e/androidruntime(10642): fatal exception: thread-151 09-03 13:47:00.248: e/androidruntime(10642): android.view.viewrootimpl$calledfromwrongthreadexception: original thread created view hierarchy can touch views. 09-03 13:47:00.248: e/androidruntime(10642):    @ android.view.viewrootimpl.checkthread(viewrootimpl.java:5908)  09-03 13:47:00.248: e/androidruntime(10642):    @ com.example.fromstart.mainactivity.images(mainactivity.java:90) 09-03 13:47:00.248: e/androidruntime(10642):    @ com.example.fromstart.mainactivity$2.run(mainactivity.java:59) 09-03 13:47:00.248: e/androidruntime(10642):    @ java.lang.thread.run(thread.java:841) 

mainactivity.java:

public class mainactivity extends activity {     imageview jpgview;     textview tv;      //adapter mdbadapter;        adapter info = new adapter(this);     string path;      handler smhandler = new handler()      {         public void handlemessage(message msg)          {             textview mytextview =              (textview)findviewbyid(r.id.textview1);             mytextview.settext("button pressed");         }     };     @override     protected void oncreate(bundle savedinstancestate)      {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);         jpgview = (imageview)findviewbyid(r.id.imageview1);         tv = (textview) findviewbyid(r.id.textview1);     }     @override     public boolean oncreateoptionsmenu(menu menu)      {          // inflate menu; adds items action bar if present.         getmenuinflater().inflate(r.menu.main, menu);           final runnable runnable = new runnable()          {                  public void run()                   {                             images();                  }         };         int delay = 1000; // delay 1 sec.         int period = 15000; // repeat every 4 sec.         timer timer = new timer();         timer.scheduleatfixedrate(new timertask()          {             public void run()              {                 smhandler.post(runnable);             }         }, delay, period);         thread mythread = new thread(runnable);         mythread.start();         return true; }         public void handlemessage(message msg)          {                          string string = "sample";             textview mytextview = (textview)findviewbyid(r.id.textview1);             mytextview.settext(string);         }         public void images()         {             try             {                  for(int i=0;i<=20;i++)                 {                      path = info.getpath();                 bitmap bitmap = bitmapfactory.decodefile(path);                 jpgview.setimagebitmap(bitmap);                  }             }             catch(nullpointerexception er)             {                 string ht=er.tostring();                 toast.maketext(getapplicationcontext(), ht, toast.length_long).show();             }          }     } 

i newbie android, started working on threads. if find mistakes in code, please point out , please suggest me, right way deal problem.

thanks in advance.

update:

 public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);     final handler mhandler = new handler();      // create runnable posting     runonuithread(new runnable()      {         public void run()          {             images();         }     });     int delay = 1000; // delay 1 sec.     int period = 15000; // repeat every 4 sec.     timer timer = new timer();     timer.scheduleatfixedrate(new timertask()      {         public void run()          {             images();         }     }, delay, period);     }  public void images() {     try     {      toast.maketext(getapplicationcontext(), "1", toast.length_long).show();              path = info.getpath();               toast.maketext(getapplicationcontext(), "2", toast.length_long).show();         bitmap bitmap = bitmapfactory.decodefile(path);         toast.maketext(getapplicationcontext(), "3", toast.length_long).show();         jpgview.setimagebitmap(bitmap);         toast.maketext(getapplicationcontext(), "4", toast.length_long).show();      }     catch(nullpointerexception er)     {             string ht=er.tostring();             toast.maketext(getapplicationcontext(), ht, toast.length_long).show();     }      }  } 

you cannot update/access ui from thread.

you have this

     public void run()       {            images();      } 

and in images have

    jpgview.setimagebitmap(bitmap); 

you need use runonuithread updating ui.

    runonuithread(new runnable() {                      @override                     public void run() {                       //                     }                 }); 

timertask runs on different thread. have use handler updati ui.

you can use handler.

edit:

handler m_handler; runnable m_handlertask ;   m_handler = new handler();    m_handlertask = new runnable() {   @override    public void run() {       // something. call images()      m_handler.postdelayed(m_handlertask, 1000);        }   };  m_handlertask.run(); 

if still wish use timer task use runonuithread

timer.scheduleatfixedrate(new timertask()      {         public void run()          {               runonuithread(new runnable() {                      @override                     public void run() {                       images();                     }                 });           }     }, delay, period); 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -