canvas - Android Null Exception on Calling function -


i want change color on button click passing color integer parameter on click android throws error of null pointer exception. , application crashes .. stuck here kindly me please function name setcolor()

   myview class    public class myview extends activity {     button brushlarge;    button brushmeduim;    button brushsmall;    button erase;    button save;    button trash;    button rainbow;    button color1;     button color2;         button color3;      this.color1 = ((button)findviewbyid(r.id.color1));      this.color2 = ((button)findviewbyid(r.id.color2));      this.color3 = ((button)findviewbyid(r.id.color3));       this.color1.setonclicklistener(new view.onclicklistener()       {         public void onclick(view paramview)         {      toast.maketext(getapplicationcontext(), "color1", toast.length_long).show();           //myview.this.coloringview.setcolor("#cd5c5c");         }       });       this.color2.setonclicklistener(new view.onclicklistener()       {         public void onclick(view paramview)         {             //drawview.changepaintcolor(0xffff0000);0);             try             {coloringview.changepaintcolor(0xffff0000);              }             catch(exception e)             {   toast.maketext(getapplicationcontext(), "color1"+e,toast.length_long).show();             }              //coloringview.changepaintcolor(0xffff0000);             }       });       this.color3.setonclicklistener(new view.onclicklistener()       {         public void onclick(view paramview)         {           myview.this.coloringview.setcolor("#fa8072");         }       });       return;     } }    drawview class          public class drawview extends view {     private paint paint = new paint();     private path path = new path();     private paint circlepaint = new paint();     private path circlepath = new path();     canvas mcanvas;     string color = "#ffee22";         public drawview(context context, attributeset attrs) {             super(context, attrs);          paint.setantialias(true);         paint.setcolor(color.green);         paint.setstyle(paint.style.stroke);         paint.setstrokejoin(paint.join.round);         paint.setstrokewidth(15f);          circlepaint.setantialias(true);         circlepaint.setcolor(color.blue);         circlepaint.setstyle(paint.style.stroke);         circlepaint.setstrokejoin(paint.join.miter);         circlepaint.setstrokewidth(4f); mcanvas=new canvas();     }        public void changepaintcolor(int color){             this.paint.setcolor(color);         }      @override     protected void ondraw(canvas canvas) {          canvas.drawpath(path, paint);         canvas.drawpath(circlepath, circlepaint);     }      @override     public boolean ontouchevent(motionevent event) {          // gives x , y coordinates on event.         float pointx = event.getx();         float pointy = event.gety();          // checks event occurs         switch (event.getaction()) {         case motionevent.action_down:             path.moveto(pointx, pointy);              return true;         case motionevent.action_move:             path.lineto(pointx, pointy);             circlepath.reset();              // (circle's center x-coordinate, y-coordinate, radius of             // circle, direction wind shape)             circlepath.addcircle(pointx, pointy, 30, path.direction.cw);             //circlepath.addrect(pointx - 25, pointy - 25, pointx + 25, pointy + 25, path.direction.cw); /*          rectf rect = new rectf(pointx - 25, pointy - 25, pointx + 25, pointy + 25);             circlepath.addroundrect(rect, 0, 0, path.direction.cw); */             break;          case motionevent.action_up:             circlepath.reset();              break;         default:             return false;         }          // schedules repaint.         // force view draw.         postinvalidate();         return true;     } /////function------------------------------------     public void apple()     {     //  paint.setcolor(ap);         //toast.maketext(getcontext(), "rehman", toast.length_short).show();         this.paint.setcolor(0xff0000ff);       }       public void setcolor(string color)     {           this.paint.setcolor(0xff0000ff);         }     public void onsizechange(int paramint)       {        paint.setstrokewidth(paramint);       }       public void clear()       {         if (this.mcanvas != null)         {           this.paint.setargb(255, 255, 255, 255);           mcanvas.drawpaint(this.paint);           invalidate();           paint.setcolor(color.parsecolor(this.color));         }       }  } 

you need override oncreate

   @override    protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);     this.color1 = ((button)findviewbyid(r.id.color1));     this.color2 = ((button)findviewbyid(r.id.color2));     this.color3 = ((button)findviewbyid(r.id.color3));     ...//rest of code     } 

you need set content of layout activity first , intialize views.

also need add drawing view layout.

in oncreate

     linearlayout ll = (linearlayout)findviewbyid(r.layout.linearlayout);      // assuming have linear layout in xml add drawing view lineasrlayout      // can have relative layout      drawingview view = new drawingview(this);      ll.addview(view); 

edit

public class mainactivity extends activity         implements colorpickerdialog.oncolorchangedlistener {      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(new myview(this));       //circlepaint = new paint();         //circlepath = new path();         //circlepaint.setantialias(true);         //circlepaint.setcolor(color.blue);         //circlepaint.setstyle(paint.style.stroke);         //circlepaint.setstrokejoin(paint.join.miter);         //circlepaint.setstrokewidth(4f);          //circlepath.reset();         //circlepath.addcircle(mx, my, 30, path.direction.cw);         mpaint = new paint();         mpaint.setantialias(true);         mpaint.setdither(true);         mpaint.setcolor(0xffff0000);         mpaint.setstyle(paint.style.stroke);         mpaint.setstrokejoin(paint.join.round);         mpaint.setstrokecap(paint.cap.round);         mpaint.setstrokewidth(12);          memboss = new embossmaskfilter(new float[] { 1, 1, 1 },                                        0.4f, 6, 3.5f);          mblur = new blurmaskfilter(8, blurmaskfilter.blur.normal);     }      private paint       mpaint;     private maskfilter  memboss;     private maskfilter  mblur;      public void colorchanged(int color) {         mpaint.setcolor(color);     }      public class myview extends view {          private static final float minp = 0.25f;         private static final float maxp = 0.75f;          private bitmap  mbitmap;         private canvas  mcanvas;         private path    mpath,circlepath;         private paint   mbitmappaint,circlepaint;          public myview(context c) {             super(c);             circlepaint = new paint();             circlepath = new path();             circlepaint.setantialias(true);             circlepaint.setcolor(color.blue);             circlepaint.setstyle(paint.style.stroke);             circlepaint.setstrokejoin(paint.join.miter);             circlepaint.setstrokewidth(4f);               mpath = new path();             mbitmappaint = new paint(paint.dither_flag);         }          @override         protected void onsizechanged(int w, int h, int oldw, int oldh) {             super.onsizechanged(w, h, oldw, oldh);             mbitmap = bitmap.createbitmap(w, h, bitmap.config.argb_8888);             mcanvas = new canvas(mbitmap);         }          @override         protected void ondraw(canvas canvas) {             canvas.drawcolor(0xffaaaaaa);              canvas.drawbitmap(mbitmap, 0, 0, mbitmappaint);              canvas.drawpath(mpath, mpaint);             canvas.drawpath(circlepath, circlepaint);         }          private float mx, my;         private static final float touch_tolerance = 4;          private void touch_start(float x, float y) {             mpath.reset();             mpath.moveto(x, y);             mx = x;             = y;         }         private void touch_move(float x, float y) {             float dx = math.abs(x - mx);             float dy = math.abs(y - my);             if (dx >= touch_tolerance || dy >= touch_tolerance) {                 mpath.quadto(mx, my, (x + mx)/2, (y + my)/2);                 mx = x;                 = y;             }             circlepath.reset();             circlepath.addcircle(mx, my, 30, path.direction.cw);         }         private void touch_up() {             mpath.lineto(mx, my);             // commit path our offscreen             mcanvas.drawpath(mpath, mpaint);             // kill don't double draw             circlepath.reset();             mpath.reset();         }          @override         public boolean ontouchevent(motionevent event) {             float x = event.getx();             float y = event.gety();              switch (event.getaction()) {                 case motionevent.action_down:                     touch_start(x, y);                     invalidate();                     break;                 case motionevent.action_move:                     touch_move(x, y);                     invalidate();                     break;                 case motionevent.action_up:                     touch_up();                     invalidate();                     break;             }             return true;         }     }      private static final int color_menu_id = menu.first;     private static final int emboss_menu_id = menu.first + 1;     private static final int blur_menu_id = menu.first + 2;     private static final int erase_menu_id = menu.first + 3;     private static final int srcatop_menu_id = menu.first + 4;      @override     public boolean oncreateoptionsmenu(menu menu) {         super.oncreateoptionsmenu(menu);          menu.add(0, color_menu_id, 0, "color").setshortcut('3', 'c');         menu.add(0, emboss_menu_id, 0, "emboss").setshortcut('4', 's');         menu.add(0, blur_menu_id, 0, "blur").setshortcut('5', 'z');         menu.add(0, erase_menu_id, 0, "erase").setshortcut('5', 'z');         menu.add(0, srcatop_menu_id, 0, "srcatop").setshortcut('5', 'z');          /****   mechanism extend filter effects?         intent intent = new intent(null, getintent().getdata());         intent.addcategory(intent.category_alternative);         menu.addintentoptions(                               menu.alternative, 0,                               new componentname(this, noteslist.class),                               null, intent, 0, null);         *****/         return true;     }      @override     public boolean onprepareoptionsmenu(menu menu) {         super.onprepareoptionsmenu(menu);         return true;     }      @override     public boolean onoptionsitemselected(menuitem item) {         mpaint.setxfermode(null);         mpaint.setalpha(0xff);          switch (item.getitemid()) {             case color_menu_id:                 new colorpickerdialog(this, this, mpaint.getcolor()).show();                 return true;             case emboss_menu_id:                 if (mpaint.getmaskfilter() != memboss) {                     mpaint.setmaskfilter(memboss);                 } else {                     mpaint.setmaskfilter(null);                 }                 return true;             case blur_menu_id:                 if (mpaint.getmaskfilter() != mblur) {                     mpaint.setmaskfilter(mblur);                 } else {                     mpaint.setmaskfilter(null);                 }                 return true;             case erase_menu_id:                 mpaint.setxfermode(new porterduffxfermode(                                                         porterduff.mode.clear));                 return true;             case srcatop_menu_id:                 mpaint.setxfermode(new porterduffxfermode(                                                     porterduff.mode.src_atop));                 mpaint.setalpha(0x80);                 return true;         }         return super.onoptionsitemselected(item);     } } 

colorpickerdialog

public class colorpickerdialog extends dialog {      public interface oncolorchangedlistener {         void colorchanged(int color);     }      private oncolorchangedlistener mlistener;     private int minitialcolor;      private static class colorpickerview extends view {         private paint mpaint;         private paint mcenterpaint;         private final int[] mcolors;         private oncolorchangedlistener mlistener;          colorpickerview(context c, oncolorchangedlistener l, int color) {             super(c);             mlistener = l;             mcolors = new int[] {                 0xffff0000, 0xffff00ff, 0xff0000ff, 0xff00ffff, 0xff00ff00,                 0xffffff00, 0xffff0000             };             shader s = new sweepgradient(0, 0, mcolors, null);              mpaint = new paint(paint.anti_alias_flag);             mpaint.setshader(s);             mpaint.setstyle(paint.style.stroke);             mpaint.setstrokewidth(32);              mcenterpaint = new paint(paint.anti_alias_flag);             mcenterpaint.setcolor(color);             mcenterpaint.setstrokewidth(5);         }          private boolean mtrackingcenter;         private boolean mhighlightcenter;          @override         protected void ondraw(canvas canvas) {             float r = center_x - mpaint.getstrokewidth()*0.5f;              canvas.translate(center_x, center_x);              canvas.drawoval(new rectf(-r, -r, r, r), mpaint);             canvas.drawcircle(0, 0, center_radius, mcenterpaint);              if (mtrackingcenter) {                 int c = mcenterpaint.getcolor();                 mcenterpaint.setstyle(paint.style.stroke);                  if (mhighlightcenter) {                     mcenterpaint.setalpha(0xff);                 } else {                     mcenterpaint.setalpha(0x80);                 }                 canvas.drawcircle(0, 0,                                   center_radius + mcenterpaint.getstrokewidth(),                                   mcenterpaint);                  mcenterpaint.setstyle(paint.style.fill);                 mcenterpaint.setcolor(c);             }         }          @override         protected void onmeasure(int widthmeasurespec, int heightmeasurespec) {             setmeasureddimension(center_x*2, center_y*2);         }          private static final int center_x = 100;         private static final int center_y = 100;         private static final int center_radius = 32;          private int floattobyte(float x) {             int n = java.lang.math.round(x);             return n;         }         private int pintobyte(int n) {             if (n < 0) {                 n = 0;             } else if (n > 255) {                 n = 255;             }             return n;         }          private int ave(int s, int d, float p) {             return s + java.lang.math.round(p * (d - s));         }          private int interpcolor(int colors[], float unit) {             if (unit <= 0) {                 return colors[0];             }             if (unit >= 1) {                 return colors[colors.length - 1];             }              float p = unit * (colors.length - 1);             int = (int)p;             p -= i;              // p fractional part [0...1) , index             int c0 = colors[i];             int c1 = colors[i+1];             int = ave(color.alpha(c0), color.alpha(c1), p);             int r = ave(color.red(c0), color.red(c1), p);             int g = ave(color.green(c0), color.green(c1), p);             int b = ave(color.blue(c0), color.blue(c1), p);              return color.argb(a, r, g, b);         }          private int rotatecolor(int color, float rad) {             float deg = rad * 180 / 3.1415927f;             int r = color.red(color);             int g = color.green(color);             int b = color.blue(color);              colormatrix cm = new colormatrix();             colormatrix tmp = new colormatrix();              cm.setrgb2yuv();             tmp.setrotate(0, deg);             cm.postconcat(tmp);             tmp.setyuv2rgb();             cm.postconcat(tmp);              final float[] = cm.getarray();              int ir = floattobyte(a[0] * r +  a[1] * g +  a[2] * b);             int ig = floattobyte(a[5] * r +  a[6] * g +  a[7] * b);             int ib = floattobyte(a[10] * r + a[11] * g + a[12] * b);              return color.argb(color.alpha(color), pintobyte(ir),                               pintobyte(ig), pintobyte(ib));         }          private static final float pi = 3.1415926f;          @override         public boolean ontouchevent(motionevent event) {             float x = event.getx() - center_x;             float y = event.gety() - center_y;             boolean incenter = java.lang.math.sqrt(x*x + y*y) <= center_radius;              switch (event.getaction()) {                 case motionevent.action_down:                     mtrackingcenter = incenter;                     if (incenter) {                         mhighlightcenter = true;                         invalidate();                         break;                     }                 case motionevent.action_move:                     if (mtrackingcenter) {                         if (mhighlightcenter != incenter) {                             mhighlightcenter = incenter;                             invalidate();                         }                     } else {                         float angle = (float)java.lang.math.atan2(y, x);                         // need turn angle [-pi ... pi] unit [0....1]                         float unit = angle/(2*pi);                         if (unit < 0) {                             unit += 1;                         }                         mcenterpaint.setcolor(interpcolor(mcolors, unit));                         invalidate();                     }                     break;                 case motionevent.action_up:                     if (mtrackingcenter) {                         if (incenter) {                             mlistener.colorchanged(mcenterpaint.getcolor());                         }                         mtrackingcenter = false;    // draw w/o halo                         invalidate();                     }                     break;             }             return true;         }     }      public colorpickerdialog(context context,                              oncolorchangedlistener listener,                              int initialcolor) {         super(context);          mlistener = listener;         minitialcolor = initialcolor;     }      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         oncolorchangedlistener l = new oncolorchangedlistener() {             public void colorchanged(int color) {                 mlistener.colorchanged(color);                 dismiss();             }         };          setcontentview(new colorpickerview(getcontext(), l, minitialcolor));         settitle("pick color");     } } 

Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -