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;
}
}
BroadcastReceiverinsertcontact()
で、データベース Helperを呼び出します。
DBAdapter db = new DBAdapter(context);
db.insertContact("ehsan", "emain");
しかし、SMS を受信すると、これらのエラーが発生します。
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 でエミュレーターを確認したところ、データベース フォルダーに MyDB1 がありました。エラーの理由は何ですか?