首页 > 解决方案 > Androidx Guideline 无法转换为 ViewGroup

问题描述

我刚刚迁移到 androidx,我得到了这样的错误

androidx.constraintlayout.widget.Guideline cannot be cast to android.view.ViewGroup

在我将代码迁移到 androidx 之前它工作正常。

我已经检查了一些关于这个转换问题的解决方案,但大多数情况下他们有一个小错误,比如在按钮内转换布局。而且我已经做了一些研究并得出结论,显示此错误是因为 androidx 指南不是 ViewGroup 的子项。但我还没有找到解决这个问题的方法。

我在下面包含了我的 xml 和 java 代码

SettingAccActivity.java

protected void onCreate(@Nullable Bundle savedInstanceState) {
    setContext(this);
    updateSharedPref(PreferenceConnector.ACTIVITY, this.getClass().getSimpleName());
    super.onCreate(savedInstanceState);
    FrameLayout contentFrameLayout = findViewById(R.id.frame_content);
    LayoutInflater inflater = (LayoutInflater) this.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
    inflater.inflate(R.layout.activity_settings_acc, contentFrameLayout);
... }

注意:错误开启inflater.inflate(R.layout.activity_settings_acc, contentFrameLayout);

activity_base.xml

<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<include
    ... />

<androidx.drawerlayout.widget.DrawerLayout
    android:id="@+id/layout_drawer"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:background="@color/light_gray"
    app:layout_constraintBottom_toTopOf="@id/layout_bottom_navi"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@id/toolbar_main">

    <FrameLayout
        android:id="@+id/frame_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <ListView
        ... />

</androidx.drawerlayout.widget.DrawerLayout>

<include
    ... />

</androidx.constraintlayout.widget.ConstraintLayout>

activity_setting_acc.xml

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/light_gray">

<include
    ... />

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/layout_row_password"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/padding_tiny"
    android:background="@color/white"
    android:paddingTop="@dimen/padding_normal"
    android:paddingRight="@dimen/padding_normal"
    android:paddingBottom="@dimen/padding_normal"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@id/header_title">

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guide_img_password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.12" />

    <ImageView
        android:id="@+id/img_remember_pass"
        android:layout_width="50dp"
        android:layout_height="50dp"
        app:layout_constraintLeft_toLeftOf="@id/guide_img_password"
        app:layout_constraintRight_toRightOf="@id/guide_img_password"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_remembermypasword_new" />

    ...

</androidx.constraintlayout.widget.ConstraintLayout>

...

</androidx.constraintlayout.widget.ConstraintLayout>

任何建议/答案对我都有很大帮助。

之前谢谢。

标签: javaandroid-studioandroidxconstraintlayout-guideline

解决方案


我已经找到了解决方案。当我将布局从 调整为 时androidandroidx出现了错误的更新。我把旧ConstraintLayout的改成Guideline


推荐阅读