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

后端 未结 7 1681
北恋
北恋 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 02:45

    If you look in the source of ContentProvider (just hold SHIFT and click on the classname in Android Studio) then you will find that the implementation is holding an object of type Context as mContext.

    Your solution is just the same, which means if mContext of ContentProvider is null, your reference will also be null. So there is no need for this.

    To help you out, this is just a warning of your IDE if make such a construct yourself. But in this case there will always be context, because the ContentProvider is generated by your system. To avoid the error in your IDE just write @SuppressWarnings("ConstantConditions") above your class definition like:

    ...
    @SuppressWarnings("ConstantConditions")
    public class NoteProvider extends ContentProvider {
    ...
    

提交回复
热议问题