jaypabs
jaypabs

Reputation: 1567

App crashes when receiving of message

I have the following code that works before until I added some code to save the SMS received.

package com.example.smsTest;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;

public class SmsReceiver extends BroadcastReceiver {
    private SQLiteAdapter mySQLiteAdapter;
@Override
public void onReceive(Context context, Intent intent) {
    Message message = null;

       Bundle extras = intent.getExtras();
       if (extras == null)
       return;

       Object[] pdus = (Object[]) extras.get("pdus");
       for (int i = 0; i < pdus.length; i++) {
          SmsMessage SMessage = SmsMessage.createFromPdu((byte[]) pdus[i]);
          String sender = SMessage.getOriginatingAddress();
          String body = SMessage.getMessageBody().toString();

          message = mySQLiteAdapter.createMessage(body);

          // A custom Intent that will used as another Broadcast
         Intent in = new Intent("SmsMessage.intent.MAIN").
         putExtra("get_msg", sender+":"+body);

         // To display a Toast whenever there is an SMS.
         Toast.makeText(context,body,Toast.LENGTH_LONG).show();

         //You can place your check conditions here(on the SMS or the sender)            
         //and then send another broadcast 
         context.sendBroadcast(in);

        // This is used to abort the broadcast and can be used to silently
        // process incoming message and prevent it from further being 
        // broadcasted. Avoid this, as this is not the way to program an app.
        this.abortBroadcast();
        }
     }
 }

The code that I have added that cause this crash are the following:

private SQLiteAdapter mySQLiteAdapter;
Message message = null;
message = mySQLiteAdapter.createMessage(body);

Here's the code of createMessage function:

public Message createMessage(String message) {
    String[] columns = new String[]{KEY_ID, KEY_CONTENT};

    ContentValues values = new ContentValues();
    values.put(KEY_CONTENT, message);
    long insertId = sqLiteDatabase.insert(MYDATABASE_TABLE, null,
    values);
    Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE,
            columns, KEY_ID + " = " + insertId, null,
    null, null, null);
    cursor.moveToFirst();
    Message newMessage = cursorToMessage(cursor);
    cursor.close();
    return newMessage;
}

I don't know where the error come from.

Upvotes: 0

Views: 319

Answers (2)

Akshay Joy
Akshay Joy

Reputation: 1765

Please comment this Line of Code,

 Toast.makeText(context,body,Toast.LENGTH_LONG).show();

I think this will work, I have faced the same Issue with UI operation on Background Service, Please commnent the code, or Replace with Log.d() to Log the Information

Upvotes: 0

BobTheBuilder
BobTheBuilder

Reputation: 19284

Looks like you don't instantiate mySQLiteAdapter. You must instantiate before accessing it.

Upvotes: 2

Related Questions