Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BadParcelableException:ClassNotFoundException when unmarshalling: android.support.v4.app.FragmentManagerState

Tags:

android

I've migrated to SDK Android 27.1.0 3 days ago, and there are some crashs like this one, I can't understand why. It appears (currently) on Android 8 and 6.

BadParcelableException ClassNotFoundException when unmarshalling: android.support.v4.app.FragmentManagerState android.support.v4.app.Fragment.setUserVisibleHint

android.os.Parcel.readParcelableCreator (Parcel.java:2916)
android.os.Parcel.readParcelable (Parcel.java:2842)
android.os.Parcel.readValue (Parcel.java:2745)
android.os.Parcel.readArrayMapInternal (Parcel.java:3114)
android.os.BaseBundle.initializeFromParcelLocked (BaseBundle.java:273)
android.os.BaseBundle.unparcel (BaseBundle.java:226)
android.os.BaseBundle.putBoolean (BaseBundle.java:532)
arrow_right
android.support.v4.app.Fragment.setUserVisibleHint (Fragment.java:960)
android.support.v4.app.FragmentStatePagerAdapter.instantiateItem (FragmentStatePagerAdapter.java:121)
android.support.v4.view.ViewPager.addNewItem (ViewPager.java:1004)
android.support.v4.view.ViewPager.populate (ViewPager.java:1218)
android.support.v4.view.ViewPager.populate (ViewPager.java:1086)
android.support.v4.view.ViewPager$3.run (ViewPager.java:267)
android.view.Choreographer$CallbackRecord.run (Choreographer.java:911)
android.view.Choreographer.doCallbacks (Choreographer.java:723)
android.view.Choreographer.doFrame (Choreographer.java:655)
android.view.Choreographer$FrameDisplayEventReceiver.run (Choreographer.java:897)
android.os.Handler.handleCallback (Handler.java:790)
android.os.Handler.dispatchMessage (Handler.java:99)
android.os.Looper.loop (Looper.java:164)
android.app.ActivityThread.main (ActivityThread.java:6494)
java.lang.reflect.Method.invoke (Method.java)
com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:438)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:807)

This is my adapter:

public abstract class CalendarPagerAdapter extends FragmentStatePagerAdapter {

    private static final String TAG = LogUtils.makeLogTag(CalendarPagerAdapter.class);

    protected DateTime mDateTime;
    private final int mCount;
    protected int mTodayPosition;

    public static class CalendarContext {
        public int mRange; // range = nb of days supported
        public int mTodayPosition; // Today index in this area
        public int mCurrentWeek; // Week number of today
        public DateTime mFrom, mTo; // Compute from and to datetimes
        public boolean mIsSundayFirstDay;

        public CalendarContext(int area, int todayPosition, DateTime from, DateTime to,
                               int currentWeek, boolean isSundayFirstDay) {
            mRange = area;
            mTodayPosition = todayPosition;
            mFrom = from;
            mTo = to;
            mCurrentWeek = currentWeek;
            mIsSundayFirstDay = isSundayFirstDay;
        }
    }

    public static CalendarContext computeAreaAndTodayPosition(int initialArea, int initialTodayPosition) {
        // Compute min / max dates from now
        DateTime from = new DateTime().minusDays(initialArea - initialTodayPosition).dayOfWeek().withMinimumValue();
        DateTime to = new DateTime().plusDays(initialTodayPosition).dayOfWeek().withMaximumValue();

        boolean isSundayFirstDay = false;
        Calendar calendar = Calendar.getInstance(CompatUtils.getLocale(false));
        if (calendar.getFirstDayOfWeek() == Calendar.SUNDAY) {
            isSundayFirstDay = true;
            from = from.minusDays(1);
            to = to.minusDays(1);
        }

        LogUtils.LOGD("XXXX", "from dt=" + from.toString());
        LogUtils.LOGD("XXXX", "to dt=" + to.toString());

        // Compute nb days area supported
        int daysRange = daysBetween(from, to).getDays() + 1;
        LogUtils.LOGD("XXXX", "daysRange=" + daysRange);

        // Compute today position
        int todayPosition = daysBetween(from, DateTime.now().withTimeAtStartOfDay()).getDays() + 1;
        LogUtils.LOGD("XXXX", "todayPosition=" + todayPosition);

        int currentWeek = DateTime.now().getWeekOfWeekyear() - from.getWeekOfWeekyear();
        LogUtils.LOGD("XXXX", "currentWeek=" + currentWeek);

        return new CalendarContext(daysRange, todayPosition, from, to, currentWeek, isSundayFirstDay);
    }

