如何防止Android GreedoLayout越界异常?

时间:2017-12-28 09:30:37

标签: android android-recyclerview android-adapter

如何防止Greedo访问超出范围的索引?

以下是堆栈跟踪:

---------崩溃的开始

  

12-28 17:19:22.338 8668-8668 / com.pixerf.android E / AndroidRuntime:FATAL EXCEPTION:main                                                                     过程:com.pixerf.android,PID:8668                                                                     java.lang.IndexOutOfBoundsException:索引:56,大小:56                                                                         at java.util.ArrayList.get(ArrayList.java:411)                                                                         在com.fivehundredpx.greedolayout.GreedoLayoutSizeCalculator.sizeForChildAtPosition(GreedoLayoutSizeCalculator.java:71)                                                                         在com.fivehundredpx.greedolayout.GreedoLayoutManager.sizeForChildAtPosition(GreedoLayoutManager.java:282)                                                                         在com.fivehundredpx.greedolayout.GreedoLayoutManager.preFillGrid(GreedoLayoutManager.java:206)                                                                         在com.fivehundredpx.greedolayout.GreedoLayoutManager.onLayoutChildren(GreedoLayoutManager.java:122)                                                                         在android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3719)                                                                         在android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3436)                                                                         在android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3988)                                                                         在android.view.View.layout(View.java:17523)                                                                         在android.view.ViewGroup.layout(ViewGroup.java:5612)                                                                         在android.support.v4.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:611)                                                                         在android.view.View.layout(View.java:17523)                                                                         在android.view.ViewGroup.layout(ViewGroup.java:5612)                                                                         在android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)                                                                         在android.view.View.layout(View.java:17523)                                                                         在android.view.ViewGroup.layout(ViewGroup.java:5612)                                                                         在android.support.v4.view.ViewPager.onLayout(ViewPager.java:1769)                                                                         在android.view.View.layout(View.java:17523)                                                                         在android.view.ViewGroup.layout(ViewGroup.java:5612)                                                                         在android.support.design.widget.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:132)                                                                         在android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42)                                                                         在android.support.design.widget.AppBarLayout $ ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1361)                                                                         在android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:874)                                                                         在android.view.View.layout(View.java:17523)                                                                         在android.view.ViewGroup.layout(ViewGroup.java:5612)                                                                         在android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)                                                                         在android.widget.FrameLayout.onLayout(FrameLayout.java:261)                                                                         在android.view.View.layout(View.java:17523)                                                                         在android.view.ViewGroup.layout(ViewGroup.java:5612)                                                                         在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)                                                                         在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)                                                                         在android.widget.LinearLayout.onLayout(LinearLayout.java:1494)                                                                         在android.view.View.layout(View.java:17523)                                                                         在android.view.ViewGroup.layout(ViewGroup.java:5612)                                                                         在android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)                                                                         在android.widget.FrameLayout.onLayout(FrameLayout.java:261)                                                                         在android.view.View.layout(View.java:17523)                                                                         在android.view.ViewGroup.layout(ViewGroup.java:5612)                                                                         在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)                                                                         在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)                                                                         在android.widget.LinearLayout.onLayout(LinearLayout.java:1494)                                                                         在android.view.View.layout(View.java:17523)                                                                         在android.view.ViewGroup.layout(ViewGroup.java:5612)                                                                         在android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)                                                                         在android.widget.FrameLayout.onLayout(FrameLayout.java:261)                                                                         在com.android.internal.policy.DecorView.onLayout(DecorView.java:724)                                                                         在android.view.View.layout(View.java:17523)                                                                         在android.view.ViewGroup.layout(ViewGroup.java:5612)                                                                         在android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2342)                                                                         在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2069)                                                                         在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1246)                                                                         在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:6301)                                                                         在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:871)                                                                         在android.view.Choreographer.doCallbacks(Choreographer.java:683)                                                                         在android.view.Choreographer.doFrame(Choreographer.java:619)                                                                         在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:857)                                                                         在android.os.Handler.handleCallback(Handler.java:751)   12-28 17:19:22.339 8668-8668 / com.pixerf.android E / AndroidRuntime:at android.os.Handler.dispatchMessage(Handler.java:95)                                                                         在android.os.Looper.loop(Looper.java:154)                                                                         在android.app.ActivityThread.main(ActivityThread.java:6077)                                                                         at java.lang.reflect.Method.invoke(Native Method)                                                                         在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:866)                                                                         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

1 个答案:

答案 0 :(得分:0)

检查你的arraylist或数组大小,然后执行你的任务

if(arraylist.size()>0)
{
//do something 
}