ViewPager的ClassCastException

时间:2014-05-19 10:47:04

标签: android android-viewpager classcastexception

由于某种原因,我的代码试图转换错误的元素,从而导致ClassCastException。你们之前见过这个/有什么建议吗?

活动

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

栈跟踪

  

05-19 12:35:30.439:E / AndroidRuntime(25489):引起:   java.lang.ClassCastException:com.viewpagerindicator.CirclePageIndicator无法强制转换为   android.support.v4.view.ViewPager 05-19 12:35:30.439:   E / AndroidRuntime(25489):at com.mgd.theexplorer.ClueActivity.onCreate(ClueActivity.java:34)

我不明白为什么代码(ViewPager) findViewById(R.id.vp_clues);会抛出ClassCastException,认为它实际上是一个CirclePageIndicator。

有关信息: CirclePageIndicator是以下库的一部分:

1 个答案:

答案 0 :(得分:4)

试试这些:

  1. 再次清理并运行项目
  2. 构建项目
  3. 希望有所帮助