java - Method must be called from the UI thread, currently inferred thread is worker -


i trying draw circles in canvas. can on button click, need same when fragment loaded. below fragment code.

public class steptwentyonefragment extends fragment {      private canvasview customcanvas;      @override     public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) {         view v = inflater.inflate(r.layout.step21_fragment, container, false);         customcanvas=(canvasview)v.findviewbyid(r.id.signature_canvas);          final button button1=(button)v.findviewbyid(r.id.step18button1);           float radius=(customcanvas.getcanvaswidth()/2) - ((customcanvas.getcanvaswidth()/2)/100)*60;         new myasynctask(customcanvas).execute();          button1.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view v) {                  if(v.getid()==r.id.step18button1){                      float radius=(customcanvas.getcanvaswidth()/2) - ((customcanvas.getcanvaswidth()/2)/100)*60;                     customcanvas.drawcircle(radius);                     log.d("an_width", "" + customcanvas.getcanvaswidth());                     log.d("an_height" ,""+ customcanvas.getcanvasheight());                      v.setbackgroundresource(r.drawable.button_border_5);                     button1.settextcolor(color.white);                   }             }         });               return v;     }      public static steptwentyonefragment newinstance() {          steptwentyonefragment f = new steptwentyonefragment();         bundle b = new bundle();          f.setarguments(b);          return f;     }       @override     public void setuservisiblehint(boolean isvisibletouser) {         super.setuservisiblehint(isvisibletouser);         if(isvisibletouser) {             activity = getactivity();             if(a != null) a.setrequestedorientation(activityinfo.screen_orientation_portrait);         }     }      public class myasynctask extends asynctask<void, void, void> {          canvasview mytaskview;          myasynctask(canvasview v){             mytaskview = v;         }          @override         protected void doinbackground(void... arg0) {              mytaskview.drawcircle(150);             return null;         }      }  } 

below canvas code

public class canvasview extends view {      public int width;     public int height;     private bitmap mbitmap;     private canvas mcanvas;     private path mpath;     context context;     private paint mpaint;     private float mx, my;     private static final float tolerance = 5;     private int canvasheight, canvaswidth;     private float radius;      public canvasview(context c, attributeset attrs) {         super(c, attrs);         context = c;          mpath = new path();          mpaint = new paint();         mpaint.setstrokewidth(3);         mpaint.setcolor(color.cyan);     }      // override ondraw     @override     protected void ondraw(canvas canvas) {          super.ondraw(canvas);          mcanvas=canvas;          drawable d = getresources().getdrawable(r.drawable.circle_1);          canvasheight= canvas.getheight();         canvaswidth= canvas.getwidth();          log.d("height - "," / "+canvas.getheight());         log.d("width - "," / "+canvas.getwidth());  //         displaymetrics displaymetrics = new displaymetrics(); //        ((activity)context).getwindowmanager().getdefaultdisplay().getmetrics(displaymetrics); //        int height = displaymetrics.heightpixels; //        int width = displaymetrics.widthpixels;          float h=canvasheight/2;         float w=canvaswidth/2;           d.setbounds(0, 0, canvaswidth, canvasheight);         d.draw(canvas);          canvas.drawcircle(w, h, radius, mpaint);     }      public void clear2(){         radius=0;         //important. refreshes view calling ondraw function         invalidate();      }      public void drawcircle(float radius1) {          radius=radius1;         //important. refreshes view calling ondraw function         invalidate();      }      public int getcanvasheight()     {         return canvasheight;     }      public int getcanvaswidth()     {         return canvaswidth;     }  } 

however in fragment, inside myasynctask, shows error method drawcircle must called ui thread, inferred thread worker code line mytaskview.drawcircle(150);

how can fix this?

you can not show on ui thread if implementing in doinbackground(). background task. update ui must implement methods in postexecute().

read more asynctask : https://developer.android.com/reference/android/os/asynctask.html

clear concepts easier use asyntask.

basic structure of asynctask :

private class downloadfilestask extends asynctask<url, integer, long> {      protected long doinbackground(url... urls) {          int count = urls.length;          long totalsize = 0;          (int = 0; < count; i++) {              totalsize += downloader.downloadfile(urls[i]);              publishprogress((int) ((i / (float) count) * 100));              // escape if cancel() called              if (iscancelled()) break;          }          return totalsize;      }       protected void onprogressupdate(integer... progress) {          setprogresspercent(progress[0]);      }       protected void onpostexecute(long result) {          showdialog("downloaded " + result + " bytes");      }  } 

Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -