android custom contentprovider

那年仲夏 提交于 2019-12-24 09:47:17

问题


I wrote custom contentprovider that creates a database in sqlite3 with single table 'user_authentication'.

While the db and table have been created on their respective onCreate() methods, I am getting an NullPointerException in the overriden insert method . Below is the method:

public Uri insert(Uri uri, ContentValues initialvalues){
         SQLiteDatabase sqlitedb = dbHelper.getWritableDatabase();
         if(sUriMatcher.match(uri)!= TABLE){
             throw new IllegalArgumentException("Unkown Uri"+uri);
         }

         ContentValues values;
         if(initialvalues != null){
             values = new ContentValues(initialvalues);
         }
         else{
             values = new ContentValues();
         }

         long rowId = sqlitedb.insert(AUTHENTICATION_TABLE_NAME, "", values);

             if(rowId>0){
                 Uri uril = ContentUris.withAppendedId(User.CONTENT_URI, rowId);
                 getContext().getContentResolver().notifyChange(User.CONTENT_URI, null);
                 sqlitedb.close();
                 return uril;
             }

             throw new SQLException("Failed to insert row into User_Authentication : " + uri);


     }

While debugging, I see that table is being inserted with the 'values' but when it reaches the getContext() it return null and so the error.

I am wondering why the getContext() is returing null, becuase the custom contentprovider class extends the android.content.ContentProvider;

Could anyone please help me with this. BTW, I am using 2.3 sdk

Thanks, Raja Chandra Rangineni


回答1:


getApplicationContext() should work instead of getContext() .



来源:https://stackoverflow.com/questions/5412259/android-custom-contentprovider

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!