Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/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 ContentProvider的数据库_Android_Sqlite_Broadcastreceiver - Fatal编程技术网

Android ContentProvider的数据库

Android ContentProvider的数据库,android,sqlite,broadcastreceiver,Android,Sqlite,Broadcastreceiver,我在sqlitebrowser中创建了一个sqlite数据库,并将其复制到资产中,并将其复制到主活动中的/data/data/ir.smspeik.sms/databases/MyDB1: try { String destPath = "/data/data/" + getPackageName() + "/databases/MyDB1"; File f = new File(destPath); if (!f.exists()) {

我在sqlitebrowser中创建了一个sqlite数据库,并将其复制到资产中,并将其复制到主活动中的
/data/data/ir.smspeik.sms/databases/MyDB1

try {
    String destPath = "/data/data/" + getPackageName()
            + "/databases/MyDB1";
    File f = new File(destPath);

    if (!f.exists()) {
        CopyDB(getBaseContext().getAssets().open("smsdb"),
                new FileOutputStream(destPath));
        Toast.makeText(getApplicationContext(), "ehsan", Toast.LENGTH_LONG).show();
    }
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
我创建一个数据库助手:

public class DBAdapter {
    public static final String KEY_ROWID = "id";
    public static final String KEY_TITLE = "SmsTitle";
    public static final String KEY_BODY = "SmsBody";
    public static final String KEY_TYPE = "SmsType";
    public static final String KEY_NO = "SmsNo";
    public static final String KEY_START = "SmsStart";
    public static final String KEY_END = "SmsEnd";

    private static final String TAG = "DBAdapter";

    private static final String DATABASE_NAME = "MyDB1";
    private static final String DATABASE_TABLE = "sms";
    private static final int DATABASE_VERSION = 2;

    private final Context context;    

    private DatabaseHelper DBHelper;
    private SQLiteDatabase db;

    public DBAdapter(Context ctx) 
    {
        this.context = ctx;
        DBHelper = new DatabaseHelper(context);
    }

    private static class DatabaseHelper extends SQLiteOpenHelper 
    {
        DatabaseHelper(Context context) 
        {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) 
        {
            try {

            db =    SQLiteDatabase.openDatabase("/data/data/ir.smspeik.sms/databases/"+
            DATABASE_NAME, null, SQLiteDatabase.OPEN_READWRITE);
        //  Toast.makeText(, broadcastIntent.getStringExtra("sms"), Toast.LENGTH_SHORT).show();
            //  db.execSQL(DATABASE_CREATE);    
            //  db.openDatabase(path, factory, flags)
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
        {
            Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                    + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS contacts");
            onCreate(db);
        }
    }    

    //---opens the database---
    public DBAdapter open() throws SQLException 
    {
        db = DBHelper.getWritableDatabase();
        return this;
    }

    //---closes the database---    
    public void close() 
    {
        DBHelper.close();
    }

    //---insert a contact into the database---
    public long insertContact(String name, String email) 
    {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_TITLE,"ehsan");
        initialValues.put(KEY_BODY,"ehsan");
        initialValues.put(KEY_TYPE,"1");
        initialValues.put(KEY_NO,"4534534");
        initialValues.put(KEY_START,"1");
        initialValues.put(KEY_END,"4");

        return db.insert(DATABASE_TABLE, null, initialValues);
    }

    //---deletes a particular contact---
    public boolean deleteContact(long rowId) 
    {
        return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
    }

    //---retrieves all the contacts---
    public Cursor getAllContacts() 
    {
        return db.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TITLE,
                KEY_BODY}, null, null, null, null, null);
    }

    //---retrieves a particular contact---
    public Cursor getContact(long rowId) throws SQLException 
    {
        Cursor mCursor =
                db.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
                KEY_TITLE, KEY_BODY}, KEY_ROWID + "=" + rowId, null,
                null, null, null, null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
    }

    //---updates a contact---
    public boolean updateContact(long rowId, String name, String email) 
    {
        ContentValues args = new ContentValues();
        args.put(KEY_TITLE, name);
        args.put(KEY_BODY, email);
        return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
    }
}
在BroadcastReceiver中,我调用数据库帮助程序的
insertcontact()

DBAdapter db = new DBAdapter(context);
db.insertContact("ehsan", "emain");
但当我收到短信时,这些错误会引发:

09-12 12:10:34.561: E/AndroidRuntime(26173): FATAL EXCEPTION: main
09-12 12:10:34.561: E/AndroidRuntime(26173): java.lang.RuntimeException: Unable to start receiver ir.smspeik.sms.ReceiveSms: java.lang.NullPointerException
09-12 12:10:34.561: E/AndroidRuntime(26173):    at android.app.ActivityThread.handleReceiver(ActivityThread.java:2236)
09-12 12:10:34.561: E/AndroidRuntime(26173):    at android.app.ActivityThread.access$1500(ActivityThread.java:130)
09-12 12:10:34.561: E/AndroidRuntime(26173):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1271)
09-12 12:10:34.561: E/AndroidRuntime(26173):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-12 12:10:34.561: E/AndroidRuntime(26173):    at android.os.Looper.loop(Looper.java:137)
09-12 12:10:34.561: E/AndroidRuntime(26173):    at android.app.ActivityThread.main(ActivityThread.java:4745)
09-12 12:10:34.561: E/AndroidRuntime(26173):    at java.lang.reflect.Method.invokeNative(Native Method)
09-12 12:10:34.561: E/AndroidRuntime(26173):    at java.lang.reflect.Method.invoke(Method.java:511)
09-12 12:10:34.561: E/AndroidRuntime(26173):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-12 12:10:34.561: E/AndroidRuntime(26173):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-12 12:10:34.561: E/AndroidRuntime(26173):    at dalvik.system.NativeStart.main(Native Method)
09-12 12:10:34.561: E/AndroidRuntime(26173): Caused by: java.lang.NullPointerException
09-12 12:10:34.561: E/AndroidRuntime(26173):    at ir.smspeik.sms.DBAdapter.insertContact(DBAdapter.java:91)
09-12 12:10:34.561: E/AndroidRuntime(26173):    at ir.smspeik.sms.ReceiveSms.onReceive(ReceiveSms.java:30)
09-12 12:10:34.561: E/AndroidRuntime(26173):    at android.app.ActivityThread.handleReceiver(ActivityThread.java:2229)
09-12 12:10:34.561: E/AndroidRuntime(26173):    ... 10 more

我通过DDMS检查emulator,并将MyDB1放在数据库文件夹中。什么是错误原因

调用堆栈中有一个空指针

由以下原因引起:java.lang.NullPointerException 09-12 12:10:34.561:E/AndroidRuntime(26173):在ir.smspeik.sms.DBAdapter.insertContact(DBAdapter.java:91)


我在粘贴的代码中看不到行号,但必须是db is null

请遵循以下步骤:Smaple project在这里您错过了db.open()命令。别忘了在插入后关闭数据库连接。谢谢Yaqub Ahmad。很好,看看调用堆栈——空指针可能是因为数据库没有打开或者您没有分配它