문제

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top