Christian
Christian

Reputation: 755

Why won't sqlite database open?

I am following a tutorial, and trying to create a database, then add put texts into the database, but the application crashes when it reaches ".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;

        }
    }
}
}

and here is the database:

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 gets to tag 0 then 1, then it says "Unable to start receiver"

What's going on, and why won't it open?

Upvotes: 0

Views: 744

Answers (1)

jeet
jeet

Reputation: 29199

replace this with context in constructor:

datasource = new CommentsDataSource(this);

to

datasource = new CommentsDataSource(context);

Upvotes: 1

Related Questions