Android 布局中遇到java.lang.ClassCastException排查思路

1、动态设置LayoutParams时候

动态设置LayoutParams时候,子View设置的ViewGroupLayoutParams 错误

2、自定义View中的id与xml 中的id冲突

java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to androidx.fragment.app.FragmentContainerView

案例:

<?xml version="1.0" encoding="utf-8"?>
<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/com_c14_alpha_100">

    <com.xqhy.common.base.view.title.TitleBar
        android:id="@+id/title"
        android:layout_width="@dimen/dp_0"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        app:enable_text_color="@color/com_c1_alpha_100"
        app:title_bg="@color/com_c14_alpha_100"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:title="@string/my_fans" />

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/container"
        android:layout_width="@dimen/dp_0"
        android:layout_height="@dimen/dp_0"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/title" />
</androidx.constraintlayout.widget.ConstraintLayout>

自定义ViewTitleBar,其中使用了ViewBinding加载布局

public class TitleBar extends FrameLayout {

    private TitleBarBinding mBinding;

//省略代码...
    public TitleBar(@NonNull Context context) {
        this(context, null);
    }

    public TitleBar(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        if (attrs != null) {
            TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TitleBar);
            isHideTitle = typedArray.getBoolean(R.styleable.TitleBar_hide_title, false);
            mTitleStr = typedArray.getString(R.styleable.TitleBar_title);
            mBackIcon = typedArray.getResourceId(R.styleable.TitleBar_back_icon, 0);
            mBackgroundColor = typedArray.getColor(R.styleable.TitleBar_title_bg, ContextCompat.getColor(context, R.color.com_page_background));
            isShowMore = typedArray.getBoolean(R.styleable.TitleBar_show_more, false);
            isHideLine = typedArray.getBoolean(R.styleable.TitleBar_hide_line, false);
            typedArray.recycle();
        }

        initView();
        clickListener();
    }

    private void initView() {
        mBinding = TitleBarBinding.inflate(LayoutInflater.from(getContext()), this, true);
        mContainer = mBinding.container;
//省略代码...
    }

TitleBarBinding对应的title_bar.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/com_c14_alpha_100"
    android:orientation="vertical">

    <RelativeLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="@dimen/dp_44">

        <ImageView
            android:id="@+id/back"
            android:layout_width="@dimen/dp_44"
            android:layout_height="@dimen/dp_44"
            android:layout_centerVertical="true"
            android:layout_marginStart="@dimen/dp_2"
            android:padding="@dimen/dp_10"
            android:src="@drawable/ic_direction_left" />

    </RelativeLayout>

    <View
        android:id="@+id/view_line"
        android:layout_width="match_parent"
        android:layout_height="@dimen/dp_1"
        android:background="@color/com_c1_alpha_6" />
</LinearLayout>

这里可以看出自定义布局中的xml也包含了container的id,与页面布局中的FragmentContainerView的id冲突,这个问题主要是在遍历的时候由于id相同,导致创建的View错误,从而造成ClassCastException

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容