android.widget.FrameLayout 无法转换为 com.example.crowderia.chat.view.SnapTabView?

android.widget.FrameLayout cannot be cast to com.example.crowderia.chat.view.SnapTabView?

我有一个框架布局调用 view_snap_tabs.xml,其图像和视图如下所示

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="184dp"
    xmlns:tools="http://schemas.android.com/tools"
    tools:layout_gravity="bottom"
    tools:background="@color/light_purple">

    <ImageView
        android:id="@+id/vst_bottom_image"
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:layout_marginBottom="48dp"
        android:layout_gravity="center|bottom"
        android:src="@drawable/small_circle"/>

    <View
        android:id="@+id/vst_indicator"
        android:layout_width="48dp"
        android:layout_height="4dp"
        android:layout_gravity="bottom|center"
        android:layout_marginBottom="44dp"
        android:background="@drawable/indicator_background"/>

</FrameLayout>

我已将此布局包含在我的 activity_main.xml 中,如下所示

<include
        layout="@layout/view_snap_tabs"
        android:id="@+id/am_snap_tabs"
        android:layout_alignParentBottom="true"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"/>

我已经创建了一个方法调用 SetUpViewPager,我正在尝试将该方法添加到 main activity 中,如下所示

SnapTabView snapTabView = findViewById(R.id.am_snap_tabs);
snapTabView.setUpWithViewPager(viewPager);

但是当我 运行 应用程序时,它会从那里崩溃并给出 ClassCastExeception 我该如何解决这个问题?

已编辑======================================= =============================

public class SnapTabView extends FrameLayout implements ViewPager.OnPageChangeListener {

    private ImageView mCenterImage;
    private ImageView mStartImage;
    private ImageView mBottomImage;
    private ImageView mEndImage;
    private View mIndicator;
    private ArgbEvaluator mArgbEvaluator;
    private int mCenterColor;
    private int mSideColor;
    private int mEndViewsTranslationX;
    private int mIndicatorTranslationX;
    private int mCenterTransationY;

    public SnapTabView(@NonNull Context context) {
        this(context, null);
    }
    public SnapTabView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }
    public SnapTabView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }
    private void init() {
        LayoutInflater.from(getContext()).inflate(R.layout.view_snap_tabs, this, false);
        mCenterImage = (ImageView) findViewById(R.id.vst_center_image);
        mBottomImage = (ImageView) findViewById(R.id.vst_bottom_image);
        mEndImage = (ImageView) findViewById(R.id.vst_end_image);
        mStartImage = (ImageView) findViewById(R.id.vst_start_image);
        mIndicator = (View) findViewById(R.id.vst_indicator);
        mCenterColor = ContextCompat.getColor(getContext(), R.color.white);
        mSideColor = ContextCompat.getColor(getContext(), R.color.dark_grey);
        mArgbEvaluator = new ArgbEvaluator();
        mIndicatorTranslationX = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 80, getResources().getDisplayMetrics());
        mBottomImage.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                mEndViewsTranslationX = (int) ((mBottomImage.getX() - mStartImage.getX()) - mIndicatorTranslationX);
                mBottomImage.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                mCenterTransationY = getHeight() - mBottomImage.getBottom();
            }
        });
    }
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        if(position == 0) {
            setColor(1 - positionOffset);
            moveViews(1 - positionOffset);
            mIndicator.setTranslationX((positionOffset - 1) * mIndicatorTranslationX);
            moveAndScaleCenter(1 - positionOffset);

        } else if(position == 1) {
            setColor(positionOffset);
            moveViews(positionOffset);
            mIndicator.setTranslationX(positionOffset * mIndicatorTranslationX);
            moveAndScaleCenter(positionOffset);

        }
    }
    public void setUpWithViewPager(final ViewPager viewPager) {
        viewPager.addOnPageChangeListener(this);
        mStartImage.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if(viewPager.getCurrentItem() != 0)
                    viewPager.setCurrentItem(0);
            }
        });
        mEndImage.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if(viewPager.getCurrentItem() != 2)
                    viewPager.setCurrentItem(2);
            }
        });
    }
    @Override
    public void onPageSelected(int position) {
    }
    @Override
    public void onPageScrollStateChanged(int state) {
    }
    private void setColor(float fractionFromCenter) {
        int color = (int) mArgbEvaluator.evaluate(fractionFromCenter, mCenterColor, mSideColor);
        mCenterImage.setColorFilter(color);
        mStartImage.setColorFilter(color);
        mEndImage.setColorFilter(color);
    }

    private void moveViews(float fractionFromCenter) {
        mStartImage.setTranslationX(fractionFromCenter * mEndViewsTranslationX);
        mEndImage.setTranslationX(-fractionFromCenter * mEndViewsTranslationX);
        mIndicator.setAlpha(fractionFromCenter);
        mIndicator.setScaleX(fractionFromCenter);
    }

    private void moveAndScaleCenter(float fractionFromCenter) {
        float scale = .7f + ((1 - fractionFromCenter) * .3f);
        mCenterImage.setScaleX(scale);
        mCenterImage.setScaleY(scale);
        int translation = (int) (fractionFromCenter * mCenterTransationY);
        mCenterImage.setTranslationY(translation);
        mBottomImage.setTranslationY(translation);
        mBottomImage.setAlpha(1 - fractionFromCenter);
    }
}

