繁体   English   中英

为什么sqlite数据库不会打开?

[英]Why won't sqlite database open?

我正在遵循一个教程,并尝试创建数据库,然后将放置的文本添加到数据库中,但是当应用程序到达“ .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,然后显示“无法启动接收器”

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

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

datasource = new CommentsDataSource(this);

datasource = new CommentsDataSource(context);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM