java - EventBus 3.0 nullPoint异常?
问题描述
异常信息如下
java.lang.NullPointerException: Attempt to invoke virtual method ’java.lang.Class java.lang.Object.getClass()’ on a null object reference at org.greenrobot.eventbus.EventBus.postSingleEvent(EventBus.java:363) at org.greenrobot.eventbus.EventBus.post(EventBus.java:251) at com.culsomusic2.coreclass.BasePlayActivity.playBaseMusic(BasePlayActivity.java:90)at com.culsomusic2.songlistmodule.ui.mactivity.activityEntity.SongContentActivity$4.onItemClick(SongContentActivity.java:252) at com.zhy.adapter.recyclerview.MultiItemTypeAdapter$1.onClick(MultiItemTypeAdapter.java:68)at android.view.View.performClick(View.java:5198)at android.view.View$PerformClick.run(View.java:21147) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
EventBus 注册和反注册如下 @Override
protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); mContext = MyApplication.getContext(); Intent intent = new Intent(mContext, BaseControlService.class); startService(intent); //注册EventBus EventBus.getDefault().register(this);}
@Override
protected void onDestroy() { //反注册EventBus EventBus.getDefault().unregister(this); mContext = null; super.onDestroy();}
调用的方法如下 public void playBaseMusic(@Nullable BaseTagBean bean) {
BaseTagBean tagBean; if (bean==null){tagBean=new BaseTagBean();tagBean.setTag(CONTROL_TAG_PLAY); }else {tagBean=bean;tagBean.setTag(CONTROL_TAG_PLAY); } EventBus.getDefault().post(tagBean);}
只要调用上述playBaseMusic方法它就在EventBus.getDefault().post(tagBean);这个地方报空指针错误;请问大家如何解决。
问题解答
回答1:目前这种情况看,只有可能是你的tagbean是null了,打个断点看看吧。
相关文章:
1. javascript - 读取页面源码,页面中所有的换行都被当成<br/>读取出来 了,,求解应该怎么让它被正确的解析2. html5 - 百度Ueditor代码高亮和代码段滚动条冲突是怎么回事?3. java基础,求解答。4. javascript - vue 数据更新了。但是dom没有更新,,,,,如图5. docker api 开发的端口怎么获取?6. javascript - 静态页面引公共头尾文件,js怎么写吖?7. java - 为什么要将Runnable接口的子类对象传递给Thread的构造函数?8. javascript - 关于一段 for 循环代码执行顺序的问题9. debian - docker依赖的aufs-tools源码哪里可以找到啊?10. 如何调整IDEA中直接运行java程序的日志输出级别?