开发者

android custom contentprovider

开发者 https://www.devze.com 2023-02-19 21:51 出处:网络
I wrote custom contentprovider that creates a database in sqlite3 with single table \'user_authentication\'.

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 ne开发者_StackOverflow中文版w 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


getApplicationContext() should work instead of getContext() .

0

精彩评论

暂无评论...
验证码 换一张
取 消