AndrewBramwell
AndrewBramwell

Reputation: 494

android.support.v4.view.ViewPager cannot be cast to com.package.CustomViewPager

Im trying to implement a CustomViewPager but its saying that the Support ViewPager cannot be cast as a custom one. Im baffled as to where the non-CustomViewPager is coming from.

(package name replaced for privacy)

Here is my CustomViewPager

package com.domain.heswresources;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;

/**
 * Created by andrewb on 30/07/2015.
 */
public class CustomViewPager extends ViewPager {

    private Boolean enabled;

    public CustomViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.enabled = true;

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (this.enabled) {
            return super.onTouchEvent(event);
        }

        return false;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        if (this.enabled) {
            return super.onInterceptTouchEvent(event);
        }

        return false;
    }

    public void setPagingEnabled(Boolean enabled) {
        this.enabled = enabled;
    }
}

and here is my Activity:

package com.domain.heswresources;

import android.app.ActionBar;
import android.content.SharedPreferences.Editor;
import android.graphics.drawable.TransitionDrawable;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.View;
import com.domain.CustomViewPager;

import com.viewpagerindicator.CirclePageIndicator;


public class TutorialActivity extends FragmentActivity {

    private static final int NUM_PAGES = 4;
    private int pP = 1;
    private CustomViewPager mPager;
    private PagerAdapter mPagerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_screen_slide);

        ActionBar ab = getActionBar();
        ab.setBackgroundDrawable(getResources().getDrawable(R.drawable.dash_action_bar));
        ab.setLogo(getResources().getDrawable(R.drawable.ic_logo));
        ab.setTitle("Getting started");

        getWindow().setBackgroundDrawableResource(R.drawable.main_background);

        mPager = (CustomViewPager) findViewById(R.id.pager);
        mPagerAdapter = new TutorialPagerAdapter(getSupportFragmentManager());
        mPager.setAdapter(mPagerAdapter);


        //Bind the title indicator to the adapter (requires the ViewPagerIndicator library by Jake Wharton)
        CirclePageIndicator pageIndicator = (CirclePageIndicator)findViewById(R.id.indicator);
        pageIndicator.setViewPager(mPager);

        pageIndicator.setOnPageChangeListener(new CustomViewPager.OnPageChangeListener() {
            boolean isReverse = false;

            @Override
            public void onPageSelected(int page) {
                if (String.valueOf(pP).equals(String.valueOf(page))) {
                    isReverse = true;
                } else {
                    isReverse = false;
                }
                switch (page) {
                    case 0:
                        getWindow().setBackgroundDrawableResource(R.drawable.tran1);
                        TransitionDrawable trans1 = (TransitionDrawable) getWindow().getDecorView().getBackground();
                        trans1.startTransition(0);
                        trans1.reverseTransition(500);
                        break;
                    case 1:
                        if (isReverse) {
                            getWindow().setBackgroundDrawableResource(R.drawable.tran2);
                            TransitionDrawable trans2 = (TransitionDrawable) getWindow().getDecorView().getBackground();
                            trans2.startTransition(0);
                            trans2.reverseTransition(500);
                        } else {
                            getWindow().setBackgroundDrawableResource(R.drawable.tran1);
                            TransitionDrawable trans2 = (TransitionDrawable) getWindow().getDecorView().getBackground();
                            trans2.startTransition(500);
                        }
                        break;
                    case 2:
                        if (isReverse) {
                            getWindow().setBackgroundDrawableResource(R.drawable.tran3);
                            TransitionDrawable trans2 = (TransitionDrawable) getWindow().getDecorView().getBackground();
                            trans2.startTransition(0);
                            trans2.reverseTransition(500);
                            Log.v("ANDY2", "page 4 to 3");
                        } else {
                            getWindow().setBackgroundDrawableResource(R.drawable.tran2);
                            TransitionDrawable trans2 = (TransitionDrawable) getWindow().getDecorView().getBackground();
                            trans2.reverseTransition(2000);
                        }
                        break;
                    case 3:
                        if (isReverse) {
                            Log.v("ANDY2", "page 5 to 4");
                        } else {
                            getWindow().setBackgroundDrawableResource(R.drawable.tran3);
                            TransitionDrawable trans2 = (TransitionDrawable) getWindow().getDecorView().getBackground();
                            trans2.reverseTransition(500);
                        }
                        break;
                }
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                pP = arg0;
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {

            }
        });
    }

    public void OnClick(View v) {
        int id = v.getId();
        if (id == R.id.button1) {
            Editor ed = MainActivity.prefs.edit();
            ed.putString("type", "Acute");
            //ed.putBoolean(MainActivity.SHOW_TUTORIAL, false);
            ed.commit();
            finish();

        }
    }

    public void OnClick2(View v) {
        int id = v.getId();
        if (id == R.id.button2) {
            Editor ed = MainActivity.prefs.edit();
            ed.putString("type", "Mental Health");
            //ed.putBoolean(MainActivity.SHOW_TUTORIAL, false);
            ed.commit();
            finish();
        }
    }

    private class TutorialPagerAdapter extends FragmentPagerAdapter {
        public TutorialPagerAdapter(android.support.v4.app.FragmentManager fm) {
            super(fm);
        }

        @Override
        public TutorialFragment getItem(int position) {

            return TutorialFragment.create(position);
        }

        @Override
        public int getCount() {
            return NUM_PAGES;
        }
    }
}

And my XML Layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

<com.domain.CustomViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<com.viewpagerindicator.TitlePageIndicator
    android:id="@+id/indicator"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true"
    />

</RelativeLayout>

Its essentially falling on this line -> mPager = (CustomViewPager) findViewById(R.id.pager);

But I don't know why as this variable is set as a CustomViewPager.

Log output (if it helps)

07-30 13:50:05.630  19208-19208/com.domain.heswresources E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.domain.heswresources, PID: 19208
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.domain.heswresources/com.domain.heswresources.TutorialActivity}: java.lang.ClassCastException: android.support.v4.view.ViewPager cannot be cast to com.domain.heswresources.CustomViewPager
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2413)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
            at android.app.ActivityThread.access$900(ActivityThread.java:175)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5602)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.ClassCastException: android.support.v4.view.ViewPager cannot be cast to com.domain.heswresources.CustomViewPager
            at com.domain.heswresources.TutorialActivity.onCreate(TutorialActivity.java:53)
            at android.app.Activity.performCreate(Activity.java:5451)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2377)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
            at android.app.ActivityThread.access$900(ActivityThread.java:175)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5602)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
            at dalvik.system.NativeStart.main(Native Method)

Any Help would be appreciated!

Upvotes: 4

Views: 3247

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006554

Quoting the fictional character, Sherlock Holmes, from Sir Arthur Conan Doyle's The Sign of the Four:

How often have I said to you that when you have eliminated the impossible, whatever remains, however improbable, must be the truth?

If you are inflating a layout, and Android returns a ViewPager, most likely that is because a layout contains a ViewPager, instead of what you are expecting (in this case, a CustomViewPager). Having multiple editions of the layout resource complicates this, as perhaps the one you are working on does not have a ViewPager, but another one does.

If you're comfortable with the command line, an fgrep of your res/ directory tree, searching for android.support.v4.view.ViewPager, would be one way to track down layout resources that might still be referring to ViewPager.

Upvotes: 1

Related Questions