由于TypedArray

时间:2017-04-03 03:37:13

标签: android android-layout android-theme

我在一个v7 AppCompatActivity中有两个v4片段。一个fragmet有AppCompatImageButton,另一个是AppCompatButton。第一个膨胀得很好,另一个失败了一个神秘的错误

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

java.lang.UnsupportedOperationException: Failed to resolve attribute at index 5: TypedValue引起。两个片段都从相同的活动中膨胀并使用相同的主题。唯一的区别是一个按钮是AppCompatImageButton,只有一个图标,另一个AppCompatButton带有图标和文字。

XML文件行#70是定义AppCompatButton的行。

我猜问题是由于主题中缺少属性,索引为5的属性,但我如何找到索引5处的内容?我尝试添加/删除android:tint和android:background,它没有任何区别。

这是跟踪的相关部分:

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: 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) 

TopicInfoFragment.java:43的代码只是

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

这是片段失败的布局(底部的按钮):

<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>

这是在同一活动中膨胀的布局:

<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>

主题非常基础:

<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>

这是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'

1 个答案:

答案 0 :(得分:0)

好的,解决了。问题出在android:textColor="@color/colorText"上。 colorText被定义为引用<color name="colorText">?android:attr/colorPrimary</color>。一旦我明确定义它,问题就消失了。