juunas
juunas

Reputation: 58733

Sending SMS, NullPointerException

Ok, I've tried everything now. What I'm trying to do is just send an SMS. But I get this error when I try:

02-07 17:05:45.550: W/System.err(17373): java.lang.NullPointerException
02-07 17:05:45.550: W/System.err(17373):    at android.os.Parcel.readException(Parcel.java:1333)
02-07 17:05:45.555: W/System.err(17373):    at android.os.Parcel.readException(Parcel.java:1281)
02-07 17:05:45.555: W/System.err(17373):    at com.android.internal.telephony.ISms$Stub$Proxy.sendText(ISms.java:698)
02-07 17:05:45.555: W/System.err(17373):    at android.telephony.SmsManager.sendTextMessage(SmsManager.java:113)
02-07 17:05:45.555: W/System.err(17373):    at fi.juunas.paali.MainActivity.sendSMS(MainActivity.java:178)
02-07 17:05:45.555: W/System.err(17373):    at fi.juunas.paali.MainActivity.textSingle(MainActivity.java:104)
02-07 17:05:45.560: W/System.err(17373):    at fi.juunas.paali.MainActivity$1.onClick(MainActivity.java:80)
02-07 17:05:45.560: W/System.err(17373):    at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:167)
02-07 17:05:45.560: W/System.err(17373):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-07 17:05:45.560: W/System.err(17373):    at android.os.Looper.loop(Looper.java:137)
02-07 17:05:45.560: W/System.err(17373):    at android.app.ActivityThread.main(ActivityThread.java:4507)
02-07 17:05:45.560: W/System.err(17373):    at java.lang.reflect.Method.invokeNative(Native Method)
02-07 17:05:45.565: W/System.err(17373):    at java.lang.reflect.Method.invoke(Method.java:511)
02-07 17:05:45.565: W/System.err(17373):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
02-07 17:05:45.565: W/System.err(17373):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
02-07 17:05:45.565: W/System.err(17373):    at dalvik.system.NativeStart.main(Native Method)

I'm calling the function in a method like this:

SmsManager mgr = SmsManager.getDefault();
Log.d(TAG, "Number: " + number);
Log.d(TAG, "Message: " + msg);
mgr.sendTextMessage(number, null, msg,null,null);

number = "0401234567" msg = "T"

The thing is, this works perfectly on the emulator. Just as you would expect. But not on my Galaxy S2. I hope I'm formatting the number right. I've tried including the country code as well, but the problem is the same. Oh yeah, and the message is only one character long, so the problem can't be that it's too long. And scAddress and the PendingIntents should be allowed to be null.

I'm just running out of ideas. The only thing in my mind is that the number is formatted wrong or there is a massive bug in the S2 OS.

Any ideas?

Upvotes: 2

Views: 784

Answers (2)

juunas
juunas

Reputation: 58733

Ok, problem solved. I updated my phone to Android 4.1.2 (it previously had ICS) and now it works flawlessly. I did not modify the code. So apparently there was something wrong with the OS itself.

Upvotes: 1

Colton
Colton

Reputation: 645

https://developer.android.com/reference/android/telephony/SmsManager.html#sendTextMessage(java.lang.String,%20java.lang.String,%20java.lang.String,%20android.app.PendingIntent,%20android.app.PendingIntent)

Send a pending intent object and check why it doesn't work. The pending intent object will contain either

RESULT_ERROR_GENERIC_FAILURE
RESULT_ERROR_RADIO_OFF
RESULT_ERROR_NULL_PDU

As indicated in the API

Upvotes: 0

Related Questions