我正在使用Viewpager。在里面我正在使用片段,它包含Recyclerview。 每当我更改到另一个视图页面时,它就会崩溃并显示 java.lang.IllegalArgumentException:Scrapped或附加视图可能无法回收。 isScrap:false isAttached:true
java.lang.IllegalArgumentException:可能无法回收废弃或附加的视图。 isScrap:false isAttached:true 在android.support.v7.widget.RecyclerView $ Recycler.recycleViewHolderInternal(未知来源) 在android.support.v7.widget.RecyclerView $ Recycler.recycleView(未知来源) 在android.support.v7.widget.RecyclerView $ LayoutManager.removeAndRecycleViewAt(未知来源) 在android.support.v7.widget.RecyclerView $ LayoutManager.removeAndRecycleAllViews(未知来源) 在android.support.v7.widget.RecyclerView.setAdapterInternal(未知来源) 在android.support.v7.widget.RecyclerView.setAdapter(未知来源) 在com.itc.classmate.om.gq(未知来源) at com.itc.classmate.activities.CreateTimetableActivity $ 6.onPageSelected(Unknown Source) 在android.support.v4.view.ViewPager.dispatchOnPageSelected(未知来源) 在android.support.v4.view.ViewPager.scrollToItem(未知来源) 在android.support.v4.view.ViewPager.setCurrentItemInternal(未知来源) 在android.support.v4.view.ViewPager.setCurrentItemInternal(未知来源) 在android.support.v4.view.ViewPager.setCurrentItem(未知来源) 在android.support.design.widget.TabLayout $ h.b(未知来源) 在android.support.design.widget.TabLayout.b(未知来源) 在android.support.design.widget.TabLayout.a(未知来源) 在android.support.design.widget.TabLayout $ e.select(未知来源) 在android.support.design.widget.TabLayout $ g.performClick(未知来源) 在android.view.View $ PerformClick.run(View.java:21168) 在android.os.Handler.handleCallback(Handler.java:746) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:148) 在android.app.ActivityThread.main(ActivityThread.java:5459) at java.lang.reflect.Method.invoke(Native Method) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:728) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
我试过
你可以看到我的代码来理解这些东西:
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
init();
}
private void init() {
mTimeTableDataList = new ArrayList<>();
mUserID = getArguments().getString(Constant.KEY_USERID);
AppLog.log(TAG, "userid+++ " + mUserID);
if (mCurrDay == -1) {
mCurrDay = ((CreateTimetableActivity) getContext()).mTabItems.get(0).getDayID();
}
}
// java.lang.IllegalArgumentException: Scrapped or attached views may not be recycled. isScrap:false isAttached:true
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = null;
view = inflater.inflate(R.layout.fragment_timetable, container, false);
// mRecyclerViewTimetable = (RecyclerView) view.findViewById(R.id.recyclerview_timetable);
// RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
// layoutManager.setItemPrefetchEnabled(false);
// mRecyclerViewTimetable.setLayoutManager(layoutManager);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mRecyclerViewTimetable = (RecyclerView) getView().findViewById(R.id.recyclerview_timetable);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
layoutManager.setItemPrefetchEnabled(false);
mRecyclerViewTimetable.setLayoutManager(layoutManager);
//-------------------------------------------------------
initView();
initRecyclerViewAdapter();
//--------------------------------------------------------
}
public void update(int pFragmentPostion) {
AppLog.log(TAG, "pDay " + pFragmentPostion);
mCurrDay = pFragmentPostion;
initView();
// if(mTimetableAdapter!=null){
// mTimetableAdapter.notifyDataSetChanged();
// }
initRecyclerViewAdapter();
}
private void initView() {
if (getContext() != null && mCurrDay != -1) {
if (!SharedPreferences.getInstance(getContext()).isMessageAppeared(mUserID)) {
//-----------------------------
TableDays tableDays = new TableDays();
tableDays.openDB(getContext());
TableTimeTableDataModel firstDayOfWeekDay = tableDays.getFirstDayOfWeekDay(mUserID);
int mFirstDay = -1;
if (firstDayOfWeekDay != null) {
mFirstDay = firstDayOfWeekDay.getDayID();
}
tableDays.closeDB();
//----------------------------
AppLog.log(TAG, "mCurrDay:" + mCurrDay + ", mFirstDay:" + mFirstDay);
if (getContext() != null && isFirstDayOfTimetableFilled(mCurrDay, mFirstDay)) {
try {
readFromTimeTable(mCurrDay, mFirstDay);
//pp initRecyclerViewAdapter();
} catch (Exception e) {
AppLog.errLog(TAG, "initView " + e.getMessage());
}
}
} else {
//read data from the table for the currday
TableTimeTable table = new TableTimeTable();
table.openDB(getContext());
mTimeTableDataList = null;
mTimeTableDataList = table.getTimeTableForDay(mUserID, mCurrDay);
table.closeDB();
//-------------------------------------------
//pp initRecyclerViewAdapter();
}
}
}
private boolean isFirstDayOfTimetableFilled(int mCurrDay, int pFirstDay) {
if (mCurrDay != pFirstDay) {
TableTimeTable tableTimeTable = new TableTimeTable();
tableTimeTable.openDB(getContext());
ArrayList<TableTimeTableDataModel> list = tableTimeTable.getTimeTableForDay(mUserID, pFirstDay);
tableTimeTable.closeDB();
if (list.isEmpty()) {
showDialogForTimeTableFirstDayNotFilled();
return false;
}
}
//Toast.makeText(getString(R.string.msg_fill_first_day_timetable), true);
return true;
}
注意:我想清楚我已经看到了stackoverflow的链接,它们有同样的问题并且他们确实试图解决。我也跟着这个步骤仍然问题是坚持主义。