开发者

ClasscastException when casting to ViewPager

I am trying to create an application based on the new ViewPager from the compability library. When I copy the lines from the 开发者_开发知识库sample application:

setContentView(R.layout.main);
MyAdapter mAdapter = new MyAdapter(getSupportFragmentManager(), this);
ViewPager mPager = (ViewPager) findViewById(R.id.view_pager);
mPager.setAdapter(mAdapter);

with the following main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <android.support.v4.view.ViewPager 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/view_pager"
    />
<LinearLayout>

I get a

java.lang.ClassCastException: android.support.v4.app.NoSaveStateFrameLayout

at the line where I cast to ViewPager. Does anyone have an idea why this is happening?


Actually the problem ist the old layout inflating problem (look here). For some magic reasons findViewByID doesn't return null here. Instead of returning null it returns a android.support.v4.app.NoSaveStateFrameLayout which is very funny btw.


Class cast exceptions occur when you try to cast an object to type which is not compatible with it. So please check whether ViewPager instantiation you are doing is correct or not.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