Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 为多个Sqlite数据库表实现内容提供程序_Android - Fatal编程技术网

Android 为多个Sqlite数据库表实现内容提供程序

Android 为多个Sqlite数据库表实现内容提供程序,android,Android,我使用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

我使用
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)
内容提供者代码

主要活动

public void onLoadFinished(加载器,光标arg1){
int userCount=0;
字节[]图像e;
字符串名;
字符串项编码;
userCount=arg1.getCount();
//将当前记录指针移动到表的第一行
arg1.moveToFirst();

对于(int i=0;i
GoogleMap;
为nullyes,现在已解决
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;
}
}
  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();
        }
    }