    public CalendarPagerAdapter(FragmentManager mgr, int count, int todayPosition) {
        super(mgr);
        mDateTime = DateTime.now();
        mCount = count;
        mTodayPosition = todayPosition;
    }

    @Override
    public int getCount() {
        return mCount;
    }

    public boolean isTodayPosition(int position) {
        return computeDifferenceDays(position) == 0;
    }

    public boolean isPastPosition(int position) {
        return computeDifferenceDays(position) < 0;
    }

    public boolean isFuturPosition(int position) {
        return computeDifferenceDays(position) > 0;
    }

    protected int computeDifferenceDays(int position) {
        return position - getCalendarTodayPosition();
    }

    public long convertPositionToMs(int position) {
        return convertPositionToMs(mDateTime, position);
    }

    public long convertMinPositionToMs() {
        return convertPositionToMs(mDateTime, 0);
    }

    public long convertMaxPositionToMs() {
        return convertPositionToMs(mDateTime, mCount - 1);
    }

    public String convertPositionToDate(int position) {
        return TimeUnits.dateTimeToDateServer(new DateTime(convertPositionToMs(position)));
    }

    public long convertPositionToMs(DateTime datime, int position) {
        int dayNum = computeDifferenceDays(position);
        if (dayNum < 0)
            return datime.minusDays(Math.abs(dayNum)).getMillis();
        else if (dayNum > 0)
            return datime.plusDays(Math.abs(dayNum)).getMillis();
        else
            return datime.getMillis();
    }

    public int convertMsToPosition(long millis) {
        DateTime dtReceived = new DateTime(millis).withTimeAtStartOfDay();
        return convertDateTimeToPosition(dtReceived);
    }

    public int convertDateTimeToPosition(DateTime dtReceived) {
        DateTime now = DateTime.now().withTimeAtStartOfDay();
        int nbDays = daysBetween(now, dtReceived).getDays();
        return getCalendarTodayPosition() + nbDays;
    }

    public int getCalendarTodayPosition() {
        return mTodayPosition;
    }

    public void shiftWithOffset(WeekDatePicker weekDatePicker, TextView weekDatePickerDayTextView,
                                DateTime currentSelectedDate, int offset) {
        if (offset < 0 && mTodayPosition > 0) mTodayPosition += offset;
        mDateTime = DateTime.now();
        weekDatePicker.refreshTodayPosition();
        weekDatePickerDayTextView.setText(TimeUnits.dateTimeToString(
                currentSelectedDate, true, true, true, true, true));
    }
}

Have you got some ideas guys to fix this problem?

Thank you very much!

like image 946
anthony Avatar asked Mar 12 '18 06:03

anthony


2 Answers

@XJIOP said is correct i also have same issue on my production, but i was unable to find what the crash, i followed @XJIOP path and produce the bug,i resolved the bug by updating the compact version from

appcompat 27.1.0 to 27.1.1

since i am using proguard i also added the following line to my proguard

add the following line to proguard if you are using proguard on your production build else ignore

-keepnames class * implements android.os.Parcelable {
    public static final ** CREATOR;
}

After adding the pro guard line and updating the compact lib i was able to fix the issue and the bug hasent reported yet on my updated production build. hope this will help some others

like image 51
Ramz Avatar answered Nov 17 '22 01:11

Ramz


Same issues after update android library to 27.1.0

My viewPager has 3 tabs, this crash occurs if I select first or last tab, then after loss activity and returning to viewPager chose another tab got this crash.

Tested with 2 tabs has no crash.

My fix is

viewPager.setOffscreenPageLimit(2);

and crash is gone

EDIT

Updating to 27.1.1 has resolved issue

like image 27
XJIOP Avatar answered Nov 17 '22 03:11

XJIOP