0

我的应用程序到处使用 Otto,但我一直遇到相同的运行时异常崩溃。这发生在几个不同的事件中,这让我认为我没有正确实施 otto。我正在使用主线程总线技术,如下所示:

/**
 * Otto bus that enforces bus messages posted on the main thread
 * Adapted from: http://stackoverflow.com/questions/15431768/how-to-send-event-from-service-to-activity-with-otto-event-bus
 */
public class MXRMainThreadBus extends Bus {
    private final Bus mBus;
    private final Handler mHandler = new Handler(Looper.getMainLooper());

    public MXRMainThreadBus(final Bus bus) {
        if (bus == null) {
            throw new NullPointerException("bus must not be null");
        }
        mBus = bus;
    }

    @Override public void register(Object obj) {
        mBus.register(obj);
    }

    @Override public void unregister(Object obj) {
        mBus.unregister(obj);
    }

    @Override public void post(final Object event) {
        if (Looper.myLooper() == Looper.getMainLooper()) {
            mBus.post(event);
        } else {
            mHandler.post(new Runnable() {
                @Override public void run() {
                    mBus.post(event);
                }
            });
        }
    }
}

这是我的巴士提供商:

/**
 * Otto Bus Provider singleton
 */
public class BusProvider {
    private static MXRMainThreadBus BUS;

    private BusProvider() {}

    public static Bus getInstance() {
        if(BUS == null) {
            BUS = new MXRMainThreadBus(new Bus());
        }
        return BUS;
    }
}

这是堆栈跟踪(对于最常见的):

java.lang.RuntimeException
com.squareup.otto.Bus.throwRuntimeException(Bus.java:444)
com.squareup.otto.Bus.dispatch(Bus.java:374)
com.squareup.otto.Bus.dispatchQueuedEvents(Bus.java:355)
com.squareup.otto.Bus.post(Bus.java:324)
com.myxer.android.unicorn.utils.MXRMainThreadBus.post(MXRMainThreadBus.java:33)
com.myxer.android.unicorn.api.MXRApiController$1.onResponse(MXRApiController.java:173)
com.myxer.android.unicorn.api.MXRApiController$1.onResponse(MXRApiController.java:170)
com.myxer.android.unicorn.volley.GsonRequest.deliverResponse(GsonRequest.java:117)
com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
android.os.Handler.handleCallback(Handler.java:725)
android.os.Handler.dispatchMessage(Handler.java:92)
android.os.Looper.loop(Looper.java:137)
android.app.ActivityThread.main(ActivityThread.java:5185)
java.lang.reflect.Method.invokeNative(Native Method)
java.lang.reflect.Method.invoke(Method.java:511)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:564)
dalvik.system.NativeStart.main(Native Method)
Caused by: com.myxer.android.unicorn.fragments.MXRStationGridFragment.onStationReceivedImpl(MXRStationGridFragment.java:142)
com.myxer.android.unicorn.fragments.tabhosted.MXRRelatedStationsGridFragment.onStationReceived(MXRRelatedStationsGridFragment.java:75)
java.lang.reflect.Method.invokeNative(Native Method)
java.lang.reflect.Method.invoke(Method.java:511)
com.squareup.otto.EventHandler.handleEvent(EventHandler.java:89)
com.squareup.otto.Bus.dispatch(Bus.java:372)
com.squareup.otto.Bus.dispatchQueuedEvents(Bus.java:355)
com.squareup.otto.Bus.post(Bus.java:324)
com.myxer.android.unicorn.utils.MXRMainThreadBus.post(MXRMainThreadBus.java:33)
com.myxer.android.unicorn.api.MXRApiController$1.onResponse(MXRApiController.java:173)
com.myxer.android.unicorn.api.MXRApiController$1.onResponse(MXRApiController.java:170)
com.myxer.android.unicorn.volley.GsonRequest.deliverResponse(GsonRequest.java:117)
com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
android.os.Handler.handleCallback(Handler.java:725)
android.os.Handler.dispatchMessage(Handler.java:92)
android.os.Looper.loop(Looper.java:137)
android.app.ActivityThread.main(ActivityThread.java:5185)
java.lang.reflect.Method.invokeNative(Native Method)
java.lang.reflect.Method.invoke(Method.java:511)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:564)
dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

0

如果您在另一个线程上运行总线然后是主线程,那么您将遇到该异常。Otto Bus 提供程序在主线程上运行。然而,存在一个 Wrapper,它允许将事件传递给除主线程之外的其他线程。当您尝试将消息从正在运行的后台服务传递到 UI 线程时,这可用于服务。这是包装:

public class MainThreadBus extends Bus
  {
    private final Bus     mBus;
    private final Handler mHandler = new Handler(Looper.getMainLooper());

    public MainThreadBus(final Bus bus)
      {
        if (bus == null)
          {
            throw new NullPointerException("bus must not be null");
          }
        mBus = bus;
      }

    @Override
    public void register(Object obj)
      {
        mBus.register(obj);
      }

    @Override
    public void unregister(Object obj)
      {
        mBus.unregister(obj);
      }

    @Override
    public void post(final Object event)
      {
        if (Looper.myLooper() == Looper.getMainLooper())
          {
            mBus.post(event);
          }
        else
          {
            mHandler.post(new Runnable()
              {
                @Override
                public void run()
                  {
                    mBus.post(event);
                  }
              });
          }
      }
  }
于 2014-09-04T12:46:58.933 回答