为什么不打开sqlite数据库?

时间:2012-02-11 07:25:01

标签: android database sqlite

我正在按照教程,尝试创建数据库,然后将put文本添加到数据库中,但是当应用程序到达“.open()”时应用程序崩溃

public class SmsReceiver extends BroadcastReceiver 
{
CommentsDataSource datasource;


@Override
public void onReceive( Context context, Intent intent ) 
{
    Log.d("tag", "0");
    datasource = new CommentsDataSource(this);
    Log.d("tag", "1");
    datasource.open();



    Log.d("tag", "2");
    // ---get the SMS message passed in---
    Bundle bundle = intent.getExtras();
    SmsMessage[] msgs = null;
    //String str = "";
    if (bundle != null) {
        // ---retrieve the SMS message received---
        Object[] pdus = (Object[]) bundle.get("pdus");
        msgs = new SmsMessage[pdus.length];
        for (int i = 0; i < msgs.length; i++) {
            msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
            String str = msgs[i].getOriginatingAddress();
            //str += " :";
            String str2 =msgs[i].getMessageBody().toString();
            //str += "\n";
            Log.d("tag", "3");
            abortBroadcast();
            Log.d("tag", "4");
     String From = "Send: " + str + "  Message: " + str2;
            Log.d("tag", "5");

TestDatabaseActivity Test = new TestDatabaseActivity();
            ArrayAdapter<Comment> adapter = (ArrayAdapter<Comment>)     Test.getListAdapter();
            Comment comment;
            Log.d("tag", "6");

            String[] comments = new String[] { From };
            Log.d("tag", "7");
            int nextInt = new Random().nextInt(3);
            Log.d("tag", "8");
            // Save the new comment to the database
            comment = datasource.createComment(comments[nextInt]);
            Log.d("tag", "9");
            adapter.add(comment);
            Log.d("tag", "10");
            datasource.close();
            break;

        }
    }
}
}

这是数据库:

public class CommentsDataSource {

// Database fields
private SQLiteDatabase database;
private MySQLiteHelper dbHelper;
private String[] allColumns = { MySQLiteHelper.COLUMN_ID,
        MySQLiteHelper.COLUMN_COMMENT };

public CommentsDataSource(Context context) {
    dbHelper = new MySQLiteHelper(context);
}

public CommentsDataSource(SmsReceiver smsReceiver) {
    // TODO Auto-generated constructor stub
}

public void open() {
    database = dbHelper.getWritableDatabase();
}

public void close() {
    dbHelper.close();
}

public Comment createComment(String comment) {
    ContentValues values = new ContentValues();
    values.put(MySQLiteHelper.COLUMN_COMMENT, comment);
    long insertId = database.insert(MySQLiteHelper.TABLE_COMMENTS, null,
            values);
    // To show how to query
    Cursor cursor = database.query(MySQLiteHelper.TABLE_COMMENTS,
            allColumns, MySQLiteHelper.COLUMN_ID + " = " + insertId,   null,
            null, null, null);
    cursor.moveToFirst();
    return cursorToComment(cursor);
}

public void deleteComment(Comment comment) {
    long id = comment.getId();
    System.out.println("Comment deleted with id: " + id);
    database.delete(MySQLiteHelper.TABLE_COMMENTS, MySQLiteHelper.COLUMN_ID
            + " = " + id, null);
}

public List<Comment> getAllComments() {
    List<Comment> comments = new ArrayList<Comment>();
    Cursor cursor = database.query(MySQLiteHelper.TABLE_COMMENTS,
            allColumns, null, null, null, null, null);
    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
        Comment comment = cursorToComment(cursor);
        comments.add(comment);
        cursor.moveToNext();
    }
    // Make sure to close the cursor
    cursor.close();
    return comments;
}

private Comment cursorToComment(Cursor cursor) {
    Comment comment = new Comment();
    comment.setId(cursor.getLong(0));
    comment.setComment(cursor.getString(1));
    return comment;
}
}   

logcat获取标记0然后标记为1,然后显示“无法启动接收者”

发生了什么,为什么不打开?

1 个答案:

答案 0 :(得分:1)

将其替换为构造函数中的上下文:

datasource = new CommentsDataSource(this);

datasource = new CommentsDataSource(context);