EventBus - Class 无法识别@Subscribe 注释

EventBus - Class not recognizing @Subscribe annotation

我不明白,我已经在其他项目中使用该库并且运行良好。!我仔细检查了代码,它是一样的,但是这个代码不起作用。!?


错误

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.access0(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)

接收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);
  }


}

事件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)); 
      }
      });

}

有人建议在 ProGrad 中添加一行,这样它就不会删除订阅者等等。所以我添加了这些,但仍然无法正常工作

-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);
}

错误提示您没有带有 @Subscribe 注释的 public 方法。事实上你的方法是私有的。努力做到public.

也可以考虑使用 Sticky Events