Working on an SMS app, which crashes upon reciept of a new message. The issue seems to lie in the Broadcast Receiver for incoming SMS but I can't figure out why.
The Broadcast Receiver:
public class SMSBroadcastReceiver extends BroadcastReceiver {
public static final String SMS_BUNDLE = "pdu";
public void onReceive(Context context, Intent intent) {
Bundle intentExtras = intent.getExtras();
if (intentExtras != null) {
Object[] sms = (Object[]) intentExtras.get(SMS_BUNDLE);
String smsMessageStr = "";
for (int i = 0; i < sms.length; ++i) { //Line 24
String format = intentExtras.getString("format");
SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) sms[i], format);
String smsBody = smsMessage.getMessageBody().toString();
String address = smsMessage.getOriginatingAddress();
smsMessageStr += "SMS From: " + address + "\n";
smsMessageStr += smsBody + "\n";
}
Toast.makeText(context, "Message Received!", Toast.LENGTH_SHORT).show();
if (MainActivity.active) {
MainActivity inst = MainActivity.instance();
inst.updateInbox(smsMessageStr);
} else {
Intent i = new Intent(context, MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
}
}
I labeled line 24 where the exception gets called.
The Error:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.haltaar.texting, PID: 15909
java.lang.RuntimeException: Unable to start receiver com.example.haltaar.texting.SMSBroadcastReceiver: java.lang.NullPointerException: Attempt to get length of null array
at android.app.ActivityThread.handleReceiver(ActivityThread.java:3194)
at android.app.ActivityThread.-wrap17(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1672)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.NullPointerException: Attempt to get length of null array
at com.example.haltaar.texting.SMSBroadcastReceiver.onReceive(SMSBroadcastReceiver.java:24)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:3187)
at android.app.ActivityThread.-wrap17(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1672)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Can anyone help shed some light on this? AFAIK all permissions are being called correctly, and the check to see if intentExtras is null should stop the sms object from being null when checked? Or am I missing something?