Android: getContext().getContentResolver() sometimes gets NullPointerException

后端 未结 7 1676
北恋
北恋 2021-02-20 02:05

I want to ask why we get this annotation:

Method invocation getContext.getContentResolver() may produce NullPointerException

Why i

7条回答
  •  南方客
    南方客 (楼主)
    2021-02-20 03:00

    This usually happens when you call getActivity() or getContext() in a Fragment or DialogFragment to use one of Parent activity methods like getContentResolver() because the compiler is in doubt that these methods may return a null value.

    To solve this, instead of using getActivity() or getContext(); you can simply use requireActivity() and requireContext() respectively; these methods assure that a non-null value is returned.

    You can take a look on the answer here

提交回复
热议问题