其实这两个错误一样的:
错误日志:
java.lang.IllegalStateException: Cannot set menu creator, setAdapter has already been called.
at com.yanzhenjie.recyclerview.SwipeRecyclerView.checkAdapterExist(SwipeRecyclerView.java:245)
at com.yanzhenjie.recyclerview.SwipeRecyclerView.setSwipeMenuCreator(SwipeRecyclerView.java:305)
at com.jfkj.im.adapter.MinefriendAdapter.onBindViewHolder(MinefriendAdapter.java:115)
at com.jfkj.im.adapter.MinefriendAdapter.onBindViewHolder(MinefriendAdapter.java:53)
at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:7065)
at com.yanzhenjie.recyclerview.AdapterWrapper.onBindViewHolder(AdapterWrapper.java:200)
at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:7107)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:6012)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6279)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6118)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6114)
at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2303)
at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1627)
at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1587)
at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:665)
at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4134)
at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3851)
at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4404)
at android.view.View.layout(View.java:22144)
at android.view.ViewGroup.layout(ViewGroup.java:6335)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1839)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1683)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1592)
at android.view.View.layout(View.java:22144)
at android.view.ViewGroup.layout(ViewGroup.java:6335)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:334)
at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
at android.view.View.layout(View.java:22144)
at android.view.ViewGroup.layout(ViewGroup.java:6335)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1839)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1683)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1592)
at android.view.View.layout(View.java:22144)
at android.view.ViewGroup.layout(ViewGroup.java:6335)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:334)
at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
at android.view.View.layout(View.java:22144)
at android.view.ViewGroup.layout(ViewGroup.java:6335)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1839)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1683)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1592)
at android.view.View.layout(View.java:22144)
at android.view.ViewGroup.layout(ViewGroup.java:6335)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:334)
at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
at com.android.internal.policy.DecorView.onLayout(DecorView.java:971)
at android.view.View.layout(View.java:22144)
at android.view.ViewGroup.layout(ViewGroup.java:6335)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:3298)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2798)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1870)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8128)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1057)
2020-06-18 16:48:38.098 16842-16842/com.jfkj.vip E/AndroidRuntime: at android.view.Choreographer.doCallbacks(Choreographer.java:875)
at android.view.Choreographer.doFrame(Choreographer.java:776)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1042)
at android.os.Handler.handleCallback(Handler.java:894)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:213)
at android.app.ActivityThread.main(ActivityThread.java:8210)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101)
我这里是recyclerview嵌套recyclerview 报的错,大概的字面意思就是监听的方法不能再setadpter之前调用。
需要把监听方法放在setadpter之前就好了。
但我这里不同的是,我已经放在之前了,还是报错了。也郁闷了一会儿,无论放在那里都是报错。就想了一些方法,解决了问题,但是很鸡肋,没有使用,因为这里使用的是
SwipeRecyclerView 需要侧滑
/**
* Set to create menu listener.
*/
public void setSwipeMenuCreator(SwipeMenuCreator menuCreator) {
if (menuCreator == null) return;
checkAdapterExist("Cannot set menu creator, setAdapter has already been called.");
this.mSwipeMenuCreator = menuCreator;
}
这里会检查适配器,看到这里报错的信息是一样的,继续走
/**
* Check the Adapter and throw an exception if it already exists.
*/
private void checkAdapterExist(String message) {
if (mAdapterWrapper != null) throw new IllegalStateException(message);
}
看到这里会应该有点明白,如果这个适配器不是null的话就报上面的错误!因为我这样操作报错的,在当前页面没有错误,而是点击一个item之后进入另一个页面,返回来的时候报错,断点查看,会继续调用
onBindViewHolder方法,里面就是嵌套的recyclerview,因为第一次加载的时候,就已经设置了嵌套的recyclerview适配器,这里进来的话就出现,这个嵌套的recycler已经有的,就报错了。
解决方法:
holder.recyc_list.setAdapter(null);
holder.recyc_list.setSwipeMenuCreator(swipeMenuCreator);
holder.recyc_list.setLayoutManager(layoutManager);
在之前把适配器置null就好了!
SwipeRecyclerview使用中一些常见错误处理方法