gpt4 book ai didi

android - GridLayout$LayoutParams 无法转换为 android.support.constraint.ConstraintLayout$LayoutParams

转载 作者:搜寻专家 更新时间:2023-11-01 09:29:58 25 4
gpt4 key购买 nike

Android Studio 3.1、Java 1.8、Gradle 4.1。我使用 minVersion = 15

我想在文件 profile_category_active.xml 中以编程方式设置 GridConstrainLayout 的宽度.所以……

这是我的主要 XML

<android.support.constraint.ConstraintLayout
android:id="@+id/categoriesContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/nameContainer"
app:layout_constraintTop_toBottomOf="@+id/birthDateContainer">

<GridLayout
android:id="@+id/categoriesGridContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/place_holder_color"
app:layout_constraintEnd_toEndOf="@+id/categoriesContainer"
app:layout_constraintStart_toStartOf="@+id/categoriesContainer"
app:layout_constraintTop_toBottomOf="@+id/categoriesContainer" />

</android.support.constraint.ConstraintLayout>

此处profile_category_active.xml:

<android.support.constraint.ConstraintLayout     
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/profileCategoryContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/profile_category_active_bg">


<TextView
android:id="@+id/categoryNameTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginEnd="10dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="10dp"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:ellipsize="end"
android:maxLines="1"
android:text="TextView"
android:textColor="#292929"
android:textSize="15sp"/>
</android.support.constraint.ConstraintLayout>

这里是 Activity 代码:

public class ProfileActivity extends AppCompatActivity {
private static final String TAG = ProfileActivity.class.getName();

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ProfileBinding profileBinding = DataBindingUtil.setContentView(this, R.layout.profile);
GridLayout categoriesGridContainer = findViewById(R.id.categoriesGridContainer);
categoriesGridContainer.setColumnCount(columnCount);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for (int index = 0; index < 13; index++) {
View profileCategoryActive = inflater.inflate(R.layout.profile_category_active, null, false);
categoriesGridContainer.addView(profileCategoryActive);
ConstraintLayout profileCategoryContainer = profileCategoryActive.findViewById(R.id.profileCategoryContainer);
ConstraintLayout.LayoutParams profileCategoryContaineParams = (ConstraintLayout.LayoutParams) profileCategoryContainer.getLayoutParams();
profileCategoryContaineParams.width = (int) AndroidUtil.dpToPx(this, categoryItemWidth);
}
}
}

但是在运行时我得到错误:

FATAL EXCEPTION: main
Process: com.myproject.android.customer.debug, PID: 15496
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myproject.android.customer.debug/com.myproject.android.customer.ui.ProfileActivity}: java.lang.ClassCastException: android.widget.GridLayout$LayoutParams cannot be cast to android.support.constraint.ConstraintLayout$LayoutParams
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.ClassCastException: android.widget.GridLayout$LayoutParams cannot be cast to android.support.constraint.ConstraintLayout$LayoutParams
at com.myproject.android.customer.ui.ProfileActivity.init(ProfileActivity.java:76)

为什么这一行出错?

   ConstraintLayout.LayoutParams profileCategoryContaineParams = (ConstraintLayout.LayoutParams) profileCategoryContainer.getLayoutParams();

profileCategoryContainer 是一个ConstraintLayout(不是GridLayout)

最佳答案

试试这个

ConstraintLayout.LayoutParams clpcontactUs = new ConstraintLayout.LayoutParams(
ConstraintLayout.LayoutParams.WRAP_CONTENT, ConstraintLayout.LayoutParams.WRAP_CONTENT);
contactUs.setLayoutParams(clpcontactUs);

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

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