Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7488937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:06:50+00:00 2026-05-29T15:06:50+00:00

I am following a tutorial, and trying to create a database, then add put

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T15:06:51+00:00Added an answer on May 29, 2026 at 3:06 pm

    replace this with context in constructor:

    datasource = new CommentsDataSource(this);
    

    to

    datasource = new CommentsDataSource(context);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a modal window for my application, but unfortunately I
Trying to create a Django app based off the tutorial but using a different
I'm trying to create a ASP.NET MVC 2 webapp using the Northwind database following
I am trying to go through the following tutorial on asp.net. When I get
I'm currently following the third tutorial listed here: here where I'm trying to compile
I'm trying to create a database in my first Django project (called meu_blog). I've
i am trying to learn Java, Hibernate and the MVC pattern. Following various tutorial
I am trying to create python bindings to a vala library using the following
I am trying to create a user interface, but when I refresh the page,
I am trying to create a website using some of the Razor functionality, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.