Search code examples
androidandroid-layoutandroid-theme

Error inflating class AppCompatButton due to a missing attribute in TypedArray


I have two v4 Fragments in one v7 AppCompatActivity. One fragmet has AppCompatImageButton, the other AppCompatButton. The first one inflates just fine, the other fails with a cryptic error

android.view.InflateException: Binary XML file line #70: Binary XML file line #70: Error inflating class android.support.v7.widget.AppCompatButton

caused by java.lang.UnsupportedOperationException: Failed to resolve attribute at index 5: TypedValue. Both fragments are inflated from the same activity and use the same theme. The only difference is that one button is AppCompatImageButton with just an icon, the other AppCompatButton with an icon and text.

The XML file line #70 is the line where AppCompatButton is defined.

I guess the problem is due to a missing attribute in the Theme, the one at index 5, but how do I find what's at index 5? I tried adding/removing android:tint and android:background, it made no difference.

Here is the relevant piece of the trace:

android.view.InflateException: Binary XML file line #70: Binary XML file line #70: Error inflating class android.support.v7.widget.AppCompatButton
  at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
  at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
  at co.tinode.tindroid.TopicInfoFragment.onCreateView(TopicInfoFragment.java:42)
  at android.support.v4.app.Fragment.performCreateView(Fragment.java:2192)
  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1299)

Caused by:

Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 5: TypedValue{t=0x2/d=0x1010433 a=2 r=0x7f0d0029}
  at android.content.res.TypedArray.getColorStateList(TypedArray.java:482)
  at android.widget.TextView.<init>(TextView.java:1043)
  at android.widget.Button.<init>(Button.java:109)
  at android.widget.Button.<init>(Button.java:105)
  at android.support.v7.widget.AppCompatButton.<init>(AppCompatButton.java:66)
  at android.support.v7.widget.AppCompatButton.<init>(AppCompatButton.java:62)
  at java.lang.reflect.Constructor.newInstance(Native Method) 
  at android.view.LayoutInflater.createView(LayoutInflater.java:619) 
  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:764) 
  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 
  at android.view.LayoutInflater.rInflate(LayoutInflater.java:835) 
  at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
  at android.view.LayoutInflater.rInflate(LayoutInflater.java:838) 
  at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
  at android.view.LayoutInflater.inflate(LayoutInflater.java:515) 
  at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
  at co.tinode.tindroid.TopicInfoFragment.onCreateView(TopicInfoFragment.java:43) 

The code at TopicInfoFragment.java:43 is just

return inflater.inflate(R.layout.fragment_topic_info, container, false);

Here is the layout for the Fragment that fails (button on the bottom):

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:padding="@dimen/activity_padding"
android:orientation="vertical"
tools:context="co.tinode.tindroid.TopicInfoFragment">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="4dp">

    <android.support.v7.widget.AppCompatImageView
        android:id="@+id/imageAvatar"
        android:layout_width="@dimen/avatar_size_large"
        android:layout_height="@dimen/avatar_size_large"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="8sp"
        android:layout_marginStart="8sp"
        android:clickable="true"
        android:contentDescription="@string/avatar"
        android:src="@drawable/ic_group"/>

    <TextView
        android:id="@+id/topicTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/dialog_item_padding"
        android:layout_toLeftOf="@id/imageAvatar"
        android:layout_toStartOf="@id/imageAvatar"
        android:maxLines="2"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textIsSelectable="true"/>

    <TextView
        android:id="@+id/topicSubtitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/topicTitle"
        android:layout_marginTop="@dimen/dialog_item_padding"
        android:ellipsize="end"
        android:maxLines="1"
        android:textIsSelectable="true"/>

</RelativeLayout>

<View
    android:layout_width="match_parent"
    android:layout_height="1dip"
    android:layout_marginBottom="@dimen/activity_vertical_margin"
    android:layout_marginTop="@dimen/activity_vertical_margin"
    android:background="#66000000"/>

