android - resolve 'called from wrong thread' exception -


the purpose of app is: user enters number , clicks button. button uses input calculate fibonacci sequence timer - each number in sequence displaying each second textview. when try run timer calledfromwrongthreadexception. i've posted code below. can tell log statements believe know line causing problem. think it's because i'm calling method outside onclicklistener when move other method around cause more problems.

i've read couple other posts , i'm not sure proper way print text area using method. know how can make work?

public class mainactivity extends activity {  // primary widgets private edittext edittext; private textview textview; private button button1;  static int seconds = 0; static timer timer;  static arraylist<integer> fiblist = new arraylist<integer>();  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);     edittext = (edittext) findviewbyid(r.id.edittext1);     textview = (textview) findviewbyid(r.id.textview2);     button1 = (button) findviewbyid(r.id.button1);      final int delay = 1000;     final int period = 1000;     timer = new timer();      //attempt clear textview     textview.settext("");      button1.setonclicklistener(new onclicklistener() {          @override         public void onclick(view v) {              //clear textview             string array = " ";              fiblist.clear();             textview.settext(array);             //log.i("array", "attempt clear"+fiblist);              string input = edittext.gettext().tostring();             int number = integer.parseint(input);             int tmp = 0;              // confirm input             if (number < 20) {                 toast.maketext(getapplicationcontext(),                         "you entered: " + number, toast.length_long).show();                 (int = 0; <= number; i++) {                     fiblist.add(fib(i));                      // sum numbers                     if (fib(i) % 2 == 0) {                         tmp += fib(i);                      }                  }             } else {                 toast.maketext(getapplicationcontext(),                         "number large: " + number, toast.length_long)                         .show();             }               //i believe error occurs in method               log.i("test", "start timer");              timer.scheduleatfixedrate(new timertask() {                      public void run() {                                    log.i("test", "run timer");                         int nextindex = setinterval();                         log.i("test", "set interval");                           if (nextindex < fiblist.size()) {                               log.i("test", "try print");                                //it looks error occurs here when try print textview                               textview.settext(fiblist.get(nextindex)+ " ");                               log.i("test", "next index"+fiblist.get(nextindex));                               log.i("test", "did print");                           }                     }                 }, delay, period);              log.i("test", "end timer");          }      });  }  // run fibonacci sequence public static int fib(int n) {     if (n < 2) {         return n;     } else {         return fib(n - 1) + fib(n - 2);     } }  //counts every element through array     public static final int setinterval() {          if (seconds >= fiblist.size())             timer.cancel();         return seconds++;      }  @override public boolean oncreateoptionsmenu(menu menu) {     // inflate menu; adds items action bar if present.     getmenuinflater().inflate(r.menu.main, menu);     return true; } 

}

you can use

runonuithread(new runnable(){      public void run(){      textview.settext("aaa");      }  }); 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -