java - Android timer throws Null Pointer Exception -
i've made game in android , got pretty work, except timer. it's throwing npe whenever try run game. i've tried few different timers, looked through loads of pages trying find answer have had no luck far. i've posted relevant code , logcat below.
public class gameview extends view { private paint redpaint, blackpaint; private bitmap ball1; private string output = "output appear here"; private int ballx, bally; private int radius = 50; private int count = 0; private int circlex = 350; private int circley = 550; textview tv; public gameview(context context) { super(context); redpaint = new paint(); redpaint.setcolor(color.red); blackpaint = new paint(); blackpaint.setcolor(color.black); ball1 = bitmapfactory.decoderesource(getresources(), r.drawable.ball); tv = (textview)findviewbyid(r.id.timer); new countdowntimer(60000, 1000) { public void ontick(long millisuntilfinished) { tv.settext("seconds remaining: " + millisuntilfinished / 1000); } public void onfinish() { tv.settext("done!"); } }.start(); }
logcat, error @ tv.settext("seconds remaining: " + millisuntilfinished / 1000);
05-11 20:11:59.244 645-645/com.example.jeff.ballgame e/androidruntime﹕ fatal exception: main java.lang.nullpointerexception @ com.example.jeff.ballgame.gameview$1.ontick(gameview.java:39) @ android.os.countdowntimer$1.handlemessage(countdowntimer.java:124) @ android.os.handler.dispatchmessage(handler.java:99) @ android.os.looper.loop(looper.java:137) @ android.app.activitythread.main(activitythread.java:4424) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:511) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:784) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:551) @ dalvik.system.nativestart.main(native method) 05-11 20:11:59.374 645-651/com.example.jeff.ballgame i/dalvikvm﹕ threadid=3: reacting signal 3 05-11 20:11:59.394 645-651/com.example.jeff.ballgame i/dalvikvm﹕ wrote stack traces '/data/anr/traces.txt' 05-11 20:11:59.774 645-651/com.example.jeff.ballgame i/dalvikvm﹕ threadid=3: reacting signal 3 05-11 20:11:59.784 645-651/com.example.jeff.ballgame i/dalvikvm﹕ wrote stack traces '/data/anr/traces.txt' 05-11 20:16:59.628 645-645/com.example.jeff.ballgame i/process﹕ sending signal. pid: 645 sig: 9
gameactivity code
public class gameactivity extends activity { gameview gv; textview tv; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_game); gv = new gameview(this); setcontentview(gv); tv = (textview)findviewbyid(r.id.timer); new countdowntimer(60000, 1000) { public void ontick(long millisuntilfinished) { tv.settext("seconds remaining: " + millisuntilfinished / 1000); } public void onfinish() { tv.settext("done!"); } }.start(); }
the textview null.
tv = (textview)findviewbyid(r.id.timer);
will return null because called before setcontentview()
think call in oncreate()
Comments
Post a Comment