仅在第二次导航到 Fragment 时导致 java.IllegalStateException 错误,No Activ

2023-10-04移动开发问题
0

本文介绍了仅在第二次导航到 Fragment 时导致 java.IllegalStateException 错误,No Activity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我遇到了一个非常令人费解的错误,我什至不知道如何开始解决.

I am getting a very puzzling bug that I have no idea how to even begin working through.

我有一个带有一个活动的简单应用程序,视图是用片段实现的.其中一个片段内部有一个 ViewPager;所以我决定我想使用 v4 支持库的 getChildFragmentManager 类.我还必须使用 ActionBarSherlock,这会导致问题,因为它没有随 v4 库的 v11 一起提供.

I have a simple app with one activity, the views are implemented with Fragments. One of the fragments has a ViewPager inside of it; so I decided I that I wanted to use the getChildFragmentManager class of the v4 support library. I also had to use ActionBarSherlock, which caused a problem, because it does not ship with the v11 of the v4 library.

我通过将 ABS 中的 v4 支持库替换为 v11 库来解决此问题,并且所有内容都已编译并似乎可以正常工作,包括 ViewPager.

I fixed this by replacing the v4 support library in ABS with the v11 library, and everything compiled and appeared to be working, including the ViewPager.

这是奇怪的部分:

第一次打开带有 ViewPager 的 Fragment,它可以正常工作;但是第二次导航到,应用程序崩溃,给出一个无用的堆栈跟踪.通过调试,我发现问题出在 getChildFragmentManager 返回的 FragmentManager 上;它抛出无活动错误.

The first time the fragment with the ViewPager opens, it works properly; but the SECOND time it is navigated to, the app crashes, giving a useless stack trace. From debugging, I discovered that the problem was with the FragmentManager returned by getChildFragmentManager; it throws the No Activity error.

有人知道是什么原因造成的吗?

Does anybody have any idea what could be causing this?

我将发布您认为相关的代码.

I will post code that you think is relevant.

谢谢你,大卫

推荐答案

我点击了 jeremyvillalobos answer 中的链接(其中非常有帮助)这导致我这个解决方法.

I followed the link in jeremyvillalobos answer (which was very helpful) that led me to this workaround.

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);
            }
        }
    }

    ...
}

它对我很有效,无需重新实例化片段.

It works for me well, without the need to reinstantiate the fragment.

这篇关于仅在第二次导航到 Fragment 时导致 java.IllegalStateException 错误,No Activity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

使用 cocos2d 处理对 CCSprite 的触摸的最佳实践
Best practices for handling touches to a CCSprite with cocos2d(使用 cocos2d 处理对 CCSprite 的触摸的最佳实践)...
2024-08-12 移动开发问题
3

UIActivityViewControllerCompletionHandler 怎么做?
UIActivityViewControllerCompletionHandler How to?(UIActivityViewControllerCompletionHandler 怎么做?)...
2024-08-12 移动开发问题
4

如何暂停和恢复应用于 ccSprite 的操作?
how to pause and resume action applied to the ccSprite?(如何暂停和恢复应用于 ccSprite 的操作?)...
2024-08-12 移动开发问题
4

如何在 COCOS2d Android 中使用 CClistview?
How can I use CClistview in COCOS2d Android?(如何在 COCOS2d Android 中使用 CClistview?)...
2024-08-12 移动开发问题
5

cocos2d-android:如何显示分数
cocos2d-android: how to display score(cocos2d-android:如何显示分数)...
2024-08-11 移动开发问题
7

重用 cocos2d 动作
Reuse cocos2d actions(重用 cocos2d 动作)...
2024-08-11 移动开发问题
4