view_snap_tabs.xml 文件如下

<?xml version="1.0" encoding="utf-8"?>
<com.example.crowderia.chat.view.SnapTabView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="184dp"
    tools:layout_gravity="bottom"
    tools:background="@color/light_purple"
    android:id="@+id/snaps">

    <ImageView
        android:id="@+id/vst_center_image"
        android:layout_width="88dp"
        android:layout_height="88dp"
        android:layout_gravity="center|bottom"
        android:src="@drawable/large_circle"
        android:layout_marginBottom="96dp"/>

    <ImageView
        android:id="@+id/vst_start_image"
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:layout_marginBottom="56dp"
        android:layout_marginStart="24dp"
        android:layout_gravity="start|bottom"
        android:src="@drawable/ic_chat_bubble_24dp"/>

    <ImageView
        android:id="@+id/vst_end_image"
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:layout_marginBottom="56dp"
        android:layout_marginEnd="24dp"
        android:layout_gravity="end|bottom"
        android:src="@drawable/ic_group_work_24dp"/>

    <ImageView
        android:id="@+id/vst_bottom_image"
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:layout_marginBottom="48dp"
        android:layout_gravity="center|bottom"
        android:src="@drawable/small_circle"/>

    <View
        android:id="@+id/vst_indicator"
        android:layout_width="48dp"
        android:layout_height="4dp"
        android:layout_gravity="bottom|center"
        android:layout_marginBottom="44dp"
        android:background="@drawable/indicator_background"/>

</com.example.crowderia.chat.view.SnapTabView>

这是因为您试图将 FrameLayoutview 分配给 SnapTabView,修改您的 xml 如下

view_snap_tabs.xml

<?xml version="1.0" encoding="utf-8"?>
<com.example.crowderia.chat.view.SnapTabView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="184dp"
    xmlns:tools="http://schemas.android.com/tools">

 <FrameLayout 
    android:layout_width="match_parent"
    android:layout_height="184dp"
    tools:layout_gravity="bottom"
    tools:background="@color/light_purple">

    <ImageView
        android:id="@+id/vst_bottom_image"
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:layout_marginBottom="48dp"
        android:layout_gravity="center|bottom"
        android:src="@drawable/small_circle"/>

    <View
        android:id="@+id/vst_indicator"
        android:layout_width="48dp"
        android:layout_height="4dp"
        android:layout_gravity="bottom|center"
        android:layout_marginBottom="44dp"
        android:background="@drawable/indicator_background"/>

  </FrameLayout>

</com.example.crowderia.chat.view.SnapTabView>

这样做:

<?xml version="1.0" encoding="utf-8"?>
<com.example.crowderia.chat.view.SnapTabView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="184dp"
android:id="@+id/snaps"
xmlns:tools="http://schemas.android.com/tools">

<FrameLayout 
android:layout_width="match_parent"
android:layout_height="184dp"
tools:layout_gravity="bottom"
tools:background="@color/light_purple">

<ImageView
    android:id="@+id/vst_bottom_image"
    android:layout_width="32dp"
    android:layout_height="32dp"
    android:layout_marginBottom="48dp"
    android:layout_gravity="center|bottom"
    android:src="@drawable/small_circle"/>

<View
    android:id="@+id/vst_indicator"
    android:layout_width="48dp"
    android:layout_height="4dp"
    android:layout_gravity="bottom|center"
    android:layout_marginBottom="44dp"
    android:background="@drawable/indicator_background"/>

  </FrameLayout>

</com.example.crowderia.chat.view.SnapTabView>

在 activity 中执行此操作:

View view=findViewById(R.id.am_snap_tabs);
SnapTabView snap=(SnapTabView)view.findViewById(R.id.snaps);
snap.setUpWithViewPager(viewPager);

//am_snap_tabs是问题include中的id