标签: greenrobot-eventbus

带有Fragments和EventBus的空指针异常

我已经通过各种方法使这个项目工作,并最终确定了Green Robot的EventBus.我在这里的第二个问题是: - 多个片段到活动的沟通.

我被带到那里的事件巴士路线.我在适当的图书馆里闯入,我认为我非常接近于它的工作.我不断在任何包含该行的行中获得空指针异常

eBus.post(Str);
Run Code Online (Sandbox Code Playgroud)

我相当肯定,因为我没有正确注册eBus.我知道Fragment没有这样的上下文,但我也尝试使用getActivity()也无济于事.我在下面发布了MainActivity,CoreFragment和LogCat.如果有人可以提供帮助,我会非常感激.这已经持续了近一个月!

import de.greenrobot.event.EventBus;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.EditText;

public class MainActivity extends Activity {

EditText editText1;
String Txt;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    editText1 = (EditText) findViewById(R.id.editText1);
    Txt = "";
    EventBus eBus = new EventBus();
    eBus.register(this);<--This I removed.
        eBus.getDefault().register(this);<--This is the new line you suggested  
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it …
Run Code Online (Sandbox Code Playgroud)

android android-fragments greenrobot-eventbus

5
推荐指数
1
解决办法
6041
查看次数

未收到Greenrobot EventBus活动

我正在使用Greenrobot EventBus将事件从一个活动传递到另一个活动.

流程如下:Activity1启动 - >扫描条形码 - > Activity2启动 - >接受或拒绝响应并向Activity1发送事件.

所以Activity2通过执行以下操作向Activity1发送一个新事件:

@Override
public void onCreate(){
  EventBus.getDefault().register(this);
  // other initialization code
  EventBus.getDefault().post(new MyEvent());
}
Run Code Online (Sandbox Code Playgroud)

在Activity1中,我注册了事件总线,并且我还有公共的onEvent(MyEvent myEvent)方法来接收事件.

问题是onEvent没有被触发.我看到可能在事件总线对象上有问题(比如活动1和2中的不同实例或某些实例),但它是同一个实例.

我不知道这似乎是什么问题.如果有人可以看一看并告诉我我做错了什么,我会非常感激.

谢谢!

android greenrobot-eventbus

5
推荐指数
1
解决办法
6655
查看次数

AndroidAnnotations和EventBus

我在库中有一个带注释的Activity,它是来自同一个库的EventBus事件的订阅者.它看起来像这样,大大简化了:

@EActivity(resName = "activity_foo")
public class Foo extends Activity {

    public void onEvent(BarEvent event){
        doSomething();
    }
}
Run Code Online (Sandbox Code Playgroud)

它应该按照这个工作:

http://timnew.me/blog/2014/09/14/otto-and-android-annotations-compatibility-issue-analysis/

但实际上它会返回此错误:

无法启动活动... de.greenrobot.event.EventBusException:订阅者类com.foo.bar.activities.Foo_没有名为onEvent的公共方法

似乎EventBus没有查看父类.我想每个人都在谈论的@Subscribe注释仅在Guava和Otto中,而不是在EventBus中.没有人在谈论网络上的AA和Eventbus之间的兼容性问题,所以我必须遗漏一些东西.

我怎样才能做到这一点?

EventBus:2.4

AA:3.2

编辑:

在WonderCsabo的回答之后,我将EventBus更新到3.0 beta(包括订阅注释)和AA到3.3.1并且问题消失了,但还有另外一个:

   java.lang.NoSuchFieldError
            at libcore.reflect.AnnotationAccess.decodeValue(AnnotationAccess.java:688)
            at libcore.reflect.AnnotationAccess.getDefaultValue(AnnotationAccess.java:361)
            at java.lang.reflect.Method.getDefaultValue(Method.java:327)
            at libcore.reflect.AnnotationFactory.getElementsDescription(AnnotationFactory.java:75)
            at libcore.reflect.AnnotationFactory.<init>(AnnotationFactory.java:112)
            at libcore.reflect.AnnotationFactory.createAnnotation(AnnotationFactory.java:94)
            at libcore.reflect.AnnotationAccess.toAnnotationInstance(AnnotationAccess.java:666)
            at libcore.reflect.AnnotationAccess.toAnnotationInstance(AnnotationAccess.java:641)
            at libcore.reflect.AnnotationAccess.getDeclaredAnnotation(AnnotationAccess.java:170)
            at java.lang.reflect.Method.getAnnotation(Method.java:301)
            at de.greenrobot.event.n.b(SourceFile:133)
            at de.greenrobot.event.n.a(SourceFile:79)
            at de.greenrobot.event.c.a(SourceFile:135)
            at com.babestudios.lib.lq.activities.f.onStart(SourceFile:515)
            at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1236)
            at android.app.Activity.performStart(Activity.java:6006)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2288)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5254)
            at java.lang.reflect.Method.invoke(Native Method) …
