java - App Crashes When Saving Shared Preference for a CustomView -


trying implement movable custom view crop view specify area of detection in motion detection application. bitmaps motion detection should created according height , width of custom view user sets it. view inflating properly, app crashes custom view moved. whether has way state of box saved preference when moved? changes should made in code?

logcat

    08-15 10:34:00.166 5884-6969/com.md.areadectest i/motiondetectionactivity: saving.. previous=android.graphics.bitmap@e8b4fcb original=android.graphics.bitmap@5a303a8 bitmap=android.graphics.bitmap@d75eec1 08-15 10:34:00.279 5884-5884/com.md.areadectest d/viewrootimpl@47122fb[motiondetectionactivity]: viewpostimeinputstage processpointer 1 08-15 10:34:00.284 5884-5884/com.md.areadectest e/inputeventreceiver: exception dispatching input event. 08-15 10:34:00.284 5884-5884/com.md.areadectest e/messagequeue-jni: exception in messagequeue callback: handlereceivecallback 08-15 10:34:00.291 5884-5884/com.md.areadectest e/messagequeue-jni: java.lang.nullpointerexception: attempt invoke virtual method 'java.lang.string android.content.context.getpackagename()' on null object reference                                                                         @ android.preference.preferencemanager.getdefaultsharedpreferencesname(preferencemanager.java:499)                                                                         @ android.preference.preferencemanager.getdefaultsharedpreferences(preferencemanager.java:488)                                                                         @ com.md.areadectest.sharedprefsutils.setstringpreference(sharedprefsutils.java:43)                                                                         @ com.md.areadectest.areadetectorview.ontouchevent(areadetectorview.java:357)                                                                         @ android.view.view.dispatchtouchevent(view.java:10731)                                                                         @ android.view.viewgroup.dispatchtransformedtouchevent(viewgroup.java:2859)                                                                         @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:2535)                                                                         @ android.view.viewgroup.dispatchtransformedtouchevent(viewgroup.java:2865)                                                                         @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:2550)                                                                         @ android.view.viewgroup.dispatchtransformedtouchevent(viewgroup.java:2865)                                                                         @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:2550)                                                                         @ android.view.viewgroup.dispatchtransformedtouchevent(viewgroup.java:2865)                                                                         @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:2550)                                                                         @ android.view.viewgroup.dispatchtransformedtouchevent(viewgroup.java:2865)                                                                         @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:2550)                                                                         @ android.view.viewgroup.dispatchtransformedtouchevent(viewgroup.java:2865)                                                                         @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:2550)                                                                         @ android.view.viewgroup.dispatchtransformedtouchevent(viewgroup.java:2865)                                                                         @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:2550)                                                                         @ com.android.internal.policy.decorview.superdispatchtouchevent(decorview.java:509)                                                                         @ com.android.internal.policy.phonewindow.superdispatchtouchevent(phonewindow.java:1863)                                                                         @ android.app.activity.dispatchtouchevent(activity.java:3226)                                                                         @ com.android.internal.policy.decorview.dispatchtouchevent(decorview.java:471)                                                                         @ android.view.view.dispatchpointerevent(view.java:10960)                                                                         @ android.view.viewrootimpl$viewpostimeinputstage.processpointerevent(viewrootimpl.java:5075)                                                                         @ android.view.viewrootimpl$viewpostimeinputstage.onprocess(viewrootimpl.java:4927)                                                                         @ android.view.viewrootimpl$inputstage.deliver(viewrootimpl.java:4458)                                                                         @ android.view.viewrootimpl$inputstage.ondelivertonext(viewrootimpl.java:4511)                                                                         @ android.view.viewrootimpl$inputstage.forward(viewrootimpl.java:4477)                                                                         @ android.view.viewrootimpl$asyncinputstage.forward(viewrootimpl.java:4610)                                                                         @ android.view.viewrootimpl$inputstage.apply(viewrootimpl.java:4485)                                                                         @ android.view.viewrootimpl$asyncinputstage.apply(viewrootimpl.java:4667)                                                                         @ android.view.viewrootimpl$inputstage.deliver(viewrootimpl.java:4458)                                                                         @ android.view.viewrootimpl$inputstage.ondelivertonext(viewrootimpl.java:4511)                                                                         @ android.view.viewrootimpl$inputstage.forward(viewrootimpl.java:4477)                                                                         @ android.view.viewrootimpl$inputstage.apply(viewrootimpl.java:4485)                                                                         @ android.view.viewrootimpl$inputstage.deliver(viewrootimpl.java:4458)                                                                         @ android.view.viewrootimpl.deliverinputevent(viewrootimpl.java:6960)                                                                         @ android.view.viewrootimpl.doprocessinputevents(viewrootimpl.java:6899)                                                                         @ android.view.viewrootimpl.enqueueinputevent(viewrootimpl.java:6860)                                                                         @ android.view.viewrootimpl$windowinputeventreceiver.oninputevent(viewrootimpl.java:7070)                                                                         @ android.view.inputeventreceiver.dispatchinputevent(inputeventreceiver.java:185)                                                                         @ android.os.messagequeue.nativepollonce(native method)                                                                         @ android.os.messagequeue.next(messagequeue.java:323)                                                                         @ android.os.looper.loop(looper.java:136)                                                                         @ android.app.activitythread.main(activitythread.java:6692)                                                                         @ java.lang.reflect.method.invoke(native method)                                                                         @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1468)                                                                         @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1358) 08-15 10:34:00.292 5884-5884/com.md.areadectest d/androidruntime: shutting down vm 08-15 10:34:00.294 5884-5884/com.md.areadectest e/androidruntime: fatal exception: main                                                                   process: com.md.areadectest, pid: 5884                                                                   java.lang.nullpointerexception: attempt invoke virtual method 'java.lang.string android.content.context.getpackagename()' on null object reference                                                                       @ android.preference.preferencemanager.getdefaultsharedpreferencesname(preferencemanager.java:499)                                                                       @ android.preference.preferencemanager.getdefaultsharedpreferences(preferencemanager.java:488)                                                                       @ com.md.areadectest.sharedprefsutils.setstringpreference(sharedprefsutils.java:43)                                                                       @ com.md.areadectest.areadetectorview.ontouchevent(areadetectorview.java:357)                                                                       @ android.view.view.dispatchtouchevent(view.java:10731)                                                                       @ android.view.viewgroup.dispatchtransformedtouchevent(viewgroup.java:2859)                                                                       @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:2535)                                                                       @ android.view.viewgroup.dispatchtransformedtouchevent(viewgroup.java:2865)                                                                       @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:2550)                                                                       @ android.view.viewgroup.dispatchtransformedtouchevent(viewgroup.java:2865)                                                                       @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:2550) 

