【问题标题】:No View Found for ID when "Don't Keep Activities" Is Turned On打开“不保留活动”时未找到 ID 视图
【发布时间】:2013-07-02 01:56:32
【问题描述】:

我的应用在不活动后间歇性崩溃。所以我认为我没有正确存储东西。我打开“不要保留活动”进行故障排除,现在我的应用程序到处崩溃。

堆栈跟踪:https://gist.github.com/hanleyhansen/6d41fee54b1e129b7922 这是丢失的布局:https://gist.github.com/hanleyhansen/73ace0c99ae675023e0f

【问题讨论】:

  • “执行暂停未恢复的活动”你是直接调用onPause()吗?
  • @DiogoBento 没有。无处可去。
  • onCreate、onResume 和 onDestroy 都不行?你舒尔吗?
  • 我正在调用 onResume() 和 onDestroy()
  • 是的。你为什么这样做?你不应该那样做。 Android 系统负责调用此方法。

标签: java android eclipse android-view


【解决方案1】:

我认为您可能遇到Issue 19917 的症状。此错误存在于 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();

我发现的一些其他背景信息和解决方法有助于我解决片段问题(尤其是嵌套它们时)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多