android – Greenrobot的Eventbus:两个片段调用异步任务工作让Eventbus感到困惑
作者:互联网
我目前正在处理使用FragmentPagerAdapter在Activities下创建的Fragments.我使用GreenRobot的EventBus 3.0来返回我从服务类创建的一些AsyncTasks.但是,由于这两个片段是一个接一个地创建的,因此事件总线’post sticky’的返回使得已订阅的片段混合起来.我已经搜索了stackoverflow,做了其他人所做的事情(例如将eventBus寄存器放到onStart和OnStop等等),我仍然看不到类似的问题.希望您能够帮助我.谢谢!
这是我的两个片段:(我省略了一些不必要的代码)
1.状态碎片
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.e(TAG, "StatusFragment onCreateView");
eventBus = EventBus.getDefault();
eventBus.register(this);
StatusService statusService = StatusService.newInstance(getContext());
statusService.getStatusList(); //Async Task to call onReturnAdapter once finished
return mRecyclerView;
}
@Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
public void onReturnList(List<Status> statuses) {
Log.e(TAG, "onReturnList");
mAdapter = new StatusRecyclerAdapter(statuses);
mRecyclerView.setAdapter(mAdapter);
eventBus.removeStickyEvent(statuses);
}
@Override
public void onStop() {
Log.e(TAG, "onStop");
eventBus.unregister(this);
super.onStop();
}
2.状态服务 – 状态片段的服务类
public void getStatusList () {
Log.e(LOG_TAG, "getStatusList");
//do some async tasks here
returnList(statuses);
}
private void returnList(List<Status> statuses) {
Log.e(LOG_TAG, "returnList");
eventBus.postSticky(statuses);
}
3.图表碎片
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.e(LOG_TAG, "onCreateView");
eventBus = EventBus.getDefault();
eventBus.register(this);
chartService = ChartService.newInstance(getContext());
chartService.getDateResult(new DbRequestFeelings());
return view;
}
@Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
public void onReturnResults(List<Result> results) {
Log.e(LOG_TAG, "onReturnResults");
chartService.setupPieChart(results, feelingsPieChart);
}
@Override
public void onStop() {
eventBus.unregister(this);
super.onStop();
}
4.图表服务
public void getDateResult(final DbRequest dbRequest) {
Log.e(LOG_TAG, "getDateResult");
//do some async tasks
returnResults(results);
}
private void returnResults(List<Result> results) {
Log.e(LOG_TAG, "returnResults");
eventBus.postSticky(results);
}
5.结果类
public class Result {
....
}
6.状态类
public class Status {
...
}
因此,每次创建片段时,他们都会调用他们的服务类,而服务类又会发布List< Status>和列表<结果>几乎一个接一个.由于这个原因,它引起了混乱
03-11 11:48:19.685 15148-15148/com.paularagones.moode E/EventBus﹕ Could not dispatch event: class java.util.ArrayList to subscribing class class com.paularagones.moode.Fragments.ChartFragment
java.lang.ClassCastException: com.paularagones.moode.Models.Status cannot be cast to com.paularagones.moode.Models.Result
这是完整的堆栈跟踪:
03-11 11:48:19.589 15148-15148/com.paularagones.moode E/Moode-StatusFragment﹕ StatusFragment onCreate
03-11 11:48:19.589 15148-15148/com.paularagones.moode E/Moode-StatusFragment﹕ StatusFragment onCreateView
03-11 11:48:19.605 15148-15148/com.paularagones.moode E/StatusService﹕ getStatusList
03-11 11:48:19.609 15148-15148/com.paularagones.moode E/ChartFragment﹕ onCreateView
03-11 11:48:19.613 15148-15148/com.paularagones.moode E/ChartService﹕ newInstance
03-11 11:48:19.621 15148-15148/com.paularagones.moode E/ChartService﹕ getDateResult
03-11 11:48:19.681 15148-15148/com.paularagones.moode E/RecyclerView﹕ No adapter attached; skipping layout
03-11 11:48:19.681 15148-15148/com.paularagones.moode E/StatusService﹕ returnList
03-11 11:48:19.681 15148-15148/com.paularagones.moode E/Moode-StatusFragment﹕ onReturnList
03-11 11:48:19.681 15148-15148/com.paularagones.moode E/ChartFragment﹕ onReturnResults
03-11 11:48:19.685 15148-15148/com.paularagones.moode E/EventBus﹕ Could not dispatch event: class java.util.ArrayList to subscribing class class com.paularagones.moode.Fragments.ChartFragment
java.lang.ClassCastException: com.paularagones.moode.Models.Status cannot be cast to com.paularagones.moode.Models.Result
at com.paularagones.moode.Services.ChartService.getData(ChartService.java:147)
at com.paularagones.moode.Services.ChartService.setupPieChart(ChartService.java:129)
at com.paularagones.moode.Fragments.ChartFragment.onReturnResults(ChartFragment.java:116)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at org.greenrobot.eventbus.EventBus.invokeSubscriber(EventBus.java:485)
at org.greenrobot.eventbus.EventBus.postToSubscription(EventBus.java:420)
at org.greenrobot.eventbus.EventBus.postSingleEventForEventType(EventBus.java:397)
at org.greenrobot.eventbus.EventBus.postSingleEvent(EventBus.java:370)
at org.greenrobot.eventbus.EventBus.post(EventBus.java:251)
at org.greenrobot.eventbus.EventBus.postSticky(EventBus.java:292)
at com.paularagones.moode.Services.StatusService.returnList(StatusService.java:80)
at com.paularagones.moode.Services.StatusService.access$100(StatusService.java:24)
at com.paularagones.moode.Services.StatusService$2.onNext(StatusService.java:72)
at com.paularagones.moode.Services.StatusService$2.onNext(StatusService.java:59)
at rx.Observable$30.onNext(Observable.java:8069)
at rx.observers.SafeSubscriber.onNext(SafeSubscriber.java:139)
at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.pollQueue(OperatorObserveOn.java:202)
at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber$2.call(OperatorObserveOn.java:162)
at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
03-11 11:48:19.685 15148-15148/com.paularagones.moode E/ChartService﹕ returnResults
03-11 11:48:19.685 15148-15148/com.paularagones.moode E/Moode-StatusFragment﹕ onReturnList
03-11 11:48:19.685 15148-15148/com.paularagones.moode E/ChartFragment﹕ onReturnResults
03-11 11:48:19.693 15148-15148/com.paularagones.moode E/StatusRecyclerAdapter﹕ onCreateViewHolder
03-11 11:48:19.697 15148-15148/com.paularagones.moode E/StatusRecyclerAdapter﹕ onBindViewHolder
03-11 11:48:19.709 15148-15148/com.paularagones.moode E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.ClassCastException: com.paularagones.moode.Models.Result cannot be cast to com.paularagones.moode.Models.Status
at com.paularagones.moode.Adapters.StatusRecyclerAdapter.onBindViewHolder(StatusRecyclerAdapter.java:101)
at com.paularagones.moode.Adapters.StatusRecyclerAdapter.onBindViewHolder(StatusRecyclerAdapter.java:28)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5277)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5310)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4568)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4461)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1962)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1371)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1334)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:563)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2847)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3145)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4362)
at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1627)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4362)
at android.support.design.widget.CoordinatorLayout.layoutChild(CoordinatorLayout.java:1034)
at android.support.design.widget.CoordinatorLayout.onLayoutChild(CoordinatorLayout.java:744)
at android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42)
at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1180)
at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:757)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4362)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4362)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1420)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4362)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4362)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1420)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4362)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4362)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1866)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1687)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4212)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
at android.view.Choreographer.doCallbacks(Choreographer.java:555)
at android.view.Choreographer.doFrame(Choreographer.java:525)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
你们知道我的代码有什么问题吗?谢谢.
解决方法:
我注意到的第一件事是你没有删除onReturnResults(List< Result>结果)方法中的粘性…不确定这是否有意…但认为应该提到它.
我想强调的主要观点是,有两个onEvent()方法利用相同的通用类List<> – 这可能是一个问题 – 一个小的代码更改和快速测试可以验证这一点
为每个List<>创建一个类
public class Statuses
{
List<Status> list;
public Statuses(List<Status> data) { list = data; }
}
public class Results
{
List<Result> list;
public Results(List<Result> data) { list = data; }
}
更新onEvent()方法签名:
public void onReturnList(Statuses statuses)
public void onReturnResults(Results results)
更新.postSticky()方法调用:
eventBus.postSticky(new Statuses(statuses));
eventBus.postSticky(new Results(results));
当然,您需要从其类成员访问列表(例如):
foreach (var status in statuses.list)
和
foreach (var result in results.list)
如果你看到的问题在这些改变之后就消失了 – 那么假设greenrobot的EventBus在callstack中看到的反射调用期间没有考虑泛型List类中包含的类型是有意义的.它可能只是寻找包含List参数的第一个匹配签名;当有2个或更多匹配时,正确的匹配可能不会被调用.
这是猜测;所以你必须尝试一下,看看它是否有帮助 – 但是当我需要List<>类以避免歧义时,我已经成功地在使用这种类型的实现的项目中使用greenrobot的EventBus.
标签:android,android-fragments,greenrobot-eventbus,greenrobot-eventbus-3-0 来源: https://codeday.me/bug/20190628/1312362.html