getActivity() 在 Fragment 函数中返回 null

新手上路,请多包涵

我有一个像这样的公共方法的片段(F1)

 public void asd() {
    if (getActivity() == null) {
        Log.d("yes","it is null");
    }
}

是的,当我(从活动)调用它时,它是空的……

 FragmentTransaction transaction1 = getSupportFragmentManager().beginTransaction();
F1 f1 = new F1();
transaction1.replace(R.id.upperPart, f1);
transaction1.commit();
f1.asd();

这一定是我做错了什么,但我不知道那是什么。

原文由 Lukap 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 551
2 个回答

commit 安排事务,即它不会立即发生,而是在下一次主线程准备好时安排为主线程上的工作。

我建议添加一个

onAttach(Activity activity)

方法到您的 Fragment 并在其上放置一个断点并查看相对于您对 asd() 的调用的调用时间。您会看到它在您调用 asd() 的方法退出后被调用。 onAttach 调用是 Fragment 附加到其活动的地方,从这一点开始 getActivity() 将返回非空值( onDetach() 呼叫)。

原文由 PJL 发布,翻译遵循 CC BY-SA 3.0 许可协议

编写一个通用方法,以确保您永远不会获得 null Activity。

 public class BaseFragment extends Fragment {
    private Context contextNullSafe;

     @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
         /*View creation related to this fragment is finished here. So in case if contextNullSafe is null
         * then we can populate it here.In some discussion in - https://stackoverflow.com/questions/6215239/getactivity-returns-null-in-fragment-function
         * and https://stackoverflow.com/questions/47987649/why-getcontext-in-fragment-sometimes-returns-null,
         * there are some recommendations to call getContext() or getActivity() after onCreateView() and
         * onViewCreated() is called after the onCreateView() call every time */
        if (contextNullSafe == null) getContextNullSafety();
    }

   @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        contextNullSafe = context;
    }

    /**CALL THIS IF YOU NEED CONTEXT*/
    public Context getContextNullSafety() {
                if (getContext() != null) return getContext();
                if (getActivity() != null) return getActivity();
                if (contextNullSafe != null) return contextNullSafe;
                if (getView() != null && getView().getContext() != null) return getView().getContext();
                if (requireContext() != null) return requireContext();
                if (requireActivity() != null) return requireActivity();
                if (requireView() != null && requireView().getContext() != null)
                    return requireView().getContext();

                return null;

        }

    /**CALL THIS IF YOU NEED ACTIVITY*/
    public FragmentActivity getActivityNullSafety() {
        if (getContextNullSafety() != null && getContextNullSafety() instanceof FragmentActivity) {
            /*It is observed that if context it not null then it will be
             * the related host/container activity always*/
            return (FragmentActivity) getContextNullSafety();
        }
        return null;
    }

原文由 Gk Mohammad Emon 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题