编辑 2
我现在设法通过使用这里的技巧来摆脱错误https://code.google.com/p/android/issues/detail?id=42601#c10所以这就是为什么我的最后一次编辑放在首位的原因我的问题。
但这导致了下一个错误:
java.lang.NullPointerException
at android.support.v4.app.FragmentManagerImpl.getFragment(FragmentManager.java:569)
at android.support.v4.app.FragmentStatePagerAdapter.restoreState(FragmentStatePagerAdapter.java:211)
at android.support.v4.view.ViewPager.onRestoreInstanceState(ViewPager.java:1281)
at android.view.View.dispatchRestoreInstanceState(View.java:12043)
at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2688)
at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2694)
at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2694)
at android.view.View.restoreHierarchyState(View.java:12021)
at android.support.v4.app.Fragment.restoreViewState(Fragment.java:425)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:949)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4800)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:798)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:565)
at dalvik.system.NativeStart.main(Native Method)
现在我不知道从哪里继续......
编辑 0 - 第一个问题
我得到以下异常:
java.lang.IllegalStateException: No activity
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1091)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1086)
at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1877)
at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1492)
at android.support.v4.app._HoloFragment.performActivityCreated(_HoloFragment.java:251)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4800)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:798)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:565)
at dalvik.system.NativeStart.main(Native Method)
我不知道从哪里开始。我正在使用最新的支持库(v4,rev18)。
一切正常,直到我添加Fragment
一个ViewPager
与FragmentStatePagerAdapter
.
我到处都在使用支持库,我尝试了https://code.google.com/p/android/issues/detail?id=42601中的解决方案...
但是还没有任何效果...
我不知道从哪里开始,有人有提示吗?
编辑 1:背景和代码
1)在我的 MainActivity 我使用以下内容:
第三页有viewpager ...
public void setPage(int pos, boolean addToBackStack)
{
Fragment nextFragment = null;
FragmentTransaction transaction = null;
switch (pos)
{
case 0:
if (mFragmentAllRoutines == null)
mFragmentAllRoutines = new RoutinesListFragment();
if (!mFragmentAllRoutines.isVisible())
nextFragment = mFragmentAllRoutines;
break;
case 1:
if (mFragmentRoutine == null)
mFragmentRoutine = new RoutineFragment();
if (!mFragmentRoutine.isVisible())
nextFragment = mFragmentRoutine;
break;
case 2:
if (mFragmentDay == null)
mFragmentDay = new RoutineDayFragment();
if (!mFragmentDay.isVisible())
nextFragment = mFragmentDay;
break;
default:
break;
}
if (nextFragment != null)
{
// Fragment prevFragment = getSupportFragmentManager().findFragmentById(R.id.fragment);
transaction = getSupportFragmentManager().beginTransaction();
// if (prevFragment != null)
// transaction.detach(prevFragment);
transaction.replace(R.id.fragment, nextFragment, nextFragment.getClass().getName());
// transaction.attach(nextFragment);
if (addToBackStack)
transaction.addToBackStack(null);
transaction.commit();
}
}
2)第三个片段:
这是一个带有 ViewPager 和 FragmentStatePagerAdapter 的简单 Fragment。FragmentStatePagerAdapter 获取getChildFragmentManager()
为 FragmentManager。某处似乎存在问题。