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

后端 未结 7 1677
北恋
北恋 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:43

    Ok it seems I fixed it myself by declaring Context on the beggining of the class.

    public class NoteProvider extends ContentProvider {
        Context context;
    

    then initializing it in onCreate()

    @Override
        public boolean onCreate() {
            mSQLiteOpenHelper = new NoteDbHelper(getContext());
            context = getContext();
    
            return true;
        }
    

    I think that made sure that I always have Context when I use context.getContentResolver().notifyChange(uri, null); or retCursor.setNotificationUri(context.getContentResolver(), uri); in insert/update/delete/query method- retCursor being returned cursor by mentioned methods.

    I have run the aplication on my phone and did not have issues yet if I will there will probably be an edit for this post.

    EDIT:

    It does not make a difference after all - explanationin answer by @Mate, thank you for that I think I get it now :]

提交回复
热议问题