无法从FragmentActivity调用Activity

时间:2012-08-25 10:14:42

标签: android android-intent navigation android-fragments android-fragmentactivity

我正面临一个小问题,我想从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 个答案:

答案 0 :(得分:2)

您可以使用此代码

OnClick()
{
  Intent intent = new Intent(getActivity(),nextClass.class);
  startActivity(intent);
}

答案 1 :(得分:1)

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

答案 2 :(得分:0)

你在问题​​中粘贴的代码没有正确的语法,所以我假设你在这里手动编写它以便不包含敏感代码? ......无论如何......

您的日志显示您的错误不是在暂停/保存当前活动时启动新活动。

您可以从错误中看到:

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

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

希望有所帮助。

答案 3 :(得分:-1)

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