AssertionError:此根视图已附加到催化剂实例管理器

时间:2016-12-02 17:36:24

标签: android react-native

我有一个原生Android应用,其中有一些片段充满了React Native视图。

当其中一个React Native片段被添加到backstack然后backstack弹回到React Native片段时,我收到以下错误:

E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.AssertionError: This root view has already been attached to a catalyst instance manager
    at com.facebook.infer.annotation.Assertions.assertCondition(Assertions.java:65)
    at com.facebook.react.ReactRootView.startReactApplication(ReactRootView.java:207)
    at com.vb.easternunion.fragments.ReactFragment.onActivityCreated(ReactFragment.java:52)
    at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:2096)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1295)
    at android.support.v4.app.BackStackRecord.popFromBackStack(BackStackRecord.java:1038)
    at android.support.v4.app.FragmentManagerImpl.popBackStackState(FragmentManager.java:1742)
    at android.support.v4.app.FragmentManagerImpl$2.run(FragmentManager.java:601)
    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1682)
    at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:541)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5417)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

任何人都知道为什么我的根视图错误已被附加或我如何重用相同的根视图?

My React Fragment看起来像这样:

public class ReactFragment extends Fragment {
    private ReactRootView mReactRootView;
    private ReactInstanceManager mReactInstanceManager;
    private MainActivity main;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

        main = (MainActivity) getActivity();

        mReactRootView = new ReactRootView(context);
        mReactInstanceManager =
                ((AppManager) getActivity().getApplication())
                        .getReactNativeHost()
                        .getReactInstanceManager();

    }

    @Override
    public ReactRootView onCreateView(LayoutInflater inflater, ViewGroup group, Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        return mReactRootView;
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        mReactRootView.startReactApplication(
                mReactInstanceManager,
                "AndroidApp",
                getLaunchOptions()
        );

    }

    protected
    @Nullable
    Bundle getLaunchOptions() {
        return AppManager.getReactBundle();
    }



    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mReactRootView.unmountReactApplication();
    }

}

1 个答案:

答案 0 :(得分:1)

可能您正在视图寻呼机中使用您的片段。

在onCreateView

中移动视图的定义
@Override
public ReactRootView onCreateView(LayoutInflater inflater, ViewGroup group, Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mReactRootView = new ReactRootView(getActivity());
    return mReactRootView;
}

并记住通过添加类似

的ReactFragment来卸载其他实例
@Override
public void onDetach() {
    super.onDetach();
    mReactRootView.unmountReactApplication();
}