Search code examples
androidandroid-viewpagerclasscastexception

ClassCastException with ViewPager


For some reason, my code is trying to cast the wrong element and hence leads to a ClassCastException. Any of you seen this before/Have any suggestions?

Activity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_clue);

    cluesUnlocked = getIntent().getIntExtra(NUMBER_OF_CLUES_UNLOCKED, 4);
    MyAdapter mAdapter = new MyAdapter(getSupportFragmentManager());
    ViewPager mPager = (ViewPager) findViewById(R.id.vp_clues); // LINE 34 - ERROR
    mPager.setAdapter(mAdapter);

    CirclePageIndicator mIndicator = (CirclePageIndicator)findViewById(R.id.indicator);
    mIndicator.setViewPager(mPager);

    //We set this on the indicator, NOT the pager
    mIndicator.setOnPageChangeListener(new MyOnPageChangeListener());
}

activity_clue.xml

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="com.mgd.theexplorer.ClueActivity"
android:baselineAligned="false"
tools:ignore="MergeRootFrame">

   <com.viewpagerindicator.CirclePageIndicator
        android:id="@+id/indicator"
        android:padding="10dip"
        android:layout_height="25dip"
        android:layout_width="fill_parent" />

<android.support.v4.view.ViewPager
    android:id="@+id/vp_clues"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >
</android.support.v4.view.ViewPager>


</LinearLayout>

StackTrace

05-19 12:35:30.439: E/AndroidRuntime(25489): Caused by: java.lang.ClassCastException: com.viewpagerindicator.CirclePageIndicator cannot be cast to android.support.v4.view.ViewPager 05-19 12:35:30.439: E/AndroidRuntime(25489): at com.mgd.theexplorer.ClueActivity.onCreate(ClueActivity.java:34)

I don't understand why the code (ViewPager) findViewById(R.id.vp_clues); throws a ClassCastException thinking it is actually a CirclePageIndicator.

For info: The CirclePageIndicator is part of the following library:


Solution

  • Try these :

    1. Clean and run project again
    2. Build your project

    Hope it helps