2

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?

Franziskus Karsunke
  • 4,948
  • 3
  • 40
  • 54
  • What are the class hierarchies for android.support.v4.view.ViewPager and android.support.v4.app.NoSaveStateFrameLayout? – Maciej Pigulski Sep 06 '11 at 10:02
  • Its in the compability library you can build the documentation yourself (http://developer.android.com/sdk/compatibility-library.html#Docs) The class hierarchy of the viewpager is java.lang.Object> ViewGroup > android.support.v4.view.ViewPager and the class hierarchy of NoSaveStateFrameLayout is java.lang.Object > FrameLayout > android.support.v4.app.NoSaveStateFrameLayout – Franziskus Karsunke Sep 06 '11 at 13:32

2 Answers2

3

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.

Community
  • 1
  • 1
Franziskus Karsunke
  • 4,948
  • 3
  • 40
  • 54
-2

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.

Taryn
  • 242,637
  • 56
  • 362
  • 405
Rocker
  • 669
  • 5
  • 15
  • Your error seemed to be more related to JAVA than android so I posted the answer in intention to help you out but you seems to be more intelligent and you could solve your problem on your own so be happy with your android knowledge. – Rocker Sep 06 '11 at 16:18
  • You can do some very crafty casting in Android – CQM Sep 08 '11 at 00:53