我实现了一个使用ContentProvider
从两个不同的数据库表中检索数据的应用程序,有错误消息:
FATAL EXCEPTION: main
Process: pioneers.safwat.mazad, PID: 4108
java.lang.NullPointerException: Attempt to invoke interface method 'int android.database.Cursor.getCount()' on a null object reference
at pioneers.safwat.mazad.MainActivity.onLoadFinished(MainActivity.java:118)
at pioneers.safwat.mazad.MainActivity.onLoadFinished(MainActivity.java:27)
at android.support.v4.app.LoaderManagerImpl$LoaderInfo.callOnLoadFinished(LoaderManager.java:476)
at android.support.v4.app.LoaderManagerImpl$LoaderInfo.onLoadComplete(LoaderManager.java:444)
at android.support.v4.content.Loader.deliverResult(Loader.java:128)
at android.support.v4.content.CursorLoader.deliverResult(CursorLoader.java:107)
at android.support.v4.content.CursorLoader.deliverResult(CursorLoader.java:39)
at android.support.v4.content.AsyncTaskLoader.dispatchOnLoadComplete(AsyncTaskLoader.java:257)
at android.support.v4.content.AsyncTaskLoader$LoadTask.onPostExecute(AsyncTaskLoader.java:82)
at android.support.v4.content.ModernAsyncTask.finish(ModernAsyncTask.java:487)
at android.support.v4.content.ModernAsyncTask$InternalHandler.handleMessage(ModernAsyncTask.java:504)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
ContenProvider代码
public class SellerBuyerProvider extends ContentProvider {
public static final String PROVIDER_NAME = "pioneers.safwat.mazad.SellerBuyerProvider";
/** A uri to do operations on locations table. A content provider is identified by its uri */
public static final Uri CONTENT_URI = Uri.parse("content://" + PROVIDER_NAME + "/sellers" );
public static final Uri CONTENT_URI2 = Uri.parse("content://" + PROVIDER_NAME + "/buyers" );
/** Constant to identify the requested operation */
private static final int MSellers = 1;
private static final int MBuyers = 2;
private static final UriMatcher uriMatcher ;
static {
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI(PROVIDER_NAME, "sellers", MSellers);
uriMatcher.addURI(PROVIDER_NAME, "buyers", MBuyers);
}
SellersDB msellersDB2;
BuyersDB mbuyersDB2;
@Override
public boolean onCreate() {
msellersDB2 = new SellersDB(getContext());
mbuyersDB2=new BuyersDB(getContext());
return true;
}
/** A callback method which is invoked when insert operation is requested on this content provider */
@Override
public Uri insert(Uri uri, ContentValues values) {
Uri _uri=null;
switch (uriMatcher.match(uri)) {
case MSellers:
long srowID = msellersDB2.insert(values);
if (srowID > 0) {
_uri = ContentUris.withAppendedId(CONTENT_URI, srowID);
getContext().getContentResolver().notifyChange(_uri, null);
}
break;
case MBuyers:
long browID = mbuyersDB2.insert(values);
if (browID > 0) {
_uri = ContentUris.withAppendedId(CONTENT_URI2, browID);
getContext().getContentResolver().notifyChange(_uri, null);
}
break;
default:
try {
throw new SQLException("Failed to insert : " + uri);
} catch (SQLException e) {
e.printStackTrace();
}
}
return _uri;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
int count=0;
switch (uriMatcher.match(uri)) {
case MSellers:
SQLiteDatabase db = msellersDB2.getWritableDatabase();
count = db.update(MY_DATABASE_TABLE, values,
selection, selectionArgs);
getContext().getContentResolver().notifyChange(uri, null);
break;
case MBuyers:
SQLiteDatabase db2 = mbuyersDB2.getWritableDatabase();
count = db2.update(MY_DATABASE_TABLE, values,
selection, selectionArgs);
getContext().getContentResolver().notifyChange(uri, null);
break;
}
return count;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
int cnt = 0;
switch (uriMatcher.match(uri)) {
case MSellers:
cnt = msellersDB2.del();
break;
case MBuyers:
cnt = mbuyersDB2.del();
break;
}
return cnt;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
Cursor retCursor = null;
switch (uriMatcher.match(uri)) {
case MSellers:
retCursor = msellersDB2.getAllSellers();
return retCursor;
case MBuyers:
retCursor = mbuyersDB2.getAllBuyers();
return retCursor;
}
return null;
}
@Override
public String getType(Uri uri) {
return null;
}
}
MainActivity
public void onLoadFinished(Loader<Cursor> loader, Cursor arg1) {
int userCount = 0;
byte [] imagee;
String name;
String itemecode;
userCount = arg1.getCount();
// Move the current record pointer to the first row of the table
arg1.moveToFirst();
for(int i=0;i<userCount;i++){
imagee=arg1.getBlob(arg1.getColumnIndex(SellersDB.FIELD_ITEMIMAGE));
name=arg1.getString(arg1.getColumnIndex(SellersDB.FIELD_NAME));
itemecode=arg1.getString(arg1.getColumnIndex(SellersDB.FIELD_ITEMCODE));
Toast.makeText(getBaseContext(),"Added Successfully "+name, Toast.LENGTH_SHORT).show();
Album a = new Album("Seller:"+name, Double.parseDouble(itemecode), imagee);
albumList.add(a);
adapter.notifyDataSetChanged();
arg1.moveToNext();
}
}
该错误出现在该行中: userCount = arg1.getCount();