安卓定制的ContentProvider

安卓定制的ContentProvider

本文介绍了安卓定制的ContentProvider的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写的创建与单个表user_authenticationsqlite3的数据库定制的ContentProvider。

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

虽然数据库和表已在各自的的onCreate()方法创建的,我得到一个 NullPointerException异常在被覆盖的插入方法。下面是方法:

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);


     }

在调试时,我看到桌子被插入的价值,但是当它到达的getContext()它返回null,所以错误。

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.

我很奇怪,为什么的getContext()是returing空,监守自定义ContentProvider类扩展了 android.content.ContentProvider ;

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

任何人都可以请帮我这。顺便说一句,我使用的2.3 SDK

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

谢谢,
拉贾钱德拉Rangineni

Thanks,Raja Chandra Rangineni

推荐答案

getApplicationContext()应该工作,而不是的getContext的()。

getApplicationContext() should work instead of getContext() .

这篇关于安卓定制的ContentProvider的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 19:01