Android 布局中遇到j(luò)ava.lang.ClassCastException排查思路

1、動(dòng)態(tài)設(shè)置LayoutParams時(shí)候

動(dòng)態(tài)設(shè)置LayoutParams時(shí)候,子View設(shè)置的ViewGroupLayoutParams 錯(cuò)誤

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對(duì)應(yīng)的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,與頁(yè)面布局中的FragmentContainerView的id沖突,這個(gè)問(wèn)題主要是在遍歷的時(shí)候由于id相同,導(dǎo)致創(chuàng)建的View錯(cuò)誤,從而造成ClassCastException

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容