我在Crashlytics上遇到了很多与我为FirebaseRemoteConfig创建的rx-wrapper相关的NPE崩溃。
我的代码:
public Completable fetch() {
remoteConfig.activateFetched();
return Completable.create(e ->
remoteConfig.fetch(cacheExpiration).addOnCompleteListener(
task -> {
if (task.isSuccessful()) {
Log.d(TAG, "Config fetched successfully");
remoteConfig.activateFetched();
if (!e.isDisposed()) {
e.onComplete();
}
} else {
if (!e.isDisposed()) {
Log.e(TAG, "Config fetch error", task.getException());
e.onError(task.getException());
}
}
}));
}
然后我订阅它:
composite.clear();
composite.add(
helper.fetch()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(new DisposableCompletableObserver() {
@Override
public void onComplete() {
Timber.tag(TAG).d("Remote config fetched");
}
@Override
public void onError(@NonNull Throwable throwable) {
Timber.tag(TAG).d(throwable, "Remote config fetch error");
}
})
);
我遇到了3种类型的崩溃:
Crash #1
Fatal Exception: java.lang.NullPointerException
at io.reactivex.internal.operators.completable.CompletableCreate.subscribeActual(CompletableCreate.java:36)
at io.reactivex.Completable.subscribe(Completable.java:1635)
at io.reactivex.internal.operators.completable.CompletableSubscribeOn$SubscribeOnObserver.run(CompletableSubscribeOn.java:64)
at io.reactivex.Scheduler$DisposeTask.run(Scheduler.java:452)
at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:61)
at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:52)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:152)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:265)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
Crash #2
Fatal Exception: java.lang.NullPointerException
at io.reactivex.internal.operators.completable.CompletableSubscribeOn.subscribeActual(CompletableSubscribeOn.java:36)
at io.reactivex.Completable.subscribe(Completable.java:1635)
at io.reactivex.internal.operators.completable.CompletableObserveOn.subscribeActual(CompletableObserveOn.java:34)
at io.reactivex.Completable.subscribe(Completable.java:
Crash #3
Fatal Exception: java.lang.NullPointerException
at io.reactivex.internal.operators.completable.CompletableObserveOn$ObserveOnCompletableObserver.onSubscribe(CompletableObserveOn.java:68)
at io.reactivex.internal.operators.completable.CompletableSubscribeOn.subscribeActual(CompletableSubscribeOn.java:36)
at io.reactivex.Completable.subscribe(Completable.java:1635)
at io.reactivex.internal.operators.completable.CompletableObserveOn.subscribeActual(CompletableObserveOn.java:34)
at io.reactivex.Completable.subscribe(Completable.java:1635)
奇怪的是,崩溃只发生在Android 4上,主要是4.4和4.1。
答案 0 :(得分:2)
所有这些错误的共同点是,它们似乎是由null
引起的,其中CompletableObserver
是预期的。
鉴于subscribeWith
获取有效对象,发生这种情况的唯一方法是通过错误配置RxJavaPlugin.onSubscribe
挂钩,它可以包装观察者,因此由于某种原因用{替换观察者{1}}。
我建议使用反射检查null
类中的onCompletableSubscribe
字段,看看是否有一些回调可能会对这些java版本表现得很奇怪。