java - EventBus 3.0 nullPoint异常?
PHPz
PHPz 2017-04-18 10:54:12
0
1
944

异常信息如下

 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);这个地方报空指针错误;
请问大家如何解决。

PHPz
PHPz

学习是最好的投资!

모든 응답(1)
PHPzhong

현재 상황을 볼 때 유일한 가능성은 태그빈이 null일 가능성이 있다는 것입니다. 중단점을 설정하고 살펴보겠습니다.

최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