gpt4 book ai didi

android.widget.LinearLayout$LayoutParams 无法转换为 android.support.constraint.ConstraintLayout$LayoutParams

转载 作者:行者123 更新时间:2023-11-29 22:46:44 26 4
gpt4 key购买 nike

我遇到了以下错误

java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.support.constraint.ConstraintLayout$LayoutParams

最佳答案

您的 View 可能是 LinearLayout 的 subview (而不是 ConstraintLayout 的 subview )。所以,你必须使用:

LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams()

一些背景:如果你有这个布局,例如

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

<View
android:id="@+id/child_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>

如果您尝试从 child_view 获取 LayoutParams,您将获取 LinearLayout.LayoutParams 的实例(因为 child_view LinearLayout 的子元素。

另一方面:

<ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<View
android:id="@+id/child_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</ConstraintLayout>

这为您提供了 ConstraintLayout.LayoutParams 对象。

因此,您必须确保您的 Java 代码正确反射(reflect)您的布局。

关于android.widget.LinearLayout$LayoutParams 无法转换为 android.support.constraint.ConstraintLayout$LayoutParams,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58303299/

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