0

我面临一个小问题,我想从 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
4

4 回答 4

2

你可以使用这个代码

OnClick()
{
  Intent intent = new Intent(getActivity(),nextClass.class);
  startActivity(intent);
}
于 2013-08-04T21:24:18.920 回答
1

设置适配器后尝试 pager.setSaveEnabled(false)。

于 2013-01-30T21:34:48.277 回答
0

您在问题中粘贴的代码没有正确的语法,所以我假设您在这里手动编写它是为了不包含敏感代码?... 反正...

您的日志显示您的错误不是在开始一个新的活动,而是在暂停/保存您当前的活动。

您可以从您的错误中看到:

at com.directionalviewpager.DirectionalViewPager.onSaveInstanceState(DirectionalViewPager.java:454)

您的 onSaveInstanceState() 覆盖方法是失败的。

希望有帮助。

于 2012-09-26T14:50:01.620 回答
-1

pager.setSaveEnabled(false)设置适配器后尝试。这个对我有用。

于 2014-06-30T00:30:26.843 回答