GreenRobot的EventBus总是返回NullPointerException

时间:2017-06-10 06:36:20

标签: android event-bus greenrobot-eventbus-3.0

我不确定,这个图书馆的内容是什么,但结果总是不一致(至少对我而言)。

我在 ActivityB 中有 ActivityA ActivityB FragmentB1 。我将不得不将ActivityA的String ID传递给ActivityB,FragmentB1。由于我将在ActivityB中有一些片段也需要这个ID,我想为什么不使用EventBus。因此,ActivityA成为发布者和ActivityB,FragmentB1成为订阅者。

定义事件的POJO

public class EventUid {
    private final String Uid;

    public EventUid(String uid) {
        Uid = uid;
    }

    public String getUid() {
        return Uid;
    }
}

ActivityA的代码如下。选择Spinner项时会运行以下代码。

EventBus.getDefault().post(new EventUid(Uid));

ActivityB,FragmentB1的代码如下。首先,我在onStart()注册活动。

@Override
public void onStart() {
    super.onStart();
    EventBus.getDefault().register(this);
}

然后我在onStop()中取消注册事件。

@Override
public void onStop() {
    EventBus.getDefault().unregister(this);
    super.onStop();
}

在订阅Event之前,我在ActivityB和FragmentB1中定义了一个String。

private String ProductUid;

最后,我在ActivityB和FragmentB1中订阅了该事件,如下所示。

@Subscribe(threadMode = ThreadMode.MAIN)
public void onEventUid(EventUid event) {
    ProductUid = event.getUid();
}

我不确定我可能在哪里出错,但ActivityB和FragmentB1总是返回NullPointerException。有人能用正确的方式指出我吗?

由于

编辑:添加LogCat。 PS:此问题的活动,片段名称(以便于理解而命名)将与LogCat不同。

--------- beginning of crash
06-10 12:44:20.342 3019-3019/blog.deshki.editor E/AndroidRuntime: FATAL EXCEPTION: main
                                                                  Process: blog.deshki.editor, PID: 3019
                                                                  java.lang.RuntimeException: Unable to start activity ComponentInfo{blog.deshki.editor/blog.deshki.editor.screens.media.MediaActivity}: java.lang.NullPointerException: Can't pass null for argument 'pathString' in child()
                                                                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                      at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                      at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                      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)
                                                                   Caused by: java.lang.NullPointerException: Can't pass null for argument 'pathString' in child()
                                                                      at com.google.firebase.database.DatabaseReference.child(Unknown Source)
                                                                      at blog.deshki.editor.screens.media.MediaActivity.firebaseSettings(MediaActivity.java:291)
                                                                      at blog.deshki.editor.screens.media.MediaActivity.onCreate_aroundBody0(MediaActivity.java:267)
                                                                      at blog.deshki.editor.screens.media.MediaActivity$AjcClosure1.run(MediaActivity.java:1)
                                                                      at org.aspectj.runtime.reflect.JoinPointImpl.proceed(JoinPointImpl.java:149)
                                                                      at com.frogermcs.androiddevmetrics.aspect.ActivityLifecycleAnalyzer.executeWithTracingIfEnabled(ActivityLifecycleAnalyzer.java:88)
                                                                      at com.frogermcs.androiddevmetrics.aspect.ActivityLifecycleAnalyzer.ajc$inlineAccessMethod$com_frogermcs_androiddevmetrics_aspect_ActivityLifecycleAnalyzer$com_frogermcs_androiddevmetrics_aspect_ActivityLifecycleAnalyzer$executeWithTracingIfEnabled(ActivityLifecycleAnalyzer.java:1)
                                                                      at com.frogermcs.androiddevmetrics.aspect.ActivityLifecycleAnalyzer.logAndExecute(ActivityLifecycleAnalyzer.java:66)
                                                                      at blog.deshki.editor.screens.media.MediaActivity.onCreate(MediaActivity.java:262)
                                                                      at android.app.Activity.performCreate(Activity.java:6237)
                                                                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                      at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                      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) 
06-10 12:44:20.358 1279-2221/? E/SurfaceFlinger: ro.sf.lcd_density must be defined as a build property

0 个答案:

没有答案