gpt4 book ai didi

android - 由于 TypedArray 中缺少属性,扩展类 AppCompatButton 时出错

转载 作者:行者123 更新时间:2023-11-29 02:42:24 31 4
gpt4 key购买 nike

我在一个 v7 AppCompatActivity 中有两个 v4 fragment 。一个 fragment 有 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 引起。这两个 fragment 都是从同一个 Activity 中膨胀出来的,并使用相同的主题。唯一的区别是一个按钮是只有一个图标的 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);

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

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

这是在同一个 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>

主题很基础:

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

最佳答案

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

关于android - 由于 TypedArray 中缺少属性,扩展类 AppCompatButton 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43176145/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com