我面临一个小问题,我想从 FragmentActivity 调用 Activity。我怎样才能做到这一点?
这是我的代码。
(已编辑)
class myClass Extends FragmentActivity
{
OnCreate()
{
DirectionalViewPager pager = (DirectionalViewPager) findViewById(R.id.pager);
pager.setOrientation(DirectionalViewPager.VERTICAL);
pager.setAdapter(new TestFragmentAdapter(getSupportFragmentManager()));
// I think problem is with this above line in setting adapter because when I remove above three line it is not showing me error..
}
OnClick()
{
Intent intent = new Intent(myClass.this,nextClass.class);
startActivity(intent);
}
}
class TestFragmentAdapter extends FragmentPagerAdapter {
final String[] CONTENT = new String[] { "One", "Two", "Three",
"Four","Five","Six","Seven","Eight","Nine" };
public TestFragmentAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return TestFragment.newInstance(CONTENT[position]);
}
@Override
public int getCount() {
return CONTENT.length;
}
}
错误:
08-25 17:37:04.230: W/dalvikvm(32535): threadid=1: thread exiting with uncaught exception (group=0x400cf760)
08-25 17:37:04.260: E/AndroidRuntime(32535): FATAL EXCEPTION: main
08-25 17:37:04.260: E/AndroidRuntime(32535): java.lang.RuntimeException: Unable to pause activity {com.chirag.teahouse/com.chirag.teahouse.TeaHouseActivity}: java.lang.NullPointerException
08-25 17:37:04.260: E/AndroidRuntime(32535): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2490)
08-25 17:37:04.260: E/AndroidRuntime(32535): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2447)
08-25 17:37:04.260: E/AndroidRuntime(32535): at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:2425)
08-25 17:37:04.260: E/AndroidRuntime(32535): at android.app.ActivityThread.access$1700(ActivityThread.java:122)
08-25 17:37:04.260: E/AndroidRuntime(32535): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1012)
08-25 17:37:04.260: E/AndroidRuntime(32535): at android.os.Handler.dispatchMessage(Handler.java:99)
08-25 17:37:04.260: E/AndroidRuntime(32535): at android.os.Looper.loop(Looper.java:132)
08-25 17:37:04.260: E/AndroidRuntime(32535): at android.app.ActivityThread.main(ActivityThread.java:4028)
08-25 17:37:04.260: E/AndroidRuntime(32535): at java.lang.reflect.Method.invokeNative(Native Method)
08-25 17:37:04.260: E/AndroidRuntime(32535): at java.lang.reflect.Method.invoke(Method.java:491)
08-25 17:37:04.260: E/AndroidRuntime(32535): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
08-25 17:37:04.260: E/AndroidRuntime(32535): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
08-25 17:37:04.260: E/AndroidRuntime(32535): at dalvik.system.NativeStart.main(Native Method)
08-25 17:37:04.260: E/AndroidRuntime(32535): Caused by: java.lang.NullPointerException
08-25 17:37:04.260: E/AndroidRuntime(32535): at android.support.v4.view.ViewPager.onSaveInstanceState(ViewPager.java:507)
08-25 17:37:04.260: E/AndroidRuntime(32535): at com.directionalviewpager.DirectionalViewPager.onSaveInstanceState(DirectionalViewPager.java:454)
08-25 17:37:04.260: E/AndroidRuntime(32535): at android.view.View.dispatchSaveInstanceState(View.java:8258)
08-25 17:37:04.260: E/AndroidRuntime(32535): at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:1990)
08-25 17:37:04.260: E/AndroidRuntime(32535): at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:1996)
08-25 17:37:04.260: E/AndroidRuntime(32535): at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:1996)
08-25 17:37:04.260: E/AndroidRuntime(32535): at android.view.View.saveHierarchyState(View.java:8241)
08-25 17:37:04.260: E/AndroidRuntime(32535): at com.android.internal.policy.impl.PhoneWindow.saveHierarchyState(PhoneWindow.java:1603)
08-25 17:37:04.260: E/AndroidRuntime(32535): at android.app.Activity.onSaveInstanceState(Activity.java:1164)
08-25 17:37:04.260: E/AndroidRuntime(32535): at android.support.v4.app.FragmentActivity.onSaveInstanceState(FragmentActivity.java:470)
08-25 17:37:04.260: E/AndroidRuntime(32535): at android.app.Activity.performSaveInstanceState(Activity.java:1114)
08-25 17:37:04.260: E/AndroidRuntime(32535): at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1184)
08-25 17:37:04.260: E/AndroidRuntime(32535): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2472)
08-25 17:37:04.260: E/AndroidRuntime(32535): ... 12 more