我在一个库中有一个带注释的 Activity ,该 Activity 是同一库中EventBus事件的订阅者。看起来像这样,大大简化了:
@EActivity(resName = "activity_foo")
public class Foo extends Activity {
public void onEvent(BarEvent event){
doSomething();
}
}
它应根据以下方式工作:
http://timnew.me/blog/2014/09/14/otto-and-android-annotations-compatibility-issue-analysis/
但实际上,它会返回此错误:
看来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)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NoSuchFieldException: PostThread
at java.lang.Class.getDeclaredField(Class.java:890)
at libcore.reflect.AnnotationAccess.decodeValue(AnnotationAccess.java:685)
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)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
而且我注意到这两个问题(缺少onEvent和现在的PostThread只是发行版中的一个问题。我将ProGuard与EventBus和AA的建议异常(exception)一起使用)。
编辑2 :
我加了
-keep class de.greenrobot.** {*;}
它似乎正在工作。
最佳答案
您有三种选择:
顺便说一句,您应该更新到最新的AndroidAnnotations 3.3.1。
关于AndroidAnnotations和EventBus,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30990480/