我正在开发一个我想使用Android Annotations和Otto Bus Event from Square
的应用为了将这两个库集成在一起,我遵循了这个link here。此外,我使用了那里建议的Otto 2.0-wip
库而不是Otto Git
。
这就是我实施的方式:
我为总线创建了一个单例类:
@EBean(scope = EBean.Scope.Singleton)
public class BusProviderAA extends BasicBus{
}
我在my Fragment Class
内为此类声明了一个对象,我想在事件中subscribe
:
@Bean
BusProviderAA ottoBus;
@AfterInject
protected void initAfterInjectMFragment()
.... // my stuff here
ottoBus.register(this);
}
@Override
public void onStop() {
super.onStop();
ottoBus.unregister(this);
}
@Subscribe
public void onChangeUserDetailsEvent(ChangeUserDetailsEvent mEvent){
Log.e(" s onChangeUserDetailsEvent", "ss onChangeUserDetailsEvent");
if(mEvent.msg.contains("Data_changed")){
//TODO
}
}
当my communicator class
从服务器通信获得callback
时,我将事件发送到总线。
这是我post event
到我班上班车的方式:
@Bean //To use this enhanced class in another enhanced class or in an enhanced Android component, use @Bean:
BusProviderAA ottoBus;
public void callbackResponse(....){
....// my stuff here and after callback
ottoBus.post(changeUserDetailsEvent(serverResponse.getMsg()));
}
@Produce
public ChangeUserDetailsEvent changeUserDetailsEvent(String msg){
return new ChangeUserDetailsEvent(msg);
}
这是我的ChangeUserDetailsEvent
课程:
public class ChangeUserDetailsEvent {
public final String msg;
public ChangeUserDetailsEvent(String msg) {
this.msg = msg;
}
}
问题:
我的问题是我在公共事件onChangeUserDetailsEvent
订阅的方法没有被调用,我甚至不知道如何调试来解决这个问题。
我应该提一下,当我在没有使用Annotations的Fragment
和没有Annotation的Bus
单例中实现它时,总线事件可以正常工作。当我不使用Annotations时,这是Bus singleton类:
public class BusProvider {
private static final Bus BUS = new BasicBus(); // me lib
public static Bus getInstance(){
return BUS;
}
private BusProvider(){}
}
谢谢!
修改
当我更新androidannotations:3.2
从gradle file
更改时,我也遇到了问题:
compile 'org.androidannotations:androidannotations:3.1'
apt 'org.androidannotations:androidannotations:3.1'
到:
compile 'org.androidannotations:androidannotations:3.2'
apt 'org.androidannotations:androidannotations:3.2'
。
对于3.1版,它编译但不起作用otto event bus
。对于版本3.2,它给出了错误。这意味着AA库已经做了一些我需要实现的更改,或者它有bug。我怎样才能找到解决方案?
这是其中一个错误(用于演示):错误:(27,25)错误:找不到符号类UserAccountActivity_这是一个活动类。
这是我的androidannotations.log:
17:26:05.187 [Daemon Thread 13] INFO o.a.AndroidAnnotationProcessor:84 - Initialize AndroidAnnotations 3.2 with options {androidManifestFile=C:\Users\Armando\Android Studio\Hu\app\build\intermediates\manifests\full\debug\AndroidManifest.xml, resourcePackageName=XXPack}
17:26:05.244 [Daemon Thread 13] INFO o.a.AndroidAnnotationProcessor:108 - Start processing for 15 annotations on 100 elements
17:26:05.273 [Daemon Thread 13] DEBUG o.a.h.AndroidManifestFinder:98 - AndroidManifest.xml file found with specified path: C:\Users\Armando\Android Studio\Hu\app\build\intermediates\manifests\full\debug\AndroidManifest.xml
17:26:05.279 [Daemon Thread 13] INFO o.a.AndroidAnnotationProcessor:171 - AndroidManifest.xml found: AndroidManifest [applicationPackage=XXPack, componentQualifiedNames=[XXPack.SplashScreen, XXPack.HomeActivity, XXPack.ActivityProve, XXPack.UserAccountActivity_, XXPack.CartActivity_, com.facebook.LoginActivity], permissionQualifiedNames=[android.permission.INTERNET, android.permission.READ_EXTERNAL_STORAGE, android.permission.write_external_storage], applicationClassName=null, libraryProject=false, debugabble=false, minSdkVersion=14, maxSdkVersion=-1, targetSdkVersion=21]
17:26:05.280 [Daemon Thread 13] INFO o.a.r.ProjectRClassFinder:50 - Found project R class: XXPack.R
17:26:05.286 [Daemon Thread 13] INFO o.a.r.AndroidRClassFinder:44 - Found Android class: android.R
17:26:05.304 [Daemon Thread 13] INFO o.a.p.ModelValidator:42 - Validating elements
17:26:05.304 [Daemon Thread 13] DEBUG o.a.p.ModelValidator:62 - Validating with EActivityHandler: [XXPack.CartActivity, XXPack.UserAccountActivity]
17:26:05.306 [Daemon Thread 13] DEBUG o.a.p.ModelValidator:62 - Validating with EFragmentHandler: [XXPack.fragments.AddAddressFragment, XXPack.fragments.AddressBookFragment, XXPack.fragments.ChangePasswordFragment, XXPack.fragments.MyOrdersFragment, XXPack.fragments.MyVouchersFragment, XXPack.fragments.NewsLetterFragment, XXPack.fragments.PaymentMethodFragment, XXPack.fragments.PersonalDataFragment, XXPack.fragments.UserAccountFragment]
17:26:05.308 [Daemon Thread 13] DEBUG o.a.p.ModelValidator:62 - Validating with EBeanHandler: [XXPack.bus.BusProviderAA, XXPack.communicator.UserAccountCommunicator]
17:26:05.308 [Daemon Thread 13] DEBUG o.a.p.ModelValidator:62 - Validating with EViewGroupHandler: [XXPack.layouts.AddressBookItem, XXPack.layouts.CartItem, XXPack.layouts.OrdersItem, XXPack.layouts.UserAccountListFooter, XXPack.layouts.UserAccountListHeader]
17:26:05.319 [Daemon Thread 13] DEBUG o.a.p.ModelValidator:62 - Validating with ItemClickHandler: [lv_user_account(int)]
17:26:05.321 [Daemon Thread 13] DEBUG o.a.p.ModelValidator:62 - Validating with OptionsMenuHandler: [XXPack.fragments.UserAccountFragment]
17:26:05.323 [Daemon Thread 13] DEBUG o.a.p.ModelValidator:62 - Validating with BeanHandler: [ottoBus, ottoBus, communicator]
17:26:05.324 [Daemon Thread 13] DEBUG o.a.p.ModelValidator:62 - Validating with ProduceHandler: [produceNonceEvent(XXPack.models.Nonce), produceErrorEvent(XXPack.models.ErrorCommunication), produceFeatureCategoryEvent(java.util.ArrayList<XXPack.models.SimpleCategory>), produceErrorEvent(XXPack.models.ErrorCommunication), produceProductEvent(java.util.ArrayList<XXPack.models.Product>), produceErrorEvent(XXPack.models.ErrorCommunication), changeUserDetailsEvent(java.lang.String), produceErrorEvent(XXPack.models.ErrorCommunication), produceUserEvent(XXPack.models.User), produceErrorEvent(XXPack.models.ErrorCommunication)]
17:26:05.325 [Daemon Thread 13] WARN o.a.p.ModelValidator:69 - Element ProduceHandler unvalidated by
17:26:05.325 [Daemon Thread 13] WARN o.a.p.ModelValidator:69 - Element ProduceHandler unvalidated by
17:26:05.325 [Daemon Thread 13] WARN o.a.p.ModelValidator:69 - Element ProduceHandler unvalidated by
17:26:05.325 [Daemon Thread 13] WARN o.a.p.ModelValidator:69 - Element ProduceHandler unvalidated by
17:26:05.326 [Daemon Thread 13] WARN o.a.p.ModelValidator:69 - Element ProduceHandler unvalidated by
17:26:05.326 [Daemon Thread 13] WARN o.a.p.ModelValidator:69 - Element ProduceHandler unvalidated by
17:26:05.326 [Daemon Thread 13] ERROR o.a.h.AnnotationHelper:126 - @com.squareup.otto.Produce can only be used on a method with zero parameter, instead of 1
17:26:05.327 [Daemon Thread 13] WARN o.a.p.ModelValidator:69 - Element ProduceHandler unvalidated by
17:26:05.327 [Daemon Thread 13] ERROR o.a.h.AnnotationHelper:126 - @com.squareup.otto.Produce can only be used on a method with zero parameter, instead of 1
17:26:05.327 [Daemon Thread 13] WARN o.a.p.ModelValidator:69 - Element ProduceHandler unvalidated by
17:26:05.327 [Daemon Thread 13] WARN o.a.p.ModelValidator:69 - Element ProduceHandler unvalidated by
17:26:05.328 [Daemon Thread 13] WARN o.a.p.ModelValidator:69 - Element ProduceHandler unvalidated by
17:26:05.328 [Daemon Thread 13] DEBUG o.a.p.ModelValidator:62 - Validating with SubscribeHandler: [onNonceEvent(XXPack.event.NonceEvent), onErrorEvent(XXPack.event.ErrorEvent), onFeaturesAndCategory(XXPack.event.FeatureCategoryEvent), onNonceEvent(XXPack.event.NonceEvent), onErrorEvent(XXPack.event.ErrorEvent), onUserEvent(XXPack.event.UserEvent), onChangeUserDetailsEvent(XXPack.event.ChangeUserDetailsEvent)]
17:26:05.328 [Daemon Thread 13] WARN o.a.p.ModelValidator:69 - Element SubscribeHandler unvalidated by
17:26:05.328 [Daemon Thread 13] WARN o.a.p.ModelValidator:69 - Element SubscribeHandler unvalidated by
17:26:05.328 [Daemon Thread 13] WARN o.a.p.ModelValidator:69 - Element SubscribeHandler unvalidated by
17:26:05.328 [Daemon Thread 13] WARN o.a.p.ModelValidator:69 - Element SubscribeHandler unvalidated by
17:26:05.328 [Daemon Thread 13] WARN o.a.p.ModelValidator:69 - Element SubscribeHandler unvalidated by
17:26:05.328 [Daemon Thread 13] WARN o.a.p.ModelValidator:69 - Element SubscribeHandler unvalidated by
17:26:05.328 [Daemon Thread 13] DEBUG o.a.p.ModelValidator:62 - Validating with AfterInjectHandler: [initAfterInjectCart(), initAfterInjectAddAddress(), initAfterInjectAddressBook(), initAfterInjectChangePass(), initAfterInjectAddressBook(), initAfterInjectPersonalData()]
17:26:05.328 [Daemon Thread 13] DEBUG o.a.p.ModelValidator:62 - Validating with AfterViewsHandler: [initAfterViewsCart(), initUserAccountAct(), initAddAddressFragment(), initAddressFragment(), initChangePassFragment(), initMyOrdersFragment(), initMyVoucherFragment(), initNewsLetterFragment(), initPaymentFragment(), initViewsAfterViews(), initUserAccountFragment()]
17:26:05.333 [Daemon Thread 13] INFO o.a.p.ModelProcessor:69 - Processing root elements
17:26:05.338 [Daemon Thread 13] DEBUG o.a.p.ModelProcessor:160 - Processing root elements EActivityHandler: [XXPack.UserAccountActivity, XXPack.CartActivity]
17:26:05.353 [Daemon Thread 13] DEBUG o.a.p.ModelProcessor:160 - Processing root elements EFragmentHandler: [XXPack.fragments.AddressBookFragment, XXPack.fragments.AddAddressFragment, XXPack.fragments.NewsLetterFragment, XXPack.fragments.MyOrdersFragment, XXPack.fragments.UserAccountFragment, XXPack.fragments.PaymentMethodFragment, XXPack.fragments.PersonalDataFragment, XXPack.fragments.ChangePasswordFragment, XXPack.fragments.MyVouchersFragment]
17:26:05.358 [Daemon Thread 13] DEBUG o.a.p.ModelProcessor:160 - Processing root elements EBeanHandler: [XXPack.bus.BusProviderAA, XXPack.communicator.UserAccountCommunicator]
17:26:05.358 [Daemon Thread 13] DEBUG o.a.p.ModelProcessor:160 - Processing root elements EViewGroupHandler: [XXPack.layouts.CartItem, XXPack.layouts.UserAccountListFooter, XXPack.layouts.OrdersItem, XXPack.layouts.AddressBookItem, XXPack.layouts.UserAccountListHeader]
17:26:05.363 [Daemon Thread 13] INFO o.a.p.ModelProcessor:77 - Processing enclosed elements
17:26:05.368 [Daemon Thread 13] INFO o.a.AndroidAnnotationProcessor:250 - Number of files generated by AndroidAnnotations: 18
17:26:05.368 [Daemon Thread 13] INFO o.a.g.ApiCodeGenerator:52 - Writting following API classes in project: []
17:26:05.373 [Daemon Thread 13] DEBUG o.a.g.SourceCodewriter:55 - Generating class: XXPack.bus.BusProviderAA_
17:26:05.388 [Daemon Thread 13] DEBUG o.a.g.SourceCodewriter:55 - Generating class: XXPack.layouts.AddressBookItem_
17:26:05.456 [Daemon Thread 13] DEBUG o.a.g.SourceCodewriter:55 - Generating class: XXPack.layouts.CartItem_
17:26:05.468 [Daemon Thread 13] DEBUG o.a.g.SourceCodewriter:55 - Generating class: XXPack.layouts.OrdersItem_
17:26:05.480 [Daemon Thread 13] DEBUG o.a.g.SourceCodewriter:55 - Generating class: XXPack.layouts.UserAccountListFooter_
17:26:05.491 [Daemon Thread 13] DEBUG o.a.g.SourceCodewriter:55 - Generating class: XXPack.layouts.UserAccountListHeader_
17:26:05.500 [Daemon Thread 13] DEBUG o.a.g.SourceCodewriter:55 - Generating class: XXPack.CartActivity_
17:26:05.514 [Daemon Thread 13] DEBUG o.a.g.SourceCodewriter:55 - Generating class: XXPack.UserAccountActivity_
17:26:05.529 [Daemon Thread 13] DEBUG o.a.g.SourceCodewriter:55 - Generating class: XXPack.fragments.AddAddressFragment_
17:26:05.546 [Daemon Thread 13] DEBUG o.a.g.SourceCodewriter:55 - Generating class: XXPack.fragments.AddressBookFragment_
17:26:05.559 [Daemon Thread 13] DEBUG o.a.g.SourceCodewriter:55 - Generating class: XXPack.fragments.ChangePasswordFragment_
17:26:05.587 [Daemon Thread 13] DEBUG o.a.g.SourceCodewriter:55 - Generating class: XXPack.fragments.MyOrdersFragment_
17:26:05.600 [Daemon Thread 13] DEBUG o.a.g.SourceCodewriter:55 - Generating class: XXPack.fragments.MyVouchersFragment_
17:26:05.613 [Daemon Thread 13] DEBUG o.a.g.SourceCodewriter:55 - Generating class: XXPack.fragments.NewsLetterFragment_
17:26:05.624 [Daemon Thread 13] DEBUG o.a.g.SourceCodewriter:55 - Generating class: XXPack.fragments.PaymentMethodFragment_
17:26:05.634 [Daemon Thread 13] DEBUG o.a.g.SourceCodewriter:55 - Generating class: XXPack.fragments.PersonalDataFragment_
17:26:05.649 [Daemon Thread 13] DEBUG o.a.g.SourceCodewriter:55 - Generating class: XXPack.fragments.UserAccountFragment_
17:26:05.664 [Daemon Thread 13] DEBUG o.a.g.SourceCodewriter:55 - Generating class: XXPack.communicator.UserAccountCommunicator_
17:26:05.670 [Daemon Thread 13] INFO o.a.p.TimeStats:81 - Time measurements: [Whole Processing = 426 ms], [Generate Sources = 302 ms], [Process Annotations = 40 ms], [Extract Annotations = 27 ms], [Validate Annotations = 25 ms], [Find R Classes = 18 ms], [Extract Manifest = 6 ms],
17:26:05.671 [Daemon Thread 13] INFO o.a.AndroidAnnotationProcessor:122 - Finish processing
答案 0 :(得分:3)
AndroidAnnotations 3.1有一个错误:它没有处理Produce
和Subscribe
注释。在AA 3.2中我解决了这个问题。
您可以问为什么AA必须处理这些注释?不幸的是Otto无法从父类中读取注释。但是使用AA,您始终使用生成的子类。这意味着当Otto处理生成的子类时,它没有找到任何Otto注释,这就是为什么你的事件处理程序方法没有被调用。为了解决此问题,我们添加了两个注释处理程序,它们处理Subscribe
和Produce
。这些处理程序仅覆盖生成的类中的方法,并将Otto注释复制到这些方法。这样Otto就可以读取生成的类中的注释。
例如:
@EActivity(R.layout.activity_my)
class MyActivity extends Activity{
@Produce
public Event produce() {
return ...; // create the event
}
}
class MyActivity_ extends MyActivity {
...
@Produce
@Override
public Event produce() {
return super.produce();
}
}
@Produce
注释不能有任何参数,因为Otto在调用方法(Otto doc)时无法弄清楚要传递给它的内容。