Run Code Online (Sandbox Code Playgroud)

android android-annotations greenrobot-eventbus

5
推荐指数
2
解决办法
3862
查看次数

Android:绿色机器人事件总线:单个帖子收到多个事件

我在Android中使用绿色机器人事件总线

我使用EventBus.getDefault()调用所有事件.post和onStop我调用EventBus.getDefault().unregister(this); 在我的活动中.然而,一旦我按下并重新打开应用程序,在单个事件帖子上,会收到多个onEvent().还有其他人遇到过这个问题吗?

@Override
protected void onStart() {
    super.onStart();
    getBus().register(this);
}

@Override
protected void onPause() {
    getBus().unregister(this);
    super.onPause();
}

@Override
protected void onStop() {
    getBus().unregister(this);
    super.onStop();
}


protected EventBus getBus() {
    return EventBus.getDefault();
}
Run Code Online (Sandbox Code Playgroud)

android greenrobot-eventbus

5
推荐指数
1
解决办法
1335
查看次数

Android:用Greenrobot EventBus如何在2个Activity之间进行通信?

我想在2活动之间进行沟通.两者都是register()和unregister()方法:

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

@Override
public void onStop() {
    EventBus.getDefault().unregister(this);
    super.onStop();
}
Run Code Online (Sandbox Code Playgroud)

单击列表(项目选择)时,将启动ActivityB.ActivityB的目的是更新一些信息,并将这些新信息发送给ActivityA; 所以在ActivityB中我打电话:

EventBus.getDefault().post(new MyNewEvent(bla bla bla));
Run Code Online (Sandbox Code Playgroud)

在我的ActivityA中我有这个方法:

public void onEvent(MyNewEvent event) {
    ...
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,这个方法onEvent从未被调用过.为什么?因为当ActivityB启动时,调用ActivityA中的方法onStop(),所以完成了对总线的取消注册...

那么如何通过使用EventBus在这两个Activity之间进行通信?

感谢你们!

android android-activity greenrobot-eventbus

5
推荐指数
2
解决办法
4449
查看次数

在多活动中将 LiveData 替换为 EventBus

目前我的项目使用 EventBus 来发布事件,我正在尝试用 EventBus 替换 LiveData。理论上,它们的工作原理类似。一开始我迁移时没有任何痛苦。但出现了一个我不知道如何处理的情况。

这是场景。

LaunchActivity -> PersonInfoActivity -> UpdateInfoActivity

目前我们在LaunchActivity和PersonInfoActivity中使用EventBus订阅UserInfoEvent

LaunchActivity.java

public class LaunchActivity{
     @Subscribe(threadMode = ThreadMode.MAIN)
     public void onEvent(UserInfoEvent event){

     }
}
Run Code Online (Sandbox Code Playgroud)

PersonInfoActivity.java

public class PersonInfoActivity{
     @Subscribe(threadMode = ThreadMode.MAIN)
     public void onEvent(UserInfoEvent event){

     }
}
Run Code Online (Sandbox Code Playgroud)

更新信息活动.java

public class UpdateInfoActivity{
     public void onSubmit(){
          EventBus.getDefault().post(new UserInfoEvent());
     }
}
Run Code Online (Sandbox Code Playgroud)

而问题是,我想用LiveData来代替这个场景

我做了什么?

我读过有关 livedata Room 的单例使用的问题 - LiveData 观察者在数据库更新时不会触发,我想尝试使用相同的方式但没有运气。onChanged()每次当我开始活动时,事件都会触发

java android greenrobot-eventbus android-livedata android-architecture-components

5
推荐指数
1
解决办法
6147
查看次数

EventBus发布无法更新工具栏标题.任何解决方法?

我使用EventBus(GreenRobot)从backgroundThread发布到我的活动(这是一个UiThread).我可以开始片段等.我也想更新我的标题,所以我做了如下.(调用Activity setTitle())

@Subscribe
public void onEvent(AuthenticationEvent event) {
    setTitle(event.getTitle());
}
Run Code Online (Sandbox Code Playgroud)

但是,如下所示,存在错误.(应用程序没有崩溃,但错误清楚地显示在日志中,并且工具栏标题在此之后表现得很奇怪).

E/EventBus: Could not dispatch event: class package.AuthenticationEvent to subscribing class class package.MainActivity
      android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6357)
Run Code Online (Sandbox Code Playgroud)

有没有办法解决它,EventBus的帖子仍然允许我更新我的Ui(例如标题)?

android android-activity greenrobot-eventbus

4
推荐指数
1
解决办法
1169
查看次数

如何将消息从后台服务发布到UI片段?

我和EventBusGreenrobot 有问题.

我试图从我的同步适配器的后台服务发布事件,并在片段中捕获它以更新UI.

问题是当我尝试从同步适配器发布事件时,我在调试日志中得到以下内容:

No subscribers registered for event class olexiimuraviov.ua.simplerssreader.event.UpdateUIEvent
No subscribers registered for event class org.greenrobot.eventbus.NoSubscriberEvent

我在其中注册片段onResume并取消注册onPause

@Override
public void onResume() {
    super.onResume();
    EventBus.getDefault().register(this);
    if (mDebug) Log.d(LOG_TAG, EventBus.getDefault().isRegistered(this) + "");
}

@Override
public void onPause() {
    super.onPause();
    EventBus.getDefault().unregister(this);
}
Run Code Online (Sandbox Code Playgroud)

onResume中的日志语句显示片段已成功注册.

这是onEvent方法:

@Subscribe
public void onEvent(UpdateUIEvent event) {
    if (mSwipeRefreshLayout.isRefreshing())
        mSwipeRefreshLayout.setRefreshing(false);
}
Run Code Online (Sandbox Code Playgroud)

我试图用Background threadmode调用onEvent方法,但它没有帮助.

在此之后,我试图使用处理程序发布事件,但eventbus仍然找不到任何注册订阅者的事件.

 new Handler(Looper.getMainLooper()).post(new Runnable() {
     @Override
     public void run() {
         EventBus.getDefault().post(new UpdateUIEvent());
     }
 });
Run Code Online (Sandbox Code Playgroud)

这是我的onPerform同步适配器方法:

    @Override
public void onPerformSync(Account account, …
Run Code Online (Sandbox Code Playgroud)

android event-bus android-syncadapter android-fragments greenrobot-eventbus

4
推荐指数
1
解决办法
1958
查看次数

Greenrobot EventBus无法发送事件

我正在使用EventBus从长时间运行的线程传递到更新其UI的Fragments.这个长时间运行的线程基本上是在监听网络活动,解析它并将事件放在总线上.我有2个片段设置接收事件.

当我从Fragments的onEvent方法更新Fragments的UI(textview)时出现错误.如何从单独的线程更新片段中的UI?

这是我的片段代码和我看到的logcat输出...

logcat的:

10-15 02:19:44.637: I/System.out(682): debugger has settled (1440)
10-15 02:19:45.021: V/BRRT(682): start thread
10-15 02:19:45.021: V/BRRT(682): past start thread
10-15 02:19:45.031: V/TEST(682): running thread...
10-15 02:19:45.121: D/Event(682): No subscribers registered for event class com.broadreach.radiotester.io.ExpeditionEvent
10-15 02:19:45.121: D/Event(682): No subscribers registered for event class de.greenrobot.event.NoSubscriberEvent
10-15 02:19:45.261: V/BRRT(682): OneUp event received: 204 : 32
10-15 02:19:45.271: W/ResourceType(682): No package identifier when getting value for resource number 0x00000000
10-15 02:19:45.351: E/Event(682): Could not dispatch event: class com.broadreach.radiotester.io.ExpeditionEvent to …
Run Code Online (Sandbox Code Playgroud)

java multithreading android android-fragments greenrobot-eventbus

3
推荐指数
1
解决办法
8887
查看次数

构建proj时的EventBus异常 - Subscriber类没有名为onEvent的公共方法

所以,我的应用程序从/向我的MainActivity交换消息到/从后台服务,我使用EventBus来处理它.我正在注册这两个组件

EventBus.getDefault().register(this);
Run Code Online (Sandbox Code Playgroud)

在他们的onCreates.我正在发送/接收一个活动:

EventBus.getDefault().post(new MyMessagePojo("message"));
Run Code Online (Sandbox Code Playgroud)

public void onEvent(MyMessagePojo event) { ... }
Run Code Online (Sandbox Code Playgroud)

当我将项目从AndroidStudio运行到我的测试手机时,一切正常.但是,当我生成签名的APK时,我安装了应用程序并遇到以下异常崩溃:

Subscriber class my.package.MainActivity has no public methods called onEvent
Run Code Online (Sandbox Code Playgroud)

它显然有的地方.我尝试将其更改onEventonEventMainThread我的MainActivity,但没有成功.这是非常令人沮丧的,因为我即将发布应用程序,现在我无法解决这个问题.

我已经通过这个这个,但他们都没有帮助.

有任何想法吗?

android event-bus greenrobot-eventbus

3
推荐指数
1
解决办法
2679
查看次数