android - Simple Runnable experiment Works on AVD but not on hardware device -
i'm beginner @ both android development , stackoverflow please don't shoot me if step on toes ! trying simple app working user pushes button , displayed integer increments while button held down, , button stays when button released. ground breaking stuff !
so went following approach :
public class mainactivity extends activity { button pushbtn; textview textview,inc_txt; string tag ="button act"; int inc_val =0; public handler myhandler=null; boolean finishedthread = false; boolean threadrunning = false; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); log.i(tag, "in oncreate"); } @override protected void onstart() { // todo auto-generated method stub super.onstart(); log.i(tag, "in onstart"); pushbtn=(button)findviewbyid(r.id.btn_push); log.i(tag, "in onstart 1"); textview=(textview)findviewbyid(r.id.testtext); inc_txt=(textview)findviewbyid(r.id.increment_text); pushbtn.setontouchlistener(new ontouchlistener() { @override public boolean ontouch(view v, motionevent button_event) { log.i(tag, "in ontouch event"); switch (button_event.getaction()) { case motionevent.action_down: log.i(tag, " ..down"); textview.settext("button down"); if(myhandler!=null) { log.i(tag, "handle not null"); return true; } myhandler=new handler(); log.i(tag, "postingaction original"); myhandler.postdelayed(pushedaction, 250); return true; case motionevent.action_up: textview.settext("button up"); myhandler.removecallbacks(pushedaction); myhandler=null; return true; default: textview.settext("default"); myhandler=null; break; } return false; } }); log.i(tag, "in onstart 3"); } runnable pushedaction= new runnable() { @override public void run() { inc_txt.settext(integer.tostring(inc_val)); inc_val++; log.i(tag, "postingaction repeat"); myhandler.postdelayed(this, 250); // todo auto-generated method stub } };
and happy days works on avd (4.2.2). (its button , 2 textviews)
but when went put onto nexus 4 (4.3), original postdelayed (in "onstart") works fine , second postdelayed, recursive call in runnable throws null pointer exception.
see logcat:
08-08 10:08:14.245: i/button act(19992): in ontouch event 08-08 10:08:14.245: i/button act(19992): ..down 08-08 10:08:14.245: i/button act(19992): postingaction original 08-08 10:08:14.275: i/button act(19992): in ontouch event 08-08 10:08:14.385: i/button act(19992): in ontouch event 08-08 10:08:14.405: i/button act(19992): in ontouch event 08-08 10:08:14.425: i/button act(19992): in ontouch event 08-08 10:08:14.495: i/button act(19992): postingaction repeat 08-08 10:08:14.495: d/androidruntime(19992): shutting down vm 08-08 10:08:14.495: w/dalvikvm(19992): threadid=1: thread exiting uncaught exception (group=0x4152c700) 08-08 10:08:14.495: e/androidruntime(19992): fatal exception: main 08-08 10:08:14.495: e/androidruntime(19992): java.lang.nullpointerexception 08-08 10:08:14.495: e/androidruntime(19992): @ com.example.buttonexperiments.mainactivity$1.run(mainactivity.java:94) 08-08 10:08:14.495: e/androidruntime(19992): @ android.os.handler.handlecallback(handler.java:730) 08-08 10:08:14.495: e/androidruntime(19992): @ android.os.handler.dispatchmessage(handler.java:92) 08-08 10:08:14.495: e/androidruntime(19992): @ android.os.looper.loop(looper.java:137) 08-08 10:08:14.495: e/androidruntime(19992): @ android.app.activitythread.main(activitythread.java:5103) 08-08 10:08:14.495: e/androidruntime(19992): @ java.lang.reflect.method.invokenative(native method) 08-08 10:08:14.495: e/androidruntime(19992): @ java.lang.reflect.method.invoke(method.java:525) 08-08 10:08:14.495: e/androidruntime(19992): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:737) 08-08 10:08:14.495: e/androidruntime(19992): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:553) 08-08 10:08:14.495: e/androidruntime(19992): @ dalvik.system.nativestart.main(native method)
can see doing wrong? appreciated. sorry if made mess of asking question ( logcat .. know )! safe travels !
i think problem might in default case of switch case , in action put myhandler null. there reason why that? create new handler everytime touch , null everytime. why that? put myhandler=new handler();
in oncreate , leave way, no need put null , create new one.
Comments
Post a Comment