{"record":{"id":"dd59d4c4464fafc3","repo":"huanghaibin-dev/CalendarView","slug":"e-printstacktrace","errorCode":null,"errorMessage":"e.printStackTrace()","messagePattern":"e\\.printStackTrace\\(\\)","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"calendarview/src/main/java/com/haibin/calendarview/CalendarView.java","lineNumber":108,"sourceCode":"        init(context);\n    }\n\n    /**\n     * 初始化\n     *\n     * @param context context\n     */\n    private void init(Context context) {\n        LayoutInflater.from(context).inflate(R.layout.cv_layout_calendar_view, this, true);\n        FrameLayout frameContent = findViewById(R.id.frameContent);\n        this.mWeekPager = findViewById(R.id.vp_week);\n        this.mWeekPager.setup(mDelegate);\n\n        try {\n            Constructor constructor = mDelegate.getWeekBarClass().getConstructor(Context.class);\n            mWeekBar = (WeekBar) constructor.newInstance(getContext());\n        } catch (Exception e) {\n            e.printStackTrace();\n        }\n\n        frameContent.addView(mWeekBar, 2);\n        mWeekBar.setup(mDelegate);\n        mWeekBar.onWeekStartChange(mDelegate.getWeekStart());\n\n        this.mWeekLine = findViewById(R.id.line);\n        this.mWeekLine.setBackgroundColor(mDelegate.getWeekLineBackground());\n        LayoutParams lineParams = (LayoutParams) this.mWeekLine.getLayoutParams();\n        lineParams.setMargins(mDelegate.getWeekLineMargin(),\n                mDelegate.getWeekBarHeight(),\n                mDelegate.getWeekLineMargin(),\n                0);\n        this.mWeekLine.setLayoutParams(lineParams);\n\n        this.mMonthPager = findViewById(R.id.vp_month);\n        this.mMonthPager.mWeekPager = mWeekPager;\n        this.mMonthPager.mWeekBar = mWeekBar;","sourceCodeStart":90,"sourceCodeEnd":126,"githubUrl":"https://github.com/huanghaibin-dev/CalendarView/blob/f5479ea3baefdbba2453cea19a208eca83baeb9e/calendarview/src/main/java/com/haibin/calendarview/CalendarView.java#L90-L126","documentation":"CalendarView.init() reflectively instantiates the custom WeekBar class configured via app:calendar_week_bar_view. The constructor lookup (Context argument) or newInstance fails, and the library swallows the exception with printStackTrace, leaving mWeekBar null, which then crashes on frameContent.addView(mWeekBar, 2) with a NullPointerException.","triggerScenarios":"A custom WeekBar subclass is declared in XML (app:calendar_week_bar_view=\"com.example.MyWeekBar\") but the class is not public, lacks a public MyWeekBar(Context) constructor, is an inner (non-static) class, or was ProGuard/R8-obfuscated or removed at build time.","commonSituations":"Developers subclass WeekBar inside an Activity as a non-static inner class; enabling minifyEnabled true without a keep rule for the custom view; typo in the fully-qualified class name; the custom class lives in a module not included as a dependency.","solutions":["Ensure the custom WeekBar class is public, top-level or static, and has an explicit public constructor taking Context: public MyWeekBar(Context context).","Check logcat for the swallowed stack trace (ClassNotFoundException / NoSuchMethodException) to identify the exact failure.","Add a ProGuard keep rule: -keep class com.example.MyWeekBar { public <init>(android.content.Context); }.","Verify app:calendar_week_bar_view in XML matches the fully-qualified class name exactly.","As a last resort, temporarily fall back to the default WeekBar (remove the XML attribute) to confirm the custom class is the problem."],"exampleFix":"// before\npublic class CalendarActivity extends AppCompatActivity {\n    class MyWeekBar extends WeekBar { public MyWeekBar(Context c){super(c);} }\n}\n// after\npublic class MyWeekBar extends WeekBar {\n    public MyWeekBar(Context context) { super(context); }\n}","handlingStrategy":"try-catch","validationCode":"// Before setting XML attribute, verify at startup:\nClass<?> c = Class.forName(\"com.example.MyWeekBar\");\nif (!WeekBar.class.isAssignableFrom(c)) throw new IllegalStateException(\"Must extend WeekBar\");\nc.getConstructor(Context.class); // throws if constructor missing\n","typeGuard":"static boolean isValidWeekBar(Class<?> cls) {\n    return WeekBar.class.isAssignableFrom(cls)\n        && java.lang.reflect.Modifier.isPublic(cls.getModifiers())\n        && !cls.isMemberClass();\n}","tryCatchPattern":"try {\n    calendarView.setWeekBar(MyWeekBar.class);\n} catch (Throwable t) {\n    Log.e(\"Calendar\", \"Custom WeekBar failed, using default\", t);\n    // continue with library default\n}","preventionTips":["Always declare custom calendar views as public top-level (or static) classes.","Always provide a public constructor that only takes Context.","Add ProGuard keep rules for every custom calendar view class.","Test custom view configuration in a minified release build, not just debug.","Watch logcat at startup for printStackTrace output from CalendarView."],"tags":["android","reflection","view-instantiation","calendarview"],"backgroundTag":"class-not-found","analyzedSha":"f5479ea3baefdbba2453cea19a208eca83baeb9e","analyzedAt":"2026-09-11T10:11:54.823Z","contentChangedAt":"2026-09-11T10:11:54.823Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}