NPE while inflating layout (Attempt to invoke virtual method ‘boolean java.lang.String.equals(java.lang.Object)’ on a null object reference)

Change <view to <View, because view is not about empty view. It’s for custom view defined through class attr, like below:

<view
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    class="com.your.package.YourCustomView" />

And you got

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference

because of LayoutInflater tries to parse class attr:

LayoutInflater source code

//...
View createViewFromTag(View parent, String name, Context context, AttributeSet attrs,
        boolean ignoreThemeAttr) {
    if (name.equals("view")) { // line 724
        name = attrs.getAttributeValue(null, "class"); // line 725
    }

    // Apply a theme wrapper, if allowed and one is specified.
    if (!ignoreThemeAttr) {
        final TypedArray ta = context.obtainStyledAttributes(attrs, ATTRS_THEME);
        final int themeResId = ta.getResourceId(0, 0);
        if (themeResId != 0) {
            context = new ContextThemeWrapper(context, themeResId);
        }
        ta.recycle();
    }

    if (name.equals(TAG_1995)) { // line 738
        // Let's party like it's 1995!
        return new BlinkLayout(context, attrs);
    }
//...
  • On line 724 it check that your tag is view and gets true
  • On line 725 it tries to get class through class attr and gets null
  • On line 738 it tries to check for blink tag and gets crash

Also, there is a curious thing, Android developers added Easter egg, you can try it:

<view
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    class="blink">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Some text" />
</view>

Leave a Comment