android - java.lang.NullPointerException in Handler on dispatchMessage -
i'm new in android programming , have sporadic error in handler service. log error is:
java.lang.nullpointerexception @ com.rafag.taxialerta.radartaxiservice$buzonmensajetareaubicaciones.handlemessage(radartaxiservice.java:105) @ android.os.handler.dispatchmessage(handler.java:106) @ android.os.looper.loop(looper.java:136) @ android.app.activitythread.main(activitythread.java:5212) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:515) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:786) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:602) @ dalvik.system.nativestart.main(native method)
my code is:
private handler puentetareaubicaciones; static class buzonmensajetareaubicaciones extends handler { private final weakreference<radartaxiservice> wr; buzonmensajetareaubicaciones(radartaxiservice srv) { wr = new weakreference<>(srv); } @override public void handlemessage(message msg) { radartaxiservice srv = wr.get(); if (srv == null){ super.handlemessage(msg); return ; } message nuevomsg = message.obtain(); switch (msg.what) { case puente_ok: srv.recibosituacion(msg.what); nuevomsg.what = msg.what; try { srv.buzonmapa.send(nuevomsg); } catch (remoteexception e) { e.printstacktrace(); } break; default: super.handlemessage(msg); break; } } }
line 105 is:
srv.buzonmapa.send(nuevomsg)
buzonmapa init @ onbind service:
public ibinder onbind(intent intent) { return buzonservicio.getbinder(); }
and:
static class buzonmensajemapa extends handler { private final weakreference<radartaxiservice> wr; buzonmensajemapa(radartaxiservice srv) { wr = new weakreference<>(srv); } @override public void handlemessage(message msg) { radartaxiservice srv = wr.get(); if (srv == null) { super.handlemessage(msg); return; } srv.buzonmapa = msg.replyto; srv.misituaciones(msg.what); } }
in main activity, onserviceconnected:
public void onserviceconnected(componentname name, ibinder service) { buzonservicio = new messenger(service); try { message msg = message.obtain(); msg.replyto = buzonactividad; buzonservicio.send(msg); } catch (remoteexception e) { //e.printstacktrace(); } }
i have error 1 of hundred times, not able find patron reproduce always.
the general flow is:
activity ----------------> service --------------------> thread buzonmapa<------------+ puentetareaubicaciones<---+
any ideas?
it weakreference. cannot sure srv not null or gce'd. check whether srv not null.
Comments
Post a Comment