尝试在android BroadcastReceiver中打开sqlite数据库时为什么会出错?

时间:2015-11-07 17:56:09

标签: android

我是android的初学者,并在主要活动中调用报警管理器,进入报警管理器编写此代码:

public class AlarmReciever extends BroadcastReceiver {
    private static Context myContext;
    @Override
        public void onReceive(Context context, Intent intent) {
        new HttpAsyncTask().execute("http://myHOST.ir/oflineValue.aspx");
    }
    private class HttpAsyncTask extends AsyncTask<String, Void, String> {
        @Override
            protected String doInBackground(String... urls) {
            try {
                        String DATABASE_NAME = "TEMPFOOD";
                        String TABLE_NAME = "tempData";
                        SQLiteDatabase db;
                            db=SQLiteDatabase.openOrCreateDatabase(DATABASE_NAME,myContext.MODE_PRIVATE,  null);
            }catch(Exception e)
            {
            }
        }
        @Override
            protected void onPostExecute(String result) {

            }
    }
}


但在这一行:

db=SQLiteDatabase.openOrCreateDatabase(DATABASE_NAME,myContext.MODE_PRIVATE,  null);


我得到这个错误:
enter image description here


为什么我得到那个错误?

2 个答案:

答案 0 :(得分:1)

答案在您的问题中 - second argument should be SQLiteDatabase.CursorFactory

尝试将null作为第二个参数。

答案 1 :(得分:0)

openOrCreateDatabase()的第二个参数必须是SQLiteDatabase.CursorFactory,这是可选的,默认情况下为了实例化任何Cursor是必需的。如果您不想实例化,请使用值null作为第二个参数。

db=SQLiteDatabase.openOrCreateDatabase(DATABASE_NAME,myContext.MODE_PRIVATE,  null);

如果要实例化,请尝试以下代码: -

db=SQLiteDatabase.openOrCreateDatabase(DATABASE_NAME,new SQLiteDatabase.CursorFactory() {
                     @Override
                   public Cursor newCursor(SQLiteDatabase db, SQLiteCursorDriver masterQuery, String editTable, SQLiteQuery query) {
                                //Assign the values of masterQuery,query,editTable as per your requirements
                                return new SQLiteCursor(masterQuery,editTable,query);
                            }
                            },  null);