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

时间:2017-11-17 04:06:27

标签: android classcastexception android-studio-3.0

我有一个框架布局调用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,我试图在主要活动中添加该方法,如下所示

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

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

EDITED =========================================== =========================

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>

2 个答案:

答案 0 :(得分:0)

这是因为您尝试将viewFrameLayout分配到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>

答案 1 :(得分:0)

这样做:

<?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>
活动中的

执行此操作:

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

// am_snap_tabs它是问题中include的id