0

我一直试图让 2 个片段与 ViewPager 一起工作,但没有成功。这是包含 ViewPager 的主要 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;
        }


    }
}

这是日志猫:

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

下载_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
        }
}
4

2 回答 2

3

Download_ImageQuality必须是一个Fragment,而不是一个FragmentActivity

于 2012-08-24T16:05:11.253 回答
1

我们可以从文档中读取以下内容:

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

正如你所看到的,它谈论的是 Fragment 而不是 FragmentActivity,它们之间的主要区别在于 Fragment 是在 FragmentActivity 内部的。你可以把它们想象成:

片段活动 = 活动

片段=视图

注意:并非完全如此,这只是一个近似值,以使其更清晰。

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

于 2012-08-24T17:12:40.503 回答