简体   繁体   English

空对象参考上的android.support.v7.widget.RecyclerView $ ViewHolder.shouldIgnore()'

[英]android.support.v7.widget.RecyclerView$ViewHolder.shouldIgnore()' on a null object reference

Hi all this occurs when I want to set some new data to my RecyclerView . 嗨,所有这些发生在我想向RecyclerView设置一些新数据时。 firsTime it's OK but when I want refresh my RecyclerView (else section in my code) this error occurs. firsTime OK但是当我想要刷新RecyclerView (代码的其他部分)时,会发生此错误。 I understood this error will be for RecyclerView.Animation and see this code for that: 我知道此错误将针对RecyclerView.Animation ,请参见以下代码:

 RecyclerView.ItemAnimator animator = recyclerView.getItemAnimator();
  if (animator instanceof SimpleItemAnimator) {
       ((SimpleItemAnimator) animator).setSupportsChangeAnimations(false);
  }

I put this code to my fragment onCreateView and Sorry, my problem was not resolved. 我将此代码放到片段onCreateView ,对不起,我的问题没有解决。

Can anyone tell me what is going wrong? 谁能告诉我怎么了? Thanks. 谢谢。

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_distance_report_lists, container, false);

    RecyclerView recyclerView = new RecyclerView(view.getContext());
    //((SimpleItemAnimator) recyclerView.getItemAnimator()).setSupportsChangeAnimations(false);

    RecyclerView.ItemAnimator animator = recyclerView.getItemAnimator();
   if (animator instanceof SimpleItemAnimator) {
        ((SimpleItemAnimator) animator).setSupportsChangeAnimations(false);
    }
    if (TextUtils.isEmpty(distance)) {
        recyclerView.setAdapter(new RecyclerViewAdapterDistance(jCars, cntx));
        recyclerView.setLayoutManager(new LinearLayoutManager(view.getContext()));

    } else {

        recyclerView.setAdapter(new RecyclerViewAdapterDistance(cntx, fromDate, fromTime, toDate, toTime, distance, jCars));
        recyclerView.setLayoutManager(new LinearLayoutManager(view.getContext()));
    }
    return recyclerView;
}

Instead of this way RecyclerView recyclerView = new RecyclerView(view.getContext()); 代替这种方式,RecyclerView recyclerView = new RecyclerView(view.getContext()); please add RecylerView inside your "fragment_distance_report_lists" layout try to access this way RecyclerView rv = view.findviewbyid(R.id.recyclerviewid). 请在您的“ fragment_distance_report_lists”布局内添加RecylerView尝试以这种方式访问​​RecyclerView rv = view.findviewbyid(R.id.recyclerviewid)。

Please check this example. 请检查此示例。

fragment_blank.xml fragment_blank.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".BlankFragment">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</FrameLayout>

BlankFragment.java BlankFragment.java

public class BlankFragment extends Fragment {

    private RecyclerView recyclerView;

    public BlankFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_blank, container, false);
        recyclerView = (RecyclerView) view.findViewById(R.id.rv);
        return view;
    }
}

Hope this will resolve your issue cheers!!! 希望这能解决您的问题!

Sey Layout params to the recyclerview. Sey Layout参数返回到recyclerview。

   RecyclerView.LayoutParams params = new RecyclerView.LayoutParams
                                               (ViewGroup.LayoutParams.MATCH_PARENT,
                                                ViewGroup.LayoutParams.WRAP_CONTENT);
            recyclerView.setLayoutParams(params);

For detail description visit this link NullPointerException - Attempt to invoke virtual method RecyclerView$ViewHolder.shouldIgnore()' on a null object reference 有关详细说明,请访问此链接NullPointerException-尝试在空对象引用上调用虚拟方法RecyclerView $ ViewHolder.shouldIgnore()'

OR 要么

Try to bind your recycler view using findViewById() in the activity. 尝试在活动中使用findViewById()绑定您的回收者视图。

RecyclerView mRecyclerView= view.findViewById(R.id.recycler_view)// recycler_view is the id of RecyclerView in XML file.

oh friends i found my mistake thats in to my send data to recyclerView thancks all to help me its tru for me 哦,朋友,我发现我的错误就是我将数据发送到recyclerView了,所有这些都为我提供了帮助

  RecyclerViewAdapterDistance_Map recyclerViewAdapterDistanceMap = new RecyclerViewAdapterDistance_Map(jCars, cntx,false);
        mrecyclerView.setAdapter(recyclerViewAdapterDistanceMap);    
        RecyclerViewAdapterDistance_Map recyclerViewAdapterDistanceMap2 = new RecyclerViewAdapterDistance_Map(cntx, fromDate, fromTime, toDate, toTime, distance, jCars,false);
        mrecyclerView.setAdapter(recyclerViewAdapterDistanceMap2);
    mrecyclerView.setLayoutManager(new LinearLayoutManager(cntx));

and then in fragment 然后片段

 FragmentActivity fragmentRow = getActivity();
                    FragmentManager fragmentManager = fragmentRow.getSupportFragmentManager();
                    Fragment listFragment = fragmentManager.findFragmentById(R.id.carsList);
                    DistanceReportLists distanceReportLists = new DistanceReportLists().newInstance(jcar, cntx, edtFromDate.getText().toString(), tvFromTime.getText().toString(), edtToDate.getText().toString(), tvToTime.getText().toString(), String.valueOf(distance));
                    FragmentTransaction transaction = fragmentRow.getSupportFragmentManager().beginTransaction();
                    if (listFragment != null) {
                        transaction.remove(listFragment);
                    }
                    transaction.replace(R.id.fragment_list, distanceReportLists);
                    transaction.commit();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“RecyclerView$ViewHolder.shouldIgnore()” - java.lang.NullPointerException: Attempt to invoke virtual method 'RecyclerView$ViewHolder.shouldIgnore()' on a null object reference 错误:int android.support.v7.widget.RecyclerView $ ViewHolder.mItemViewType - ERROR : int android.support.v7.widget.RecyclerView$ViewHolder.mItemViewType RecyclerView没有LayoutManager android.support.v7.widget.RecyclerView - RecyclerView has no LayoutManager android.support.v7.widget.RecyclerView 找不到以下类: - android.support.v7.widget.RecyclerView - The following classes could not be found: - android.support.v7.widget.RecyclerView 无法导入导入android.support.v7.widget.RecyclerView; - Cannot import import android.support.v7.widget.RecyclerView; &#39;布尔android.support.v7.widget.RecyclerView $ LayoutManager.canScrollVertically()&#39; - 'boolean android.support.v7.widget.RecyclerView$LayoutManager.canScrollVertically()' android.support.v7.widget.RecyclerView无法实例化 - android.support.v7.widget.RecyclerView failed to instantiate eclipse中无法启动android.support.v7.widget.Recyclerview错误 - android.support.v7.widget.Recyclerview could not be initiated error in eclipse android.support.v4.widget.NestedScrollView无法转换为android.support.v7.widget.RecyclerView - android.support.v4.widget.NestedScrollView cannot be cast to android.support.v7.widget.RecyclerView 尝试调用虚方法 &#39;void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager - Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM