dispatchMessage中的Handler中的java.lang.NullPointerException

时间:2015-07-14 18:04:46

标签: java android android-handler

我是Android编程的新手,我在服务器的处理程序中偶尔出现错误。日志错误是:

java.lang.NullPointerException
at com.rafag.taxialerta.RadarTaxiService$buzonMensajeTareaUbicaciones.handleMessage(RadarTaxiService.java:105)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5212)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
at dalvik.system.NativeStart.main(Native Method)

我的代码是:

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;
            }
        }
    }

105行是:

srv.buzonMapa.send(nuevoMsg)

buzonMapa是onBind to Service的初始化:

public IBinder onBind(Intent intent) {
    return buzonServicio.getBinder();
}

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);
    }
}

在主要活动中,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();
    }
}

我有一百次这个错误,但我找不到一个赞助人来重现它。

一般流程是:

Activity ----------------> Service --------------------> Thread
        buzonMapa<------------+  puenteTareaUbicaciones<---+ 

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

这是弱引用。所以你不能确定srv不是null或GCe&#39; d。检查srv是否为空。

答案 1 :(得分:0)

我想我发现了这个问题的可能原因。如果应用程序因任何原因而崩溃,则START_STICKY会自动重新创建该服务,但对“buzonMapa”的引用将丢失。