java - NullPointerException when trying to create a Bitmap -


i receiving nullpointerexception when try put application stack. below stack trace application failure:

06-04 15:03:08.715 e/androidruntime(22756): process: com.example.jalexander.copyright, pid: 22756 06-04 15:03:08.715 e/androidruntime(22756): java.lang.runtimeexception: unable stop activity {com.example.jalexander.copyright/com.example.jalexander.copyright.mainactivity}: java.lang.nullpointerexception 06-04 15:03:08.715 e/androidruntime(22756):     @ android.app.activitythread.performstopactivityinner(activitythread.java:3202) 06-04 15:03:08.715 e/androidruntime(22756):     @ android.app.activitythread.handlestopactivity(activitythread.java:3253) 06-04 15:03:08.715 e/androidruntime(22756):     @ android.app.activitythread.access$1100(activitythread.java:139) 06-04 15:03:08.715 e/androidruntime(22756):     @ android.app.activitythread$h.handlemessage(activitythread.java:1227) 06-04 15:03:08.715 e/androidruntime(22756):     @ android.os.handler.dispatchmessage(handler.java:102) 06-04 15:03:08.715 e/androidruntime(22756):     @ android.os.looper.loop(looper.java:136) 06-04 15:03:08.715 e/androidruntime(22756):     @ android.app.activitythread.main(activitythread.java:5103) 06-04 15:03:08.715 e/androidruntime(22756):     @ java.lang.reflect.method.invokenative(native method) 06-04 15:03:08.715 e/androidruntime(22756):     @ java.lang.reflect.method.invoke(method.java:515) 06-04 15:03:08.715 e/androidruntime(22756):     @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:790) 06-04 15:03:08.715 e/androidruntime(22756):     @ com.android.internal.os.zygoteinit.main(zygoteinit.java:606) 06-04 15:03:08.715 e/androidruntime(22756):     @ dalvik.system.nativestart.main(native method) 06-04 15:03:08.715 e/androidruntime(22756): caused by: java.lang.nullpointerexception 06-04 15:03:08.715 e/androidruntime(22756):     @ android.graphics.bitmap.createbitmap(bitmap.java:664) 06-04 15:03:08.715 e/androidruntime(22756):     @ android.graphics.bitmap.createbitmap(bitmap.java:632) 06-04 15:03:08.715 e/androidruntime(22756):     @ com.example.jalexander.copyright.mainactivity.onstop(mainactivity.java:102) 06-04 15:03:08.715 e/androidruntime(22756):     @ android.app.instrumentation.callactivityonstop(instrumentation.java:1212) 06-04 15:03:08.715 e/androidruntime(22756):     @ android.app.activity.performstop(activity.java:5420) 06-04 15:03:08.715 e/androidruntime(22756):     @ android.app.activitythread.performstopactivityinner(activitythread.java:3199) 06-04 15:03:08.715 e/androidruntime(22756):     ... 11 more  

below source code onstop() method. highlighted line 102 ***s:

{ super.onstop(); try{ display display = getwindowmanager().getdefaultdisplay();  int tlx = (168/800)*display.getwidth(); int tly = (136/480)*display.getheight(); int brx = (631/800)*display.getwidth(); int bry = (343/480)*display.getheight();  int rotation = display.getrotation();   bitmap q = bitmapfactory.decodefile("r.drawable.stockpic");  ***bitmap template = bitmap.createbitmap(q, 168, 136, (631-168), (343-136));*** view v1 = getwindow().getdecorview();  bitmap current; process p = runtime.getruntime().exec("su"); boolean landscape = false; boolean touch = false;   do{rotation = display.getrotation();     if(rotation == 90 || rotation == 270){landscape = true;}     else{rotation = display.getrotation();}   }while(rotation != 90 && rotation != 270);  while(landscape == true){      v1.setdrawingcacheenabled(true);     current = bitmap.createbitmap(v1.getdrawingcache(), 1686, 136, (631-168), (343-136));     v1.setdrawingcacheenabled(false);     touch = current.sameas(template);     if(touch == true){         p.getoutputstream().write("input tap 750 324.7".getbytes());         touch= false;     }     else{} }}catch(ioexception e){}} 

r.drawable.stockpic resource name (and should not in quotes anyway), bitmapfactory.decodefile() expects file name. per documentation, returns null if image data not decoded.

the correct way is

bitmap q = bitmapfactory.decoderesource(yourcontext.getresources(), r.drawable.stockpic); 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -