我可以使用
ShapeableImageView
显示角落图像,但是当我检查日志时,有错误,但不知道它是什么以及如何修复它。当我将 view
更改为 ImageView 时,此错误不再显示,但我无法使用角图像功能(shapeAppearanceOverlay
)
这是错误日志:
W/ResourcesCompat: Failed to inflate ColorStateList, leaving it to the framework
java.lang.UnsupportedOperationException: Failed to resolve attribute at index 0: TypedValue{t=0x2/d=0x7f0300fc a=-1}
at android.content.res.TypedArray.getColor(TypedArray.java:556)
at androidx.core.content.res.ColorStateListInflaterCompat.inflate(ColorStateListInflaterCompat.java:160)
at androidx.core.content.res.ColorStateListInflaterCompat.createFromXmlInner(ColorStateListInflaterCompat.java:125)
at androidx.core.content.res.ColorStateListInflaterCompat.createFromXml(ColorStateListInflaterCompat.java:104)
at androidx.core.content.res.ResourcesCompat.inflateColorStateList(ResourcesCompat.java:229)
at androidx.core.content.res.ResourcesCompat.getColorStateList(ResourcesCompat.java:203)
at androidx.core.content.ContextCompat.getColorStateList(ContextCompat.java:519)
at androidx.appcompat.content.res.AppCompatResources.getColorStateList(AppCompatResources.java:48)
at d.b.a.e.v.c.a(MaterialResources.java:60)
at com.google.android.material.imageview.ShapeableImageView.<init>(ShapeableImageView.java:112)
at com.google.android.material.imageview.ShapeableImageView.<init>(ShapeableImageView.java:92)
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
at android.view.LayoutInflater.createView(LayoutInflater.java:852)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1004)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:959)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1121)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1124)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1124)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1124)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)
at android.view.LayoutInflater.inflate(LayoutInflater.java:680)
at android.view.LayoutInflater.inflate(LayoutInflater.java:532)
at f.a.a.b.d.b.d(ActivityInstallerTrackerBinding.java:164)
at f.a.a.b.d.b.c(ActivityInstallerTrackerBinding.java:158)
at mypackage.MyActivity.onCreate(MyActivity.kt:64)
at android.app.Activity.performCreate(Activity.java:8006)
at android.app.Activity.performCreate(Activity.java:7990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1329)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3584)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3775)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2246)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:233)
at android.app.ActivityThread.main(ActivityThread.java:8010)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:631)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:978)
这是我第 64 行的代码
private lateinit var binding: MyActivityBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = MyActivityBinding.inflate(layoutInflater) << Line 64
setContentView(binding.root)
initView()
}
我尝试将
app:errorTextAppearance
添加到 ShapeableImageView
,因为我看到了针对此确切错误的解决方案,但对于 TextInputLayout
,但仍然存在该错误。
编辑: 包括 ShapeableImageView xml 和样式
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/margin8"
android:background="@drawable/corner_bg"
android:gravity="center"
android:orientation="horizontal"
android:visibility="gone"
app:layout_constraintEnd_toStartOf="@+id/glRight"
app:layout_constraintStart_toEndOf="@+id/glLeft"
app:layout_constraintTop_toBottomOf="@+id/tvUpload"
tools:visibility="visible">
<com.google.android.material.imageview.ShapeableImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:shapeAppearanceOverlay="@style/roundedImageView"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:padding="1dp"
android:alpha="0.5"
android:maxHeight="100dp"
android:minHeight="100dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="@dimen/margin16"
android:layout_weight="1"
android:drawablePadding="@dimen/margin8"
android:fontFamily="@font/din_condensed_bold"
android:gravity="center"
android:text="@string/select_successful"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/ivRemove"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/ivRemove"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/margin16"
android:src="@drawable/ic_close2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
这就是风格
<style name="roundedImageView" parent="">
<item name="cornerFamily">rounded</item>
<item name="cornerSize">10dp</item>
</style>
将以下内容添加到 ShapeableImageView:
app:strokeColor="@null"
这停止了我的 logcat 输出。库中似乎存在问题。
调试以下形式的错误的一般方法:
无法膨胀 ColorStateList,将其留给框架 java.lang.UnsupportedOperationException:无法解析索引 0 处的属性:TypedValue{t=0x2/d=0xNNNNNNNN a=-1}
您的应用程序可能在某处使用自定义主题,并且父主题期望子主题提供子主题未提供的属性。
首先查看 logcat 中 TypedValue 标记后面找到的十六进制值。在原始帖子的示例中,该值为 0x7f0300fc。在 Android 应用程序的构建过程生成的 R.java 文件中查找该值。希望您会看到类似以下内容:
public static final int attributeXxx=0x7f04015a;
其中 attributeXxx 类似于 colorOnSecondaryContainer 或 shapeAppearanceSmallComponent。 您现在需要做的是找出与此关联的属性。 例如,在 colorOnSecondaryContainer 的情况下,属性可以是rippleColor;对于 shapeAppearanceSmallComponent,该属性可以是 shapeAppearance。 获得正确的关联可能很棘手,因此请尝试浏览 Material md 文件
每个需求属性都需要出现在开发人员提供的主题 xml 中,以避免出现错误消息。
因此要解决这个问题,对于每个属性