문제

Here's the layout that works :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mainlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <com.astuetz.viewpager.extensions.PagerSlidingTabStrip
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="48dip" >
    </com.astuetz.viewpager.extensions.PagerSlidingTabStrip>

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        tools:context=".MainActivity" >
    </android.support.v4.view.ViewPager>

</LinearLayout>

and here my Activity OnCreate method:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    pager = (ViewPager) findViewById(R.id.pager);

    adapter = new MyPagerAdapter(getSupportFragmentManager());

    pager.setAdapter(adapter);

    tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
    tabs.setViewPager(pager);
    tabs.setDividerColor(Color.BLUE);
    tabs.setBackgroundColor(Color.DKGRAY);
    tabs.setTextColor(Color.WHITE);
    tabs.setIndicatorColor(Color.GREEN);

}

If in the layout I inverse the PagerSlidingTabStrip and the ViewPager, the project will crash at startup, while in the first implementation, it will work fine.

Here's the error :

05-19 16:50:11.636: E/AndroidRuntime(305): FATAL EXCEPTION: main
05-19 16:50:11.636: E/AndroidRuntime(305): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hsware.peeem/com.hsware.peeem.MainActivity}: java.lang.ClassCastException: android.support.v4.view.ViewPager
05-19 16:50:11.636: E/AndroidRuntime(305):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
05-19 16:50:11.636: E/AndroidRuntime(305):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-19 16:50:11.636: E/AndroidRuntime(305):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-19 16:50:11.636: E/AndroidRuntime(305):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-19 16:50:11.636: E/AndroidRuntime(305):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-19 16:50:11.636: E/AndroidRuntime(305):  at android.os.Looper.loop(Looper.java:123)
05-19 16:50:11.636: E/AndroidRuntime(305):  at android.app.ActivityThread.main(ActivityThread.java:4627)
05-19 16:50:11.636: E/AndroidRuntime(305):  at java.lang.reflect.Method.invokeNative(Native Method)
05-19 16:50:11.636: E/AndroidRuntime(305):  at java.lang.reflect.Method.invoke(Method.java:521)
05-19 16:50:11.636: E/AndroidRuntime(305):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-19 16:50:11.636: E/AndroidRuntime(305):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-19 16:50:11.636: E/AndroidRuntime(305):  at dalvik.system.NativeStart.main(Native Method)
05-19 16:50:11.636: E/AndroidRuntime(305): Caused by: java.lang.ClassCastException: android.support.v4.view.ViewPager
05-19 16:50:11.636: E/AndroidRuntime(305):  at com.hsware.peeem.MainActivity.onCreate(MainActivity.java:41)
05-19 16:50:11.636: E/AndroidRuntime(305):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-19 16:50:11.636: E/AndroidRuntime(305):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)

It appears that it can't retrieve de R.id.pager, I'm not sure.

도움이 되었습니까?

해결책

I've noticed that when making small ordering changes, the compiler will not pick it up. A ClassCastException is occurring because findViewById is actually returning the wrong view given R.id.pager, or more accurately - R.id.pager is not referring to the correct view.

The way to fix this is to just clean and re-build your project.

다른 팁

You aren't showing your includes. There are two definitions for ViewPager, one in android.support.v4.view.ViewPager and one in android.view.ViewPager. I'll bet that you have included the wrong one in your code.

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