当“不保持活动”打开时,找不到ID的视图

时间:2013-07-02 01:56:33

标签: java android eclipse android-view

我的应用程序在一些不活动后间歇性地崩溃。所以我想我没有正确存放东西。我打开了“不要保持活动”进行故障排除,现在我的应用程序无处不在。

堆栈追踪:https://gist.github.com/hanleyhansen/6d41fee54b1e129b7922 这是缺少的布局:https://gist.github.com/hanleyhansen/73ace0c99ae675023e0f

1 个答案:

答案 0 :(得分:4)

我认为你正在经历Issue 19917的可能症状。这个bug存在于3.2及更高版本中,最近才修复(4.2)。但是,同样的修复还没有进入支持库。

查看comment 28以获得真正的修复:您需要编辑支持库并重新编译。编辑v4 / java / android / support / v4 / app / FragmentManager.java

Bundle saveFragmentBasicState(Fragment f) {
    Bundle result = null;

    if (mStateBundle == null) {
        mStateBundle = new Bundle();
    }
    f.onSaveInstanceState(mStateBundle);
    if (!mStateBundle.isEmpty()) {
        result = mStateBundle;
        mStateBundle = null;
    }

    if (f.mView != null) {
        saveFragmentViewState(f);
    }
    if (f.mSavedViewState != null) {
        if (result == null) {
            result = new Bundle();
        }
        result.putSparseParcelableArray(
                FragmentManagerImpl.VIEW_STATE_TAG, f.mSavedViewState);
    }
    if (!f.mUserVisibleHint) {
        // Only add this if it's not the default value
        // @@@ BUG, result may not have been created, can be null!
        if (result == null) {
            result = new Bundle();
        }
        result.putBoolean(FragmentManagerImpl.USER_VISIBLE_HINT_TAG, f.mUserVisibleHint);
    }

    return result;
}

如果您感觉不到任务,并且想等待Google修复支持库 这是另一种解决方法 Comment 8用于修复,您可以应用于所有片段

    @Override
    public void onSaveInstanceState(Bundle outState) {
        //first saving my state, so the bundle wont be empty.
        //http://code.google.com/p/android/issues/detail?id=19917
        outState.putString("WORKAROUND_FOR_BUG_19917_KEY", "WORKAROUND_FOR_BUG_19917_VALUE");
        super.onSaveInstanceState(outState);
    }

我还发现transaction.commitAllowingStateLoss();作为“修复”代替transaction.commit();

我发现其他一些背景信息和解决方法是相关的,这有助于我解决碎片问题(特别是在嵌套时)