0

我有一个包含 3 个片段的主要活动。

    public void getAddFragment() {  
    FragmentTransaction transaction = fragMgr.beginTransaction();
    //toggle active fragments
    hFragActive = false;
    bFragActive = false;
    aFragActive = true;

    aFragment.setRetainInstance(true);

    transaction.remove(hFragment);
    transaction.remove(bFragment);

    transaction.add(R.id.container, aFragment, TAG_TOGGLABLE_FRAG);

    transaction.commit();       
}

我能够从一个跳转到另一个并且字段值被保留,因为我正在添加/删除......我的问题是“AddFragment”上的日期字段我可以首先启动并翻转所有选项菜单(3个片段)如果我不改变任何东西,它就可以正常工作。如果我更改任何文本等仍然可以。一旦我打开日期选择器对话框(子片段)并更新我的 AddFragment 我就没事了,直到我离开 AddFragment 并回来。它与此崩溃:

日志猫:

    01-14 16:20:42.068: D/ActivityLifecycle(32483): onCreate, savedInstanceState is null
01-14 16:20:42.099: D/FragmentLifecycle(32483): onAttach attach to com.example.optionfragmenttest.Main
01-14 16:20:42.099: D/FragmentLifecycle(32483): onCreate savedInstanceState is null
01-14 16:20:42.099: D/FragmentLifecycle(32483): onCreateView savedInstanceState is null
01-14 16:20:42.099: D/FragmentLifecycle(32483): savedInstanceState is NULL
01-14 16:20:42.099: D/FragmentLifecycle(32483): onActivityCreated savedInstanceState is null
01-14 16:20:42.099: D/FragmentLifecycle(32483): onStart
01-14 16:20:42.107: D/ActivityLifecycle(32483): onStart
01-14 16:20:42.107: D/ActivityLifecycle(32483): onResume
01-14 16:20:42.107: D/FragmentLifecycle(32483): onResume
01-14 16:20:42.115: D/FragmentLifecycle(32483): onPause
01-14 16:20:42.115: D/ActivityLifecycle(32483): onPause
01-14 16:20:42.139: D/libEGL(32483): loaded /vendor/lib/egl/libEGL_POWERVR_SGX540_120.so
01-14 16:20:42.139: D/libEGL(32483): loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so
01-14 16:20:42.154: D/libEGL(32483): loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so
01-14 16:20:42.224: D/OpenGLRenderer(32483): Enabling debug mode 0
01-14 16:20:42.232: D/FragmentLifecycle(32483): onSaveInstanceState
01-14 16:20:42.232: D/ActivityLifecycle(32483): onSaveInstanceState
01-14 16:20:42.232: D/FragmentLifecycle(32483): onStop
01-14 16:20:42.232: D/ActivityLifecycle(32483): onStop
01-14 16:20:42.240: D/onCreateOptionsMenu(32483): onCreateOptionsMenu, menu is not null
01-14 16:20:42.435: D/FragmentLifecycle(32483): onStart
01-14 16:20:42.435: D/ActivityLifecycle(32483): onStart
01-14 16:20:42.521: D/ActivityLifecycle(32483): onResume
01-14 16:20:42.521: D/FragmentLifecycle(32483): onResume
01-14 16:20:49.428: D/FragmentLifecycle(32483): onPause
01-14 16:20:49.428: D/FragmentLifecycle(32483): onStop
01-14 16:20:49.428: D/FragmentLifecycle(32483): onDestroyView
01-14 16:20:49.428: D/FragmentLifecycle(32483): onDestroy
01-14 16:20:49.428: D/FragmentLifecycle(32483): onDetach
01-14 16:20:49.428: D/AddFragment(32483): onAttach attach to com.example.optionfragmenttest.Main
01-14 16:20:49.428: D/AddFragment(32483): onCreate savedInstanceState is null
01-14 16:20:49.428: D/AddFragment(32483): onCreateView savedInstanceState is null
01-14 16:20:49.467: D/dalvikvm(32483): GC_FOR_ALLOC freed 135K, 2% free 10969K/11143K, paused 18ms, total 19ms
01-14 16:20:49.490: D/AddFragment(32483): savedInstanceState is NULL
01-14 16:20:49.514: D/AddFragment(32483): onActivityCreated savedInstanceState is null
01-14 16:20:49.537: D/AddFragment(32483): Calendar instance
01-14 16:20:49.537: D/AddFragment(32483): post-OnClicListener
01-14 16:20:49.537: D/AddFragment(32483): onStart
01-14 16:20:49.537: D/AddFragment(32483): onResume
01-14 16:20:51.146: D/AddFragment(32483): datepicker touched
01-14 16:20:51.217: D/dalvikvm(32483): GC_CONCURRENT freed 100K, 2% free 11284K/11463K, paused 14ms+13ms, total 44ms
01-14 16:20:53.779: D/DateDialogFragment(32483): OnDateSet
01-14 16:20:53.810: D/DateDialogFragment(32483): OnDateSet
01-14 16:20:55.646: D/AddFragment(32483): onPause
01-14 16:20:55.646: D/AddFragment(32483): onDestroyView
01-14 16:20:55.654: D/AddFragment(32483): onDestroy
01-14 16:20:55.654: D/AddFragment(32483): onDetach
01-14 16:20:55.654: D/FragmentLifecycle(32483): onAttach attach to com.example.optionfragmenttest.Main
01-14 16:20:55.654: D/FragmentLifecycle(32483): onCreate savedInstanceState is null
01-14 16:20:55.654: D/FragmentLifecycle(32483): onCreateView savedInstanceState is null
01-14 16:20:55.662: D/FragmentLifecycle(32483): savedInstanceState is NULL
01-14 16:20:55.662: D/FragmentLifecycle(32483): onActivityCreated savedInstanceState is null
01-14 16:20:55.662: D/FragmentLifecycle(32483): onStart
01-14 16:20:55.662: D/FragmentLifecycle(32483): onResume
01-14 16:20:56.693: D/FragmentLifecycle(32483): onPause
01-14 16:20:56.693: D/FragmentLifecycle(32483): onStop
01-14 16:20:56.693: D/FragmentLifecycle(32483): onDestroyView
01-14 16:20:56.693: D/FragmentLifecycle(32483): onDestroy
01-14 16:20:56.693: D/FragmentLifecycle(32483): onDetach
01-14 16:20:56.693: D/AddFragment(32483): onAttach attach to com.example.optionfragmenttest.Main
01-14 16:20:56.693: D/AddFragment(32483): onCreate savedInstanceState is null
01-14 16:20:56.693: D/AddFragment(32483): onCreateView savedInstanceState is null
01-14 16:20:56.732: D/AddFragment(32483): savedInstanceState is NULL
01-14 16:20:56.873: D/AddFragment(32483): onActivityCreated savedInstanceState is null
01-14 16:20:56.881: D/AddFragment(32483): Calendar instance
01-14 16:20:56.889: D/AddFragment(32483): post-OnClicListener
01-14 16:20:56.889: D/AndroidRuntime(32483): Shutting down VM
01-14 16:20:56.889: W/dalvikvm(32483): threadid=1: thread exiting with uncaught exception (group=0x40f36300)
01-14 16:20:56.889: E/AndroidRuntime(32483): FATAL EXCEPTION: main
01-14 16:20:56.889: E/AndroidRuntime(32483): java.lang.IllegalStateException: No activity
01-14 16:20:56.889: E/AndroidRuntime(32483):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1075)
01-14 16:20:56.889: E/AndroidRuntime(32483):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1070)
01-14 16:20:56.889: E/AndroidRuntime(32483):    at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1861)
01-14 16:20:56.889: E/AndroidRuntime(32483):    at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1474)
01-14 16:20:56.889: E/AndroidRuntime(32483):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:931)
01-14 16:20:56.889: E/AndroidRuntime(32483):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
01-14 16:20:56.889: E/AndroidRuntime(32483):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
01-14 16:20:56.889: E/AndroidRuntime(32483):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
01-14 16:20:56.889: E/AndroidRuntime(32483):    at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
01-14 16:20:56.889: E/AndroidRuntime(32483):    at android.os.Handler.handleCallback(Handler.java:615)
01-14 16:20:56.889: E/AndroidRuntime(32483):    at android.os.Handler.dispatchMessage(Handler.java:92)
01-14 16:20:56.889: E/AndroidRuntime(32483):    at android.os.Looper.loop(Looper.java:137)
01-14 16:20:56.889: E/AndroidRuntime(32483):    at android.app.ActivityThread.main(ActivityThread.java:4745)
01-14 16:20:56.889: E/AndroidRuntime(32483):    at java.lang.reflect.Method.invokeNative(Native Method)
01-14 16:20:56.889: E/AndroidRuntime(32483):    at java.lang.reflect.Method.invoke(Method.java:511)
01-14 16:20:56.889: E/AndroidRuntime(32483):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-14 16:20:56.889: E/AndroidRuntime(32483):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-14 16:20:56.889: E/AndroidRuntime(32483):    at dalvik.system.NativeStart.main(Native Method)
01-14 16:20:58.435: I/Process(32483): Sending signal. PID: 32483 SIG: 9
4

1 回答 1

0

对于任何感兴趣的人,这是我遇到此异常时使用的解决方法:

public class CustomFragment extends Fragment {
    private static final Field sChildFragmentManagerField;
static {
    Field f = null;
    try {
        f = Fragment.class.getDeclaredField("mChildFragmentManager");
        f.setAccessible(true);
    } catch (NoSuchFieldException e) {
        Log.e(LOGTAG, "Error getting mChildFragmentManager field", e);
    }
    sChildFragmentManagerField = f;
}

@Override
public void onDetach() {
    super.onDetach();

    if (sChildFragmentManagerField != null) {
        try {
            sChildFragmentManagerField.set(this, null);
        } catch (Exception e) {
            Log.e(LOGTAG, "Error setting mChildFragmentManager field", e);
        }
    }
}

...

}

于 2013-10-19T21:34:43.830 回答