java.lang.NullPointerException:插件返回null

时间:2018-08-25 09:13:12

标签: java android rx-java2 rx-android

我搜索了NullPointerException: Plugin returned null Observer,但是没有找到任何有用的解决方法。我在Crashlytics中发现了这个问题,因此没有创建它的方案。完整的错误如下:

Fatal Exception: java.lang.NullPointerException: Plugin returned null Observer
       at io.reactivex.d.b.am.a(ObjectHelper.java:39)
       at io.reactivex.k.subscribe(Observable.java:12049)
       at io.reactivex.d.e.d.go.subscribeActual(ObservableObserveOn.java:45)
       at io.reactivex.k.subscribe(Observable.java:12051)
       at io.reactivex.k.subscribe$209b3d11(Observable.java:12037)
       at io.reactivex.k.subscribe$50b5b765(Observable.java:11997)
       at com.example.mobile.feature.home_serp.AbsSerpPresenter.observeAction(AbsSerpPresenter.kt:365)
       at com.example.mobile.fragments.f.a(Unknown Source:1489)
       at com.example.mobile.feature.home_serp.SerpAdapter.onCreateViewHolder(SerpAdapter.kt:39)
       at com.example.mobile.feature.home_serp.SerpAdapter.onCreateViewHolder(SerpAdapter.kt:17)
       at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6685)
       at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5869)
       at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5752)
       at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5748)
       at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2232)
       at android.support.v7.widget.GridLayoutManager.layoutChunk(GridLayoutManager.java:556)
       at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1519)
       at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:614)
       at android.support.v7.widget.GridLayoutManager.onLayoutChildren(GridLayoutManager.java:170)
       at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3812)
       at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3529)
       at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:4082)
       at android.view.View.layout(View.java:11420)
       at android.view.ViewGroup.layout(ViewGroup.java:4238)
       at android.support.v4.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:606)
       at android.view.View.layout(View.java:11420)
       at android.view.ViewGroup.layout(ViewGroup.java:4238)
       at android.support.design.widget.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:132)
       at android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42)
       at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1361)
       at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:894)
       at android.view.View.layout(View.java:11420)
       at android.view.ViewGroup.layout(ViewGroup.java:4238)
       at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
       at android.view.View.layout(View.java:11420)
       at android.view.ViewGroup.layout(ViewGroup.java:4238)
       at android.widget.RelativeLayout.onLayout(RelativeLayout.java:925)
       at android.view.View.layout(View.java:11420)
       at android.view.ViewGroup.layout(ViewGroup.java:4238)
       at android.support.design.widget.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:132)
       at android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42)
       at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1361)
       at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:894)
       at android.view.View.layout(View.java:11420)
       at android.view.ViewGroup.layout(ViewGroup.java:4238)
       at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
       at android.view.View.layout(View.java:11420)
       at android.view.ViewGroup.layout(ViewGroup.java:4238)
       at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1644)
       at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1502)
       at android.widget.LinearLayout.onLayout(LinearLayout.java:1415)
       at android.view.View.layout(View.java:11420)
       at android.view.ViewGroup.layout(ViewGroup.java:4238)
       at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
       at android.view.View.layout(View.java:11420)
       at android.view.ViewGroup.layout(ViewGroup.java:4238)
       at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1644)
       at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1502)
       at android.widget.LinearLayout.onLayout(LinearLayout.java:1415)
       at android.view.View.layout(View.java:11420)
       at android.view.ViewGroup.layout(ViewGroup.java:4238)
       at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
       at android.view.View.layout(View.java:11420)
       at android.view.ViewGroup.layout(ViewGroup.java:4238)
       at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1671)
       at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2628)
       at android.os.Handler.dispatchMessage(Handler.java:99)
       at android.os.Looper.loop(Looper.java:137)
       at android.app.ActivityThread.main(ActivityThread.java:4517)
       at java.lang.reflect.Method.invokeNative(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:511)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
       at dalvik.system.NativeStart.main(NativeStart.java)

我们有以下依赖关系:

implementation 'io.reactivex.rxjava2:rxkotlin:2.2.0'
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.4.0'
implementation 'io.reactivex.rxjava2:rxandroid:2.0.2'
implementation 'io.reactivex.rxjava2:rxjava:2.1.14'

我阅读了RxJava代码,并在subscribe方法中声明如下:

    @SchedulerSupport(SchedulerSupport.NONE)
    @Override
    public final void subscribe(Observer<? super T> observer) {
        ObjectHelper.requireNonNull(observer, "observer is null");
        try {
            observer = RxJavaPlugins.onSubscribe(this, observer);

            ObjectHelper.requireNonNull(observer, "Plugin returned null Observer");

            subscribeActual(observer);
        } catch (NullPointerException e) { // NOPMD
            throw e;
        } catch (Throwable e) {
            Exceptions.throwIfFatal(e);
            // can't call onError because no way to know if a Disposable has been set or not
            // can't call onSubscribe because the call might have set a Subscription already
            RxJavaPlugins.onError(e);

            NullPointerException npe = new NullPointerException("Actually not, but can't throw other exceptions due to RS");
            npe.initCause(e);
            throw npe;
        }
    }

onSubscribe方法的代码如下:

    /**
     * Calls the associated hook function.
     * @param <T> the value type
     * @param source the hook's input value
     * @param observer the observer
     * @return the value returned by the hook
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    @NonNull
    public static <T> Observer<? super T> onSubscribe(@NonNull Observable<T> source, @NonNull Observer<? super T> observer) {
        BiFunction<? super Observable, ? super Observer, ? extends Observer> f = onObservableSubscribe;
        if (f != null) {
            return apply(f, source, observer);
        }
        return observer;
    }

因此,apply方法返回null,即onObservableSubscribe.apply(source, observer)。我在这里停了!

这是我们用来订阅的代码:

fun observeAction(observable: Observable<BaseAction>) {
    if (mSubject == null) {
        mSubject = PublishSubject.create()
        mSubject!!.throttleFirst(1100, TimeUnit.MILLISECONDS)
                .subscribe(onNextObserver(),
                        { t: Throwable -> logger.e("Cannot receive the action", t) },
                        { logger.e("action receiver is completed") })
    }

    mSubscriptionsLongLife.add(
            observable.observeOn(AndroidSchedulers.mainThread())
                    .subscribe(mSubject!!::onNext, mSubject!!::onError, mSubject!!::onComplete)
    )
}

private fun onNextObserver(): (BaseAction) -> Unit {
    return { action ->
        blah blah ...
    }
}

任何修复该错误的建议都会有所帮助。

0 个答案:

没有答案