Search code examples
androidandroid-layoutlayout-inflaterremoteview

Android AppWidget - Error inflating view: Failed to resolve attribute at index


I have an AppWidget that is producing this error:

W/AppWidgetHostView: updateAppWidget couldn't find any view, using error view android.view.InflateException: Binary XML file line #2: Failed to resolve attribute at index 1: TypedValue{t=0x2/d=0x7f01009a a=-1} Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 1: TypedValue{t=0x2/d=0x7f01009a a=-1}

I can tell the error is being caused by inflation of a layout, and I can pin it to the creation of a new RemoteViews in my RemoteViewsService.RemoteViewsFactory subclass' getViewAt() method:

@Override
public RemoteViews getViewAt(int position) {
    […]
    final RemoteViews views = new RemoteViews(getPackageName(),
            R.layout.appwidget_list_item);
    […]

    return views;
}

Commenting this line out removes this particular error, and yet I cannot figure out what is wrong with the layout file it is referencing.

Layout file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/layout_view"
              android:orientation="horizontal"
              android:layout_width="match_parent"
              android:layout_height="?listPreferredItemHeight">

    <TextView
        android:id="@+id/item_name"
        android:layout_width="0px"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:gravity="center_vertical"
        android:paddingEnd="?listPreferredItemPaddingRight"
        android:paddingStart="?listPreferredItemPaddingLeft"/>

</LinearLayout>

Solution

  • The error is cause by using using theme attributes (such as ?listPreferredItemHeight) in an AppWidget layout, which isn't possible. These are the "unresolved attribute(s)" the error is referring to.