Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

你好,这个空指针怎么解决,在fragment里面加入 .compose(RxLife.with(this).bindToLifecycle()) #3

Closed
huangxumin opened this issue May 5, 2019 · 7 comments

Comments

@huangxumin
Copy link

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void io.reactivex.subjects.Subject.onNext(java.lang.Object)' on a null object reference
at com.dhh.rxlife2.LifecycleBridge.onStateChanged(LifecycleBridge.kt:29)
at com.dhh.rxlife2.LifecycleBridgeBase.onStateChanged(LifecycleBridgeBase.kt:31)
at androidx.lifecycle.LifecycleRegistry$ObserverWithState.dispatchEvent(LifecycleRegistry.java:355)
at androidx.lifecycle.LifecycleRegistry.addObserver(LifecycleRegistry.java:181)
at com.dhh.rxlife2.LifecycleBridgeBase.(LifecycleBridgeBase.kt:21)
at com.dhh.rxlife2.LifecycleBridge.(LifecycleBridge.kt:14)
at com.dhh.rxlife2.LifecycleBridge$Companion.get(LifecycleBridge.kt:19)
at com.dhh.rxlife2.RxLife.with(RxLife.kt:17)
at com.yunniu.isonic_smarthome.ui.fragment.MyHouseFragments.initView(MyHouseFragments.java:374)
at com.yunniu.isonic_smarthome.ui.fragment.MyHouseFragments.onCreateView(MyHouseFragments.java:145)

@huangxumin
Copy link
Author

额,我现在oncreate调用 RxLife.with(this),初始化之后就不会出现这个问题了。。

@huangxumin
Copy link
Author

Attempt to invoke virtual method 'void io.reactivex.subjects.Subject.onNext(java.lang.Object)' on a null object reference,其他地方还是会报错。

@dhhAndroid
Copy link
Owner

你调用代码我看一下,是否添加依赖 RxJava2,我看了代码,此处调用的是 lifecycleSubject.onNext()方法,lifecycleSubject已经在类实例化的时候初始化了 lifecycleSubject,点击查看LifecycleBridge代码,另外在Activity里使用有问题吗 ?是不是Fragment 的问题,Fragment只能是v4包下的 Fragment

@dhhAndroid
Copy link
Owner

@huangxumin Fragment应该是动态添加的吧 ?

@huangxumin
Copy link
Author

有添加rxjava2依赖,activity也会出现这个问题,我觉得不是fragment的问题。我跳转activity也会出现这个问题。fargment是动态添加的

 //判断此Fragment是否已经添加到FragmentTransaction事物中
    if (!fragment.isAdded()) {
        ft.add(R.id.fragment, fragment, fragment.getClass().getName());
    } else {
        ft.show(fragment);
    }

是v4的fragment,activity继承fragmentAvtivity.

这是activity和fragmrnt的调用代码:
注释的rxlife,我现在用RxLifecycle测试。

baseActivity里面:

public <T> LifecycleTransformer<T> bindToLifecycle() {

// return RxLife.with(this).bindOnDestroy();
return RxLifecycle.with(this).bindOnDestroy();
}
调用:

RxView.clicks(back).throttleFirst(1, TimeUnit.SECONDS).compose(bindToLifecycle())

.subscribe(new Consumer() {
@OverRide
public void accept(Unit o) throws Exception {
onBackPressed();
}
});

@dhhAndroid
Copy link
Owner

原因我已经查到,类加载构造方法执行顺序的问题,可能会导致这个问题,我正在解决,随后发布代码

@dhhAndroid
Copy link
Owner

你试一下使用最新的1.1.3版本,能否解决问题

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants