JeremyLiao / LiveEventBus

:mailbox_with_mail:EventBus for Android,消息总线,基于LiveData,具有生命周期感知能力,支持Sticky,支持AndroidX,支持跨进程,支持跨APP
Apache License 2.0
3.83k stars 515 forks source link

在自定义View里使用,布局不能预览 #148

Open SelectSex opened 2 years ago

SelectSex commented 2 years ago

描述 Bug

自定义view中调用了LiveEventBus.get方法,无法预览,

相关代码

    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
        LiveEventBus.get<Int>(EventKeys.Bet_Click_Parlay_Refresh).observeForever(updataUi)
        GlobalRequest.getMemberBalance.observeForever(getMemberBalance)
    }

异常堆栈

java.lang.NullPointerException   at java.lang.reflect.Method.invoke(Method.java:559)
   at com.jeremyliao.liveeventbus.utils.AppUtils.getApplicationByReflect(AppUtils.java:98)
   at com.jeremyliao.liveeventbus.utils.AppUtils.getApp(AppUtils.java:88)
   at com.jeremyliao.liveeventbus.core.LiveEventBusCore.registerReceiver(LiveEventBusCore.java:123)
   at com.jeremyliao.liveeventbus.core.LiveEventBusCore.<init>(LiveEventBusCore.java:83)
   at com.jeremyliao.liveeventbus.core.LiveEventBusCore.<init>(LiveEventBusCore.java:38)
   at com.jeremyliao.liveeventbus.core.LiveEventBusCore$SingletonHolder.<clinit>(LiveEventBusCore.java:44)
   at com.jeremyliao.liveeventbus.core.LiveEventBusCore.get(LiveEventBusCore.java:48)
   at com.jeremyliao.liveeventbus.LiveEventBus.get(LiveEventBus.java:35)
   at com.jeremyliao.liveeventbus.LiveEventBus.get(LiveEventBus.java:46)
   at com.android.orange_android.widget.BetTopTitleView.onAttachedToWindow(BetTopTitleView.kt:159)

截图

image 简单报错 image 具体详细错误 image

分析

在此处调用invoke,会在自定义view中预览时抛出Null异常。

image

invoke 方法会报错

image

报错后未try catch

image

SelectSex commented 2 years ago

live_event_bug_x1.8.0.zip 将我正在用的1.8.0都aar反编译修改了 getApplicationByReflect方法,引入项目,预览终于正常了 我修改的代码并不会对app运行时造成错误,只是修改了npe异常的使用,好让 预览编译顺利运行下去

Lieeber commented 2 years ago

想预览用isInEditMode就行了。

SelectSex commented 2 years ago

感谢感谢,太谢谢了,好几年的开发了,这个属性都没注意过!

Barry Lee @.***> 于 2022年1月4日周二 下午5:48写道:

想预览用isInEditMode就行了。

— Reply to this email directly, view it on GitHub https://github.com/JeremyLiao/LiveEventBus/issues/148#issuecomment-1004662817, or unsubscribe https://github.com/notifications/unsubscribe-auth/AVQWHYHD52T75QLMTOKWKTLUUK67NANCNFSM5H25CPBA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you authored the thread.Message ID: @.***>