areadetectorview.java

public class  areadetectorview extends linearlayout {  public static int width; public static int height;  private static paint boxpaint = null; private static paint textpaint = null; private static paint arrowpaint = null; private static path mpath = null; private static rect mrect = null; private static int lastx, lasty = 0; private static boolean mboxtouched = false; private static boolean marrowtouched = false; private static context mcontext; private static int arrowwidth = 0; private static paint boxpaint2 = null;  public areadetectorview(context context) {     super(context);     mcontext = context; } //attrs not there public areadetectorview(context context, attributeset attrs) {     super(context,attrs);     mcontext = context;     // todo auto-generated constructor stub     if (!this.getrootview().isineditmode()) {         arrowwidth =getdisplaypixel(context, 30);     }      //initdetectionarea();      initmembervariables();     setwillnotdraw(false); } public static int getdisplaypixel(context paramcontext, int paramint) {     return (int)(paramint * paramcontext.getresources().getdisplaymetrics().density + 0.5f); }  public static void initmembervariables() {     if (boxpaint == null) {         boxpaint = new paint();         boxpaint.setantialias(true);         boxpaint.setstrokewidth(2.0f);         //boxpaint.setstyle(style.stroke);         boxpaint.setstyle(style.fill_and_stroke);         boxpaint.setcolor(contextcompat.getcolor(mcontext, r.color.bwff_60));     }     if (arrowpaint == null) {         arrowpaint = new paint();         arrowpaint.setantialias(true);         arrowpaint.setcolor(contextcompat.getcolor(mcontext,r.color.reddd));         arrowpaint.setstyle(style.fill_and_stroke);     }     if (textpaint == null) {         textpaint = new paint();         textpaint.setcolor(contextcompat.getcolor(mcontext,r.color.yellowl));         textpaint.settextsize(16);         //txtpaint.settypeface(lcd);         textpaint.setstyle(style.fill_and_stroke);     }     if (mpath == null) {         mpath = new path();     } else {         mpath.reset();     }     if (mrect == null) {         mrect = new rect();     }      if (boxpaint2 == null) {         boxpaint2 = new paint();         boxpaint2.setantialias(true);         boxpaint2.setstrokewidth(2.0f);         //boxpaint.setstyle(style.stroke);         boxpaint2.setstyle(style.stroke);         boxpaint2.setcolor(contextcompat.getcolor(mcontext,r.color.bwff_9e));     }  }  public static void initdetectionarea() {     try {         int w = preferences.detectionarea.width();         int h = preferences.detectionarea.height();         int x = preferences.detectionarea.left;         int y = preferences.detectionarea.top;          // ver 2.6.0         if (preferences.detectionarea.left == 1                 && preferences.detectionarea.top == 1                 && preferences.detectionarea.right == 1                 && preferences.detectionarea.bottom == 1) {              w = preferences.displaywidth / 4;             h = preferences.displayheight / 3;              // ver 2.5.9             w = width / 4;             h = height / 3;              preferences.detectorwidth = w;    //utilgeneralhelper.getdisplaypixel(this, 100);             preferences.detectorheight = h;   //utilgeneralhelper.getdisplaypixel(this, 100);              x = (preferences.displaywidth / 2) - (w / 2);             y = (preferences.displayheight / 2) - (h / 2);              // ver 2.5.9             x = (width / 2) - (w / 2);             y = (height / 2) - (h / 2);          }          //preferences.detectionarea = new rect(x, x, x + preferences.detectorwidth, x + preferences.detectorheight);         preferences.detectionarea = new rect(x, y, x + w, y + h);          preferences.gdetectionbitmapint = new int[preferences.detectionarea.width() * preferences.detectionarea.height()];         preferences.gdetectionbitmapintprev = new int[preferences.detectionarea.width() * preferences.detectionarea.height()];      } catch (exception e) {         e.printstacktrace();     } }  public static void setdetectionarea(int x, int y, int w, int h) {     try {         preferences.detectionarea = new rect(x, y, w, h);      } catch (exception e) {         e.printstacktrace();     } }  private void drawareabox(canvas canvas) {     try {     } catch (exception e) {         e.printstacktrace();     } }  @override protected void dispatchdraw(canvas canvas) {     try {         if (this.getrootview().isineditmode()) {             super.dispatchdraw(canvas);             return;         }          //canvas.save(canvas.matrix_save_flag);         //preferences.detectionareaorient = utilgeneralhelper.getdetectrectbyorientation();          canvas.drawcolor(0);         mpath.reset();          canvas.drawrect(preferences.detectionarea, boxpaint);          mpath.moveto(preferences.detectionarea.right - arrowwidth, preferences.detectionarea.bottom);         mpath.lineto(preferences.detectionarea.right, preferences.detectionarea.bottom - arrowwidth);         mpath.lineto(preferences.detectionarea.right, preferences.detectionarea.bottom);         mpath.lineto(preferences.detectionarea.right - arrowwidth, preferences.detectionarea.bottom);         mpath.close();         canvas.drawpath(mpath, arrowpaint);          mpath.reset();         //canvas.drawrect(preferences.detectionareaorient, boxpaint2);         //canvas.drawrect(preferences.detectionareaorientport, boxpaint2);          textpaint.settextsize(16);         //textpaint.setletterspacing(2);          textpaint.setcolor(contextcompat.getcolor(mcontext,r.color.bwff));          textpaint.gettextbounds(getresources().getstring(r.string.str_detectarea), 0, 1, mrect);         canvas.drawtext(getresources().getstring(r.string.str_detectarea),                 preferences.detectionarea.left + 4,                 preferences.detectionarea.top + 4 + mrect.height(),                 textpaint);         int rech = mrect.height();          textpaint.setstrokewidth(1.2f);         textpaint.settextsize(18);         textpaint.setcolor(contextcompat.getcolor(mcontext,r.color.redd_9e));         textpaint.gettextbounds(getresources().getstring(r.string.str_dragandmove), 0, 1, mrect);         canvas.drawtext(getresources().getstring(r.string.str_dragandmove),                 preferences.detectionarea.left + 4,                 preferences.detectionarea.top + 20 + mrect.height()*2,                 textpaint);          textpaint.gettextbounds(getresources().getstring(r.string.str_scalearea), 0, 1, mrect);         canvas.drawtext(getresources().getstring(r.string.str_scalearea),                 preferences.detectionarea.left + 4,                 preferences.detectionarea.top + 36 + mrect.height()*3,                 textpaint);          super.dispatchdraw(canvas);         //canvas.restore();     } catch (exception e) {         e.printstacktrace();     } }  @override protected void ondraw(canvas canvas) {     try {         super.ondraw(canvas);         invalidate();     } catch (exception e) {         e.printstacktrace();     } {      } }  @override public boolean ontouchevent(motionevent event) {     boolean retvalue = true;     int x = (int)event.getx();     int y = (int)event.gety();      //appmain.txtloc.settext(string.valueof(x) + ", " + string.valueof(y));      switch (event.getaction()) {         case motionevent.action_down:             mboxtouched = touchedinboxarea(x, y);              //appmain.txtloc.settext("boxtouched: " + string.valueof(mboxtouched));              if (!mboxtouched) break;              lastx = x;             lasty = y;              boxpaint.setstyle(style.fill_and_stroke);             boxpaint.setcolor(contextcompat.getcolor(mcontext,r.color.redd_9e));              marrowtouched = touchedinarrow(x, y);             //appmain.txtloc.settext("arrowtouched: " + string.valueof(mboxtouched));              if (marrowtouched) {                 arrowpaint.setcolor(contextcompat.getcolor(mcontext,r.color.bwff_9e));             }              break;          case motionevent.action_move:             if (!mboxtouched) break;              int movex = x - lastx;             int movey = y - lasty;              //appmain.txtloc.settext("move x, y: " + string.valueof(movex) + "," + string.valueof(movey));             if (!marrowtouched) {                 if (preferences.detectionarea.left + movex < 0) {                     break;                 } //              if (preferences.detectionarea.right + movex > preferences.gdisplay.getwidth()) { //                  break; //              }                 // ver 2.5.9                 if (preferences.detectionarea.right + movex > width) {                     break;                 }                 if (preferences.detectionarea.top + movey < 0) {                     break;                 } //              if (preferences.detectionarea.bottom + movey > preferences.gdisplay.getheight()) { //                  break; //              }                 // ver 2.5.9                 if (preferences.detectionarea.bottom + movey > height) {                     break;                 }             }              if (marrowtouched) {                 if ((preferences.detectionarea.width() + movex) < arrowwidth * 2){                     break;                 }                 if ((preferences.detectionarea.height() + movey) < arrowwidth * 2) {                     break;                 }                 preferences.detectionarea.right += movex;                 preferences.detectionarea.bottom += movey;                 //log.i("dbg", "w,h: " + string.valueof(preferences.detectionarea.width()) + "," + string.valueof(preferences.detectionarea.height()));             } else {                 preferences.detectionarea.left += movex;                 preferences.detectionarea.right += movex;                 preferences.detectionarea.top += movey;                 preferences.detectionarea.bottom += movey;             }              lastx = x;             lasty = y;              //appmain.txtloc.settext(string.valueof(preferences.detectionarea.left) + ", " + string.valueof(preferences.detectionarea.top));             break;          case motionevent.action_up:             mboxtouched = false;             marrowtouched = false;             //boxpaint.setstyle(style.stroke);             boxpaint.setstyle(style.fill_and_stroke);             boxpaint.setcolor(contextcompat.getcolor(mcontext,r.color.bwff_60));             arrowpaint.setcolor(contextcompat.getcolor(mcontext,r.color.reddd));              //appmain.txtloc.settext(string.valueof(preferences.detectionarea.left) + ", " + string.valueof(preferences.detectionarea.top));              if (preferences.detectionarea.left < 0) {                 preferences.detectionarea.left = 0;             } //          if (preferences.detectionarea.right > preferences.gdisplay.getwidth()) { //              preferences.detectionarea.right = preferences.gdisplay.getwidth(); //          }             // ver 2.5.9             if (preferences.detectionarea.right > width) {                 preferences.detectionarea.right = width;             }             if (preferences.detectionarea.top < 0) {                 preferences.detectionarea.top = 0;             } //          if (preferences.detectionarea.bottom > preferences.gdisplay.getheight()) { //              preferences.detectionarea.bottom = preferences.gdisplay.getheight(); //          }             if (preferences.detectionarea.bottom > height) {                 preferences.detectionarea.bottom = height;             }              preferences.gdetectionbitmapint = new int[preferences.detectionarea.width() * preferences.detectionarea.height()];             preferences.gdetectionbitmapintprev = new int[preferences.detectionarea.width() * preferences.detectionarea.height()];             //preferences.gdetectionbitmapint = null;             //preferences.gdetectionbitmapintprev = null;              string area = string.valueof(preferences.detectionarea.left)                     + "," + string.valueof(preferences.detectionarea.top)                     + "," + string.valueof(preferences.detectionarea.right)                     + "," + string.valueof(preferences.detectionarea.bottom);             // utilgeneralhelper.savepreferencesetting(preferences.gcontext, preferences.pref_detection_area_key, area); sharedprefsutils.setstringpreference(prefe.gcontext, prefe.pref_detection_area_key, area);               break;     }       invalidate();     return retvalue; }  private boolean touchedinboxarea(int x, int y) {     boolean retvalue = false;     try {          if (x > preferences.detectionarea.left && x < preferences.detectionarea.right) {             if (y > preferences.detectionarea.top && y < preferences.detectionarea.bottom) {                 retvalue = true;             }         }     } catch (exception e) {         e.printstacktrace();     }     return retvalue; }  private boolean touchedinarrow(int x, int y) {     boolean retvalue = false;     try {          if (x > preferences.detectionarea.right - arrowwidth && x < preferences.detectionarea.right) {             if (y > preferences.detectionarea.bottom - arrowwidth && y < preferences.detectionarea.bottom) {                 retvalue = true;             }         }     } catch (exception e) {         e.printstacktrace();     }     return retvalue; }  @override protected void onmeasure(int widthmeasurespec, int heightmeasurespec) {     super.onmeasure(widthmeasurespec, heightmeasurespec);     int width = measurespec.getsize(widthmeasurespec);     int height = measurespec.getsize(heightmeasurespec);     setmeasureddimension(width, height);     width = width;     height = height;     initdetectionarea(); }  @override protected void onfinishinflate() {     super.onfinishinflate(); }  @override protected void onlayout(boolean changed, int l, int t, int r, int b) {     // todo auto-generated method stub     (int = 0; < this.getchildcount()-1; i++){         (this.getchildat(i)).layout(l, t, r, b);     }      if (changed) {         // check width height         if (r != width || b != height) {             // size not match         }     } } } 

prefe.java

public class prefe extends application{ .... public static rect detectionarea; public static int detectorheight = 0; public static int detectorwidth = 0; public static final string pref_detection_area_key = "pref_detection_area_key"; static {  ....  detectionarea = new rect(1, 1, 1, 1);  ... } } 

sharedprefsutils.java

final public class sharedprefsutils { private sharedprefsutils() {} public static boolean setstringpreference(context context, string key, string value) { sharedpreferences preferences = preferencemanager.getdefaultsharedpreferences(context); if (preferences != null && !textutils.isempty(key)) {     sharedpreferences.editor editor = preferences.edit();      editor.putstring(key, value);     return editor.commit(); } return false; } 

change line

sharedprefsutils.setstringpreference(prefe.gcontext, prefe.pref_detection_area_key, area); 

with line:

sharedprefsutils.setstringpreference(mcontext, prefe.pref_detection_area_key, area); 

Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -