{"record":{"id":"67a7fe0c4ccb8e11","repo":"material-components/material-components-android","slug":"1-s-must-have-its-adapter-set-to-a-2-s","errorCode":null,"errorMessage":"%1$s must have its Adapter set to a %2$s","messagePattern":"%1\\$s must have its Adapter set to a %2\\$s","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"lib/java/com/google/android/material/datepicker/MaterialCalendarGridView.java","lineNumber":236,"sourceCode":"      // If no next focusable item in this month, return false to let the system move focus\n      // out of the GridView.\n      return false;\n    }\n\n    setSelection(nextPosition);\n    return true;\n  }\n\n  @NonNull\n  @Override\n  public MonthAdapter getAdapter() {\n    return (MonthAdapter) super.getAdapter();\n  }\n\n  @Override\n  public final void setAdapter(ListAdapter adapter) {\n    if (!(adapter instanceof MonthAdapter)) {\n      throw new IllegalArgumentException(\n          String.format(\n              \"%1$s must have its Adapter set to a %2$s\",\n              MaterialCalendarGridView.class.getCanonicalName(),\n              MonthAdapter.class.getCanonicalName()));\n    }\n    super.setAdapter(adapter);\n  }\n\n  private void ensureFocusRingSelector(MonthAdapter monthAdapter) {\n    Drawable selector = getSelector();\n    if (selector instanceof FocusRingDrawable) {\n      return;\n    }\n\n    Drawable drawable = FocusRingDrawable.wrap(getContext(), selector);\n    if (drawable instanceof FocusRingDrawable) {\n      FocusRingDrawable focusRingDrawable = (FocusRingDrawable) drawable;\n      if (monthAdapter.calendarStyle != null) {","sourceCodeStart":218,"sourceCodeEnd":254,"githubUrl":"https://github.com/material-components/material-components-android/blob/ac7e18efeefb331850c561faf9ab8bf81d27ba68/lib/java/com/google/android/material/datepicker/MaterialCalendarGridView.java#L218-L254","documentation":"MaterialCalendarGridView (the grid inside MaterialDatePicker) requires its ListAdapter to be a MonthAdapter so it can drive day cells, selectors and navigation. setAdapter is final and throws IllegalArgumentException, formatted with the class names, when given any other adapter type. The message reads '%1$s must have its Adapter set to a %2$s' after String.format.","triggerScenarios":"Calling gridView.setAdapter(anyNonMonthAdapter) on a MaterialCalendarGridView; subclassing MaterialDatePicker internals and injecting a custom adapter; reflection-based testing tools swapping adapters on arbitrary ListViews/GridViews.","commonSituations":"Test frameworks (Espresso/Robotium recorders, screenshot utils) that iterate all AbsListView instances and reset adapters; custom date picker forks that reuse the grid with their own adapter.","solutions":["Do not set a foreign adapter on MaterialCalendarGridView; let MaterialDatePicker manage it.","If you subclass, subclass MonthAdapter and keep it compatible (it drives day rendering contracts).","In test/tooling code that touches all adapters, skip views inside MaterialDatePicker."],"exampleFix":"// before\ngridView.adapter = MyCustomAdapter(items) // not a MonthAdapter -> throws\n\n// after\nclass MyMonthAdapter(month: Month, selector: DateSelector<Long>) : MonthAdapter(month, selector)\ngridView.adapter = MyMonthAdapter(month, selector)","handlingStrategy":"type-guard","validationCode":"fun setAdapterSafely(grid: AbsListView, adapter: ListAdapter) {\n  if (grid is MaterialCalendarGridView && adapter !is MonthAdapter) return\n  grid.adapter = adapter\n}","typeGuard":"fun supportsAdapter(grid: AbsListView, adapter: ListAdapter): Boolean =\n    !(grid is MaterialCalendarGridView && adapter !is MonthAdapter)","tryCatchPattern":null,"preventionTips":["Treat MaterialCalendarGridView.setAdapter as internal to MaterialDatePicker.","In UI test/screenshot harnesses, skip adapter manipulation inside date pickers.","Subclass MonthAdapter, never replace it, when customizing day cells."],"tags":["android","material-components","datepicker","adapter","type-guard"],"backgroundTag":null,"analyzedSha":"ac7e18efeefb331850c561faf9ab8bf81d27ba68","analyzedAt":"2026-08-14T15:35:35.735Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}