当前位置: 代码迷 >> java >> Recyclerview java.lang.IndexOutOfBoundException
  详细解决方案

Recyclerview java.lang.IndexOutOfBoundException

热度:52   发布时间:2023-07-25 19:10:20.0

当我向下滚动太快并点击我登陆的任何项目时,我得到以下错误然后应用程序崩溃:java.lang.IndexOutOfBoundsException:检测到不一致。 无效的视图持有者适配器positionViewHolder {440a7260 position = 15 id = -1,oldPos = 14,pLpos:14 scrap tmpDetached no parent}我已经读过它是recyclerview中的(已知)错误。 有人有办法绕过它吗? 也许限制滚动以使所有JSON数据在recyclerview中正确加载

我使用以下方法发送我的JSON请求

private void sendJsonRequest(){
    JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, urlUpcoming, (String) null, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            //Toast.makeText(getActivity(), response.toString(), Toast.LENGTH_LONG).show();
            gamesList.addAll(parseJsonResponse(response));
            adapterUpcoming.notifyDataSetChanged();
            adapterUpcoming.setData(gamesList);
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            //error getting data
        }
    });

    //add the request
    mRequestQueue.add(request);
}

parseJsonResponse返回一个数组列表,这就是魔术发生的地方(从api获取数据)

 private ArrayList<Game> parseJsonResponse(JSONObject response) {
    final ArrayList<Game> games = new ArrayList<>();
    //{fetching data with volley} code omitted
    return games;
}

OnCreateView

 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_upcoming, container, false);
    mRecyclerView = (RecyclerView) view.findViewById(R.id.upcomingGamesList);
    mLayoutManager = new LinearLayoutManager(getActivity());
    mRecyclerView.setLayoutManager(mLayoutManager);
    adapterUpcoming = new AdapterUpcoming(getActivity());
    mRecyclerView.setAdapter(adapterUpcoming);
    urlUpcoming = MyApplication.getUrl(); //the url from which I'm fetching my data 
    sendJsonRequest();
    return view;

错误(错误相同但值不同):

E/AndroidRuntime? FATAL EXCEPTION: main
java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder{52ea27c0 position=5 id=-1, oldPos=4, pLpos:4 scrap tmpDetached no parent}
        at android.support.v7.widget.RecyclerView$Recycler.validateViewHolderForOffsetPosition(RecyclerView.java:3229)
        at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:3359)
        at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:3340)
        at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1810)
        at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1306)
        at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1269)
        at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:523)
        at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:1942)
        at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:2237)
        at android.view.View.layout(View.java:14289)
        at android.view.ViewGroup.layout(ViewGroup.java:4562)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
        at android.view.View.layout(View.java:14289)
        at android.view.ViewGroup.layout(ViewGroup.java:4562)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
        at android.view.View.layout(View.java:14289)
        at android.view.ViewGroup.layout(ViewGroup.java:4562)
        at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1594)
        at android.view.View.layout(View.java:14289)
        at android.view.ViewGroup.layout(ViewGroup.java:4562)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
        at android.view.View.layout(View.java:14289)
        at android.view.ViewGroup.layout(ViewGroup.java:4562)
        at android.support.v7.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:502)
        at android.view.View.layout(View.java:14289)
        at android.view.ViewGroup.layout(ViewGroup.java:4562)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
        at android.view.View.layout(View.java:14289)
        at android.view.ViewGroup.layout(ViewGroup.java:4562)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
        at android.view.View.layout(View.java:14289)
        at android.view.ViewGroup.layout(ViewGroup.java:4562)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
        at android.view.View.layout(View.java:14289)
        at android.view.ViewGroup.layout(ViewGroup.java:4562)
        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1976)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1730)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
        at android.view.Choreographer.doCallbacks(Choreographer.java:562)
        at android.view.Choreographer.doFrame(Choreographer.java:532)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
        at android.os.Handler.handleCallback(Handler.java:730)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5103)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        at dalvik.system.NativeStart.main(Native Method)

谢谢!!

请始终发布您需要帮助的代码。 还要确保此代码简洁且评论良好。 但是,IndexOutOfBounds异常意味着您正在尝试查看字符串或数组。 并且看负数或数字太高。 但它表示位置15因此它将超过阵列的末尾。 尝试添加一个check语句if(oldPosition + 1

  相关解决方案