android - why wont animationDrawable run after second click of button -


hi hope can , looking -

i have thread run coin spinning animation this

    public class mainactivity extends activity { static animationdrawable frameanimation;  public boolean currentspin = false; @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);      //is want me     imageview coinanima = (imageview) findviewbyid(r.id.imageview1);       button bt = (button) findviewbyid(r.id.button1);       bt.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view v) {         spincoin1();         }     });  } //end of oncreate  public void spincoin1(){     coinanima = (imageview) findviewbyid(r.id.imageview1);     coinanima.setbackgroundresource(r.anim.coin_spin_heads);           new thread(new runnable() {                     public void run() {                         frameanimation = (animationdrawable) coinanima.getbackground();                         frameanimation.start();                          try {                             thread.sleep(5000);                           } catch (interruptedexception e) {                             e.printstacktrace();                           }                                frameanimation.stop();                      //end of run                     }          //starts thread                  }).start();  //end of method       } //end of class } 

it runs onclick of button, when click button second time causes , error

which

    03-10 23:33:28.804: e/androidruntime(13689): fatal exception: thread-12 03-10 23:33:28.804: e/androidruntime(13689): android.view.viewroot$calledfromwrongthreadexception: original thread created view hierarchy can touch views. 03-10 23:33:28.804: e/androidruntime(13689):    @ android.view.viewroot.checkthread(viewroot.java:2936) 03-10 23:33:28.804: e/androidruntime(13689):    @ android.view.viewroot.invalidatechild(viewroot.java:642) 03-10 23:33:28.804: e/androidruntime(13689):    @ android.view.viewroot.invalidatechildinparent(viewroot.java:668) 03-10 23:33:28.804: e/androidruntime(13689):    @ android.view.viewgroup.invalidatechild(viewgroup.java:2511) 03-10 23:33:28.804: e/androidruntime(13689):    @ android.view.view.invalidate(view.java:5272) 03-10 23:33:28.804: e/androidruntime(13689):    @ android.view.view.invalidatedrawable(view.java:7310) 03-10 23:33:28.804: e/androidruntime(13689):    @ android.widget.imageview.invalidatedrawable(imageview.java:176) 03-10 23:33:28.804: e/androidruntime(13689):    @ android.graphics.drawable.drawable.invalidateself(drawable.java:300) 03-10 23:33:28.804: e/androidruntime(13689):    @ android.graphics.drawable.drawablecontainer.selectdrawable(drawablecontainer.java:227) 03-10 23:33:28.804: e/androidruntime(13689):    @ android.graphics.drawable.animationdrawable.setframe(animationdrawable.java:211) 03-10 23:33:28.804: e/androidruntime(13689):    @ android.graphics.drawable.animationdrawable.nextframe(animationdrawable.java:203) 03-10 23:33:28.804: e/androidruntime(13689):    @ android.graphics.drawable.animationdrawable.run(animationdrawable.java:140) 03-10 23:33:28.804: e/androidruntime(13689):    @ android.graphics.drawable.animationdrawable.start(animationdrawable.java:107) 03-10 23:33:28.804: e/androidruntime(13689):    @ com.example.testanima.mainactivity$2.run(mainactivity.java:61) 03-10 23:33:28.804: e/androidruntime(13689):    @ java.lang.thread.run(thread.java:1019) 

can tell me fix error , again looking

you can't maniuplate gui elements in thread. stated in docs.

you can update gui elements in run() using:

 public void run()  ...   runonuithread(new runnable() {                         @override                         public void run() {                             update gui elements                         }                  });  .... 

also call clear() on animation after first animation has completed. can use more once.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -