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

后端 未结 7 1693
北恋
北恋 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:54

    According to ContentProvider getContext() docs:

    Retrieves the Context this provider is running in. Only available once onCreate() has been called -- this will return null in the constructor.

    So the getContext() method does not return null in insert(), update() or delete(), because onCreate() will be called before these calls.

    So it's OK to disable that warning for that line if you use it in such case...

    //noinspection ConstantConditions
    getContext().getContentResolver().notifyChange(uri, null);
    

提交回复
热议问题