Greenrobot 的 Eventbus:调用异步任务作业的两个片段让 Eventbus 感到困惑
Greenrobot's Eventbus : Two Fragments calling async tasks jobs got Eventbus confused
我目前正在研究 Fragments
,它是在 Activities
和 FragmentPagerAdapter
下创建的。我使用 GreenRobot 的 EventBus 3.0 来 return 我从服务 class 创建的一些 AsyncTasks。但是,由于这两个片段是一个接一个创建的,因此事件总线的 return post sticky 将订阅的片段弄混了。我搜索了 Whosebug,做了其他人所做的(例如,将 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。状态服务 - 状态片段
的服务class
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.结果Class
public class Result {
....
}
6.状态 Class
public class Status {
...
}
因此,每次创建片段时,他们都会调用他们的服务 class,这又 post 几乎一个接一个地 List<Status>
和 List<Results>
。因此,它在
的地方造成了混乱
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.access0(StatusService.java:24)
at com.paularagones.moode.Services.StatusService.onNext(StatusService.java:72)
at com.paularagones.moode.Services.StatusService.onNext(StatusService.java:59)
at rx.Observable.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.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> results)
方法中的粘性...不确定这是否是故意的...但认为应该提及。
我想强调的要点是关于有两个 onEvent() 方法利用相同的泛型 class List<>
- 这很可能是问题 - 一个小的代码更改快速测试可以验证这一点
为每个 List<>
创建一个 class
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));
当然,您需要从他们的 class 成员(例如)访问列表:
foreach (var status in statuses.list)
和
foreach (var result in results.list)
如果您看到的问题在进行这些更改后就消失了 - 那么可以假设 greenrobot 的 EventBus 没有考虑通用列表 class 中包含的类型它是在调用堆栈中看到的反射调用。它可能只是寻找第一个包含 List 参数的匹配签名;当有 2 个或更多匹配项时,可能不会调用正确的匹配项。
这是猜测;所以你必须尝试一下,看看它是否有帮助——但是当我需要 classes 的 List<>s 以避免歧义时,我在使用这种类型的实现的项目中成功使用了 greenrobot 的 EventBus .
我目前正在研究 Fragments
,它是在 Activities
和 FragmentPagerAdapter
下创建的。我使用 GreenRobot 的 EventBus 3.0 来 return 我从服务 class 创建的一些 AsyncTasks。但是,由于这两个片段是一个接一个创建的,因此事件总线的 return post sticky 将订阅的片段弄混了。我搜索了 Whosebug,做了其他人所做的(例如,将 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。状态服务 - 状态片段
的服务classpublic 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.结果Class
public class Result {
....
}
6.状态 Class
public class Status {
...
}
因此,每次创建片段时,他们都会调用他们的服务 class,这又 post 几乎一个接一个地 List<Status>
和 List<Results>
。因此,它在
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.access0(StatusService.java:24)
at com.paularagones.moode.Services.StatusService.onNext(StatusService.java:72)
at com.paularagones.moode.Services.StatusService.onNext(StatusService.java:59)
at rx.Observable.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.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> results)
方法中的粘性...不确定这是否是故意的...但认为应该提及。
我想强调的要点是关于有两个 onEvent() 方法利用相同的泛型 class List<>
- 这很可能是问题 - 一个小的代码更改快速测试可以验证这一点
为每个 List<>
class
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));
当然,您需要从他们的 class 成员(例如)访问列表:
foreach (var status in statuses.list)
和
foreach (var result in results.list)
如果您看到的问题在进行这些更改后就消失了 - 那么可以假设 greenrobot 的 EventBus 没有考虑通用列表 class 中包含的类型它是在调用堆栈中看到的反射调用。它可能只是寻找第一个包含 List 参数的匹配签名;当有 2 个或更多匹配项时,可能不会调用正确的匹配项。
这是猜测;所以你必须尝试一下,看看它是否有帮助——但是当我需要 classes 的 List<>s 以避免歧义时,我在使用这种类型的实现的项目中成功使用了 greenrobot 的 EventBus .