huanghaibin-dev/CalendarView · warning

e.printStackTrace()

Error message

e.printStackTrace()

What it means

WeekViewPager.instantiateItem reflectively constructs the configured custom week view class. On any reflection failure it prints the stack trace and falls back to DefaultWeekView, so the calendar works but the custom week view is silently skipped.

Solutions

  1. Provide a public constructor accepting Context and avoid Activity casts inside it.
  2. Make the class public and top-level (or static).
  3. Read logcat for the underlying NoSuchMethodException/InvocationTargetException.
  4. Add a keep rule preserving the constructor.
  5. Verify the XML attribute names the exact fully-qualified class.

Example fix

// before
public class MyWeekView extends BaseWeekView {
    MyWeekView(Context c) { super(c); } // package-private, reflection fails
}
// after
public class MyWeekView extends BaseWeekView {
    public MyWeekView(Context context) { super(context); }
}
Defensive patterns

Strategy: fallback

Validate before calling

Class<?> cls = Class.forName("com.example.CustomWeekView");
cls.getConstructor(Context.class);

Type guard

static boolean instantiableWeekView(Class<?> cls) {
    try {
        return BaseWeekView.class.isAssignableFrom(cls)
            && cls.getConstructor(Context.class) != null;
    } catch (NoSuchMethodException e) { return false; }
}

Try / catch

// Library falls back to DefaultWeekView; preflight in app code:
try {
    CustomWeekView.class.getConstructor(Context.class).newInstance(context);
} catch (Throwable t) {
    Log.w("Calendar", "CustomWeekView failed, default week view will render", t);
}

Prevention

When it happens

Trigger: Class from app:calendar_week_view_class has no public (Context) constructor, is a non-static inner class, throws in its constructor, or is inaccessible due to obfuscation.

Common situations: Custom BaseWeekView subclass written as inner class; constructor relying on Activity context; release builds with R8 removing/rename-ing the class; class name typo in XML.

Related errors


AI-assisted analysis of huanghaibin-dev/CalendarView@f5479ea3ba (2026-09-11). Data as JSON: /api/errors/6aa00bdac7fd940b. Report an issue: GitHub.

Appendix: source

Thrown at calendarview/src/main/java/com/haibin/calendarview/WeekViewPager.java:461

        @Override
        public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
            return view.equals(object);
        }

        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, int position) {
            Calendar calendar = CalendarUtil.getFirstCalendarStartWithMinCalendar(mDelegate.getMinYear(),
                    mDelegate.getMinYearMonth(),
                    mDelegate.getMinYearDay(),
                    position + 1,
                    mDelegate.getWeekStart());
            BaseWeekView view;
            try {
                Constructor constructor = mDelegate.getWeekViewClass().getConstructor(Context.class);
                view = (BaseWeekView) constructor.newInstance(getContext());
            } catch (Exception e) {
                e.printStackTrace();
                return new DefaultWeekView(getContext());
            }
            view.mParentLayout = mParentLayout;
            view.setup(mDelegate);
            view.setup(calendar);
            view.setTag(position);
            view.setSelectedCalendar(mDelegate.mSelectedCalendar);
            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            BaseWeekView view = (BaseWeekView) object;
            view.onDestroy();
            container.removeView(view);
        }
    }

View on GitHub (pinned to f5479ea3ba)