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

限于喜欢 提交于 2019-12-25 12:14:49

问题


I have a frame layout call view_snap_tabs.xml that have image and view like below

<?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>

I have included this layout in my activity_main.xml like below

<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"/>

I have created a method call SetUpViewPager and I'm trying to add that method in main activity like below

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

But when I run the application It will crash from there and give a ClassCastExeception how can I fix this?

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 file like below

<?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>

回答1:


do this:

<?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>

in the activity do this:

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

//am_snap_tabs it is the id of the include in the question




回答2:


It's because you are trying to assign a view with FrameLayout to SnapTabView, modify your xml as below

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>


来源:https://stackoverflow.com/questions/47343123/android-widget-framelayout-cannot-be-cast-to-com-example-crowderia-chat-view-sna

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!