javaandroidevent-busgreenrobot-eventbusgreenrobot-eventbus-3.0

EventBus - Class not recognizing @Subscribe annotation


I don't understand, I'm already using the library on other projects and it's working fine.! and I double check the code and it's the same, but this one is just not working.!?


ERROR

java.lang.RuntimeException: Unable to start activity XXX/UserRegisterationActivity}: org.greenrobot.eventbus.EventBusException: 
       Subscriber class XXXX.UserRegisterationActivity and its super classes have no public methods with the @Subscribe annotation

   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2484)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2544)
   at android.app.ActivityThread.access$900(ActivityThread.java:150)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1394)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:168)
   at android.app.ActivityThread.main(ActivityThread.java:5845)
   at java.lang.reflect.Method.invoke(Native Method)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)

Receiving Activity

public class UserRegisterationActivity extends BaseActivity {


  @Subscribe // I made sure to import greenroots's class not google's
    private void initRegistration(UserRegisteration reg) {

    switch (reg.getType()) {
      case 0: // register
        // do stuff
        break;

      case 1: // ticket
        // do other stuff
        break;
      default:
        break;
    }

  }

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

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


}

Event Class

 public class UserRegisteration {

  private int type;
  private UserDetails mUserDetails ;

  public EventRegisteration(int type, UserDetails userDetails) {
    this.type = type;
    this.mUserDetails = userDetails;
  }

  public int getType() {
    return type;
  }

  public void setType(int type) {
    this.type = type;
  }

  public UserDetails getUserDetails() {
    return mUserDetails;
  }

  public void setUserDetails(UserDetails userDetails) {
    mUserDetails= userDetails;
  }

}

Post Activity

    public class MainActivity extends BaseActivity {
    int type =0;
    UserDetails userData;
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initView();

    actionButton.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View view) {
          EventBus.getDefault().post(new UserRegisteration (type, userData));
          startActivity(new Intent(MainActivity.this, UserRegisterationActivity.class)); 
      }
      });

}

Some suggest to add line in ProGrad so it won't remove subscribers and whatnot. so I added these, but still not working

-keepattributes *Annotation*
-keepclassmembers class ** {
    @org.greenrobot.eventbus.Subscribe <methods>;
}
-keep enum org.greenrobot.eventbus.ThreadMode { *; }

# Only required if you use AsyncExecutor
-keepclassmembers class * extends org.greenrobot.eventbus.util.ThrowableFailureEvent {
    <init>(java.lang.Throwable);
}

Solution

  • The error says that you have no public methods with @Subscribe annotation. And indeed your method is private. Try to make it public.

    Also consider using Sticky Events.