<TextView
    android:id="@+id/membersTitle"
    style="@style/sectionTitleStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="@dimen/dialog_item_padding"
    android:text="@string/group_members"
    />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >

    <!-- THIS BUTTON FAILS TO INFLATE -->

    <android.support.v7.widget.AppCompatButton
        android:id="@+id/buttonAddMember"
        android:contentDescription="@string/add_member"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:drawableLeft="@drawable/ic_person_add"
        android:drawableStart="@drawable/ic_person_add"
        android:text="@string/add_member"
        android:drawablePadding="4sp"
        android:textColor="@color/colorText"
        android:background="?attr/selectableItemBackgroundBorderless"
        />

</LinearLayout>

<ListView
    android:id="@id/android:list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fastScrollAlwaysVisible="true"
    android:scrollbarStyle="outsideInset"
    android:verticalScrollbarPosition="right"/>

</LinearLayout>

Here is the the layout which inflates just fine in the same activity:

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/message_view_bkg"
tools:context="co.tinode.tindroid.MessagesFragment">

<android.support.v7.widget.RecyclerView
    android:id="@+id/messages_container"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_above="@+id/sendMessagePanel"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:scrollbars="vertical"
    app:stackFromEnd="true"
    app:reverseLayout="false"/>

<LinearLayout
    android:id="@+id/sendMessagePanel"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="4dp"
    android:paddingLeft="6sp"
    android:paddingStart="6sp"
    android:orientation="horizontal"
    android:background="@android:color/background_light">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/editMessage"
        android:inputType="text"
        android:hint="@string/new_message_hint"
        android:imeOptions="actionSend"
        android:layout_weight="1" />

    <!-- This button inflates just fine -->

    <android.support.v7.widget.AppCompatImageButton
        android:id="@+id/chatSendButton"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:src="@drawable/ic_send"
        android:contentDescription="@string/send_message_button_hint"
        android:tint="@android:color/holo_blue_dark"
        android:layout_weight="0.1"
        android:paddingLeft="4sp"
        android:paddingRight="8sp"
        android:background="?attr/selectableItemBackgroundBorderless" />

</LinearLayout>

</RelativeLayout>

The theme is quite basic:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="windowActionModeOverlay">true</item>
</style>

Here is the gradle:

  apply plugin: 'com.android.application'

  android {
      compileSdkVersion 25
      buildToolsVersion '25.0.0'

      defaultConfig {
          applicationId "co.tinode.tindroid"
          minSdkVersion 15
          targetSdkVersion 25
          versionCode 1
          versionName "1.0"
      }
      buildTypes {
          release {
              minifyEnabled false
              proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
          }
      }

      packagingOptions {
          exclude 'META-INF/DEPENDENCIES'
          exclude 'META-INF/NOTICE'
          exclude 'META-INF/LICENSE'
          exclude 'META-INF/LICENSE.txt'
          exclude 'META-INF/NOTICE.txt'
      }
  }

  dependencies {
      compile fileTree(include: ['*.jar'], dir: 'libs')
      compile project(path: ':tinodesdk')
      compile 'com.android.support:appcompat-v7:25.3.1'
      compile 'com.android.support:preference-v7:25.3.1'
      compile 'com.android.support:design:25.3.1'
      compile 'com.googlecode.libphonenumber:libphonenumber:8.4.0'
      compile 'com.google.firebase:firebase-messaging:9.6.1'
      compile 'com.android.support:recyclerview-v7:25.3.1'
      compile 'com.android.support:cardview-v7:25.3.1'
      testCompile 'junit:junit:4.12'
  }


  apply plugin: 'com.google.gms.google-services'

Solution

  • OK, solved. The problem was with the android:textColor="@color/colorText". colorText was defined as a reference <color name="colorText">?android:attr/colorPrimary</color>. Once I defined it explicitly, the problem went away.