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
- Provide a public constructor accepting Context and avoid Activity casts inside it.
- Make the class public and top-level (or static).
- Read logcat for the underlying NoSuchMethodException/InvocationTargetException.
- Add a keep rule preserving the constructor.
- 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
- Public top-level class with a public (Context) constructor.
- No Activity casting or throwing code in the constructor.
- Keep rules for release builds; test week paging in a minified build.
- Confirm XML attribute matches the class after every rename.
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)