我一直试图让 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
}
}