使用带FragmentActivities的ViewPager时出现ClassCastException

时间:2012-08-24 16:03:28

标签: android android-fragments android-viewpager

我一直试图让2个片段与ViewPager一起工作但没有成功。这是包含ViewPager的Main FragmentActivity:

public class DownloadCenter extends android.support.v4.app.FragmentActivity {
    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.download_center);
        ViewPager vp = (ViewPager) findViewById(R.id.DownloadCenter_viewPager);
        vp.setAdapter(new myAdapter(getSupportFragmentManager()));



    }

    public class myAdapter extends FragmentPagerAdapter {
        public myAdapter(FragmentManager fm) {
            super(fm);
        }

        Fragment[] fragmentArray = { Fragment.instantiate(DownloadCenter.this, Download_ImageQuality.class.getName()),
                Fragment.instantiate(DownloadCenter.this, Download_Tilawat.class.getName()) };

        @Override
        public Fragment getItem(int arg0) {
            return fragmentArray[arg0];
        }

        @Override
        public int getCount() {
            return fragmentArray.length;
        }


    }
}

这是logcat:

08-24 18:42:34.238: E/AndroidRuntime(4205): FATAL EXCEPTION: main
08-24 18:42:34.238: E/AndroidRuntime(4205): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.test/test.test.DownloadCenter}: java.lang.ClassCastException: test.test.Download_ImageQuality cannot be cast to android.support.v4.app.Fragment
08-24 18:42:34.238: E/AndroidRuntime(4205):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2079)
08-24 18:42:34.238: E/AndroidRuntime(4205):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104)
08-24 18:42:34.238: E/AndroidRuntime(4205):     at android.app.ActivityThread.access$600(ActivityThread.java:132)
08-24 18:42:34.238: E/AndroidRuntime(4205):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1157)
08-24 18:42:34.238: E/AndroidRuntime(4205):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-24 18:42:34.238: E/AndroidRuntime(4205):     at android.os.Looper.loop(Looper.java:137)
08-24 18:42:34.238: E/AndroidRuntime(4205):     at android.app.ActivityThread.main(ActivityThread.java:4575)
08-24 18:42:34.238: E/AndroidRuntime(4205):     at java.lang.reflect.Method.invokeNative(Native Method)
08-24 18:42:34.238: E/AndroidRuntime(4205):     at java.lang.reflect.Method.invoke(Method.java:511)
08-24 18:42:34.238: E/AndroidRuntime(4205):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-24 18:42:34.238: E/AndroidRuntime(4205):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-24 18:42:34.238: E/AndroidRuntime(4205):     at dalvik.system.NativeStart.main(Native Method)
08-24 18:42:34.238: E/AndroidRuntime(4205): Caused by: java.lang.ClassCastException: test.test.Download_ImageQuality cannot be cast to android.support.v4.app.Fragment
08-24 18:42:34.238: E/AndroidRuntime(4205):     at android.support.v4.app.Fragment.instantiate(Fragment.java:388)
08-24 18:42:34.238: E/AndroidRuntime(4205):     at android.support.v4.app.Fragment.instantiate(Fragment.java:363)
08-24 18:42:34.238: E/AndroidRuntime(4205):     at test.test.DownloadCenter$myAdapter.<init>(DownloadCenter.java:29)
08-24 18:42:34.238: E/AndroidRuntime(4205):     at test.test.DownloadCenter.onCreate(DownloadCenter.java:18)
08-24 18:42:34.238: E/AndroidRuntime(4205):     at android.app.Activity.performCreate(Activity.java:4465)
08-24 18:42:34.238: E/AndroidRuntime(4205):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
08-24 18:42:34.238: E/AndroidRuntime(4205):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2033)
08-24 18:42:34.238: E/AndroidRuntime(4205):     ... 11 more

Download_ImageQuality标题:

public class Download_ImageQuality extends android.support.v4.app.FragmentActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.download_imagequality);
                 .... //REST OF CLASS
        }
}

2 个答案:

答案 0 :(得分:3)

Download_ImageQuality必须是Fragment,而不是FragmentActivity

答案 1 :(得分:1)

我们可以阅读documentation以下内容:

  

ViewPager最常与Fragment一起使用,这是一个   提供和管理每个页面生命周期的便捷方式。那里   是使用片段的标准适配器   ViewPager,涵盖了最常见的用例。这些是   FragmentPagerAdapter,FragmentStatePagerAdapter,FragmentPagerAdapter,   和FragmentStatePagerAdapter;这些类中的每一个都有简单的代码   展示如何使用它们构建完整的用户界面。

正如你所看到的那样,它讨论的是Fragments而不是FragmentActivities,它们之间的主要区别在于Fragment是FragmentActivity中的一个片段。您可以将它们视为:

  

FragmentActivity =活动

     

片段=查看

注意:不完全是这样,这只是一个近似值,使其更清晰。

因此,在这种情况下,最好的方法是使用FragmentPagerAdapter并使ViewPager的所有内容成为片段,并且您可以处理每个页面内的整个生命周期。