无法从FragmentActivity调用Activity



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

您可以使用此代码

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

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

您在问题中粘贴的代码没有正确的语法,所以我假设您手动将其写入此处是为了不包含敏感代码。。。无论如何

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

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

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

onSaveInstanceState()重写方法失败了。

希望能有所帮助。

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

相关内容

  • 没有找到相关文章

最新更新