Mr.AndroidShin / Dev Tools

Debugging log · Layouts

InflateException: "Binary XML file line #N"

The screen died the instant it tried to show, with android.view.InflateException: Binary XML file line #42, error inflating class .... The layout rendered perfectly in Android Studio's preview, which made the crash feel unfair. The trap in this one is that the message points at the inflation step and a line number — but the real error is almost always further down the stack trace, in the "Caused by" that explains why that particular view couldn't be built.

The short version: inflating the layout failed on one view. The top line tells you where (the line number and class), not why. Read down to the "Caused by" — that's the actual failure: a theme/attribute the view needs, a vector on an old API, a missing custom-view constructor, or a bad resource. Fix that, not the inflation.

The error

android.view.InflateException: Binary XML file line #42:
  Binary XML file line #42: Error inflating class ...
Caused by: java.lang.reflect.InvocationTargetException
Caused by: ...   <-- the real reason is here

Always scroll to the bottom-most "Caused by." The line number (#42) tells you which element in the XML to look at; the bottom cause tells you what went wrong building it. Together they usually point straight at the fix.

Cause 1 — the wrong theme (AppCompat/Material)

Many widgets require an AppCompat or Material theme to inflate — they read theme attributes that only those themes define. If the Activity (or a dialog/bottom sheet) is using a plain theme, inflation of a Material component fails. The giveaway in the cause is a message about a missing theme attribute. The fix is to give the context the right theme — this is the same root problem as the "You need to use a Theme.AppCompat theme" crash, and it bites especially with dialogs and custom contexts that don't inherit the Activity theme.

Cause 2 — a vector drawable on an old API

A app:srcCompat vector, or a vector referenced somewhere the platform can't inflate directly on older versions, can fail during inflation on those devices while working on your newer test device. Make sure vector support is enabled and that you reference vectors in the compat-friendly way, or provide a raster/alternative for the old API. If the crash only happens on older devices, suspect this first.

Cause 3 — a custom view missing the right constructor

When you write a custom View, the inflater creates it by reflection using the two-argument (Context, AttributeSet) constructor. If your class only defines (Context), inflation throws — which is why the cause is often an InvocationTargetException or a "no such constructor" further down:

class MyView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {
    // @JvmOverloads gives you the (Context, AttributeSet) the inflater needs
}

Also make sure the fully-qualified class name in the XML matches the real package, and that R8 isn't stripping the view (keep rules for reflectively instantiated classes).

Why the preview lies: Android Studio's layout renderer and a real device don't fail in the same places. The preview can tolerate a theme or API issue that a device won't. So "it renders in the preview" is not evidence the layout is fine — the device's stack trace is the source of truth.

Debugging routine

Line numbers, class and package names above are illustrative — use the ones from your own stack trace and layout.