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

时间:2018-07-18 05:29:27

标签: java android

当我想为我的RecyclerView设置一些新数据时,所有这些都发生了。 firsTimeOK,但是当我想刷新RecyclerView(代码的其他部分)时,会发生此错误。我了解此错误将针对RecyclerView.Animation,并为此查看以下代码:

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

我将此代码放入片段onCreateView中,很抱歉,我的问题没有解决。

谁能告诉我出了什么问题?谢谢。

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;
}

3 个答案:

答案 0 :(得分:0)

以这种方式代替RecyclerView recyclerView = new RecyclerView(view.getContext());请在您的“ fragment_distance_report_lists”布局内添加RecylerView尝试以这种方式访问​​RecyclerView rv = view.findviewbyid(R.id.recyclerviewid)。

请检查此示例。

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

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;
    }
}

希望这可以解决您的问题!!!!

答案 1 :(得分:0)

查看Layout参数到recyclerview。

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

有关详细说明,请访问此链接 NullPointerException - Attempt to invoke virtual method RecyclerView$ViewHolder.shouldIgnore()' on a null object reference

OR

尝试在活动中使用findViewById()绑定回收者视图。

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

答案 2 :(得分:0)

哦,朋友,我发现我的错误就是我将数据发送到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));

然后是片段

 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();