[英]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.