Android error text message cannot be sent out, sms null pointer, smsManager. sendTextMessage reports NULL pointer exception

Source: Internet
Author: User

The problem encountered during the test yesterday afternoon was solved this morning. The error code is as follows:

String phone = dbHelper. getPhoneByTime (timeString); SmsManager sms = SmsManager. getDefault (); Intent sentIntent = new Intent (Const. SENT_SMS_ACTION); PendingIntent sentPI = PendingIntent. getBroadcast (this, 0, sentIntent, 0); // create the deilverIntent parameterIntent deliverIntent = new Intent (Const. DELIVERED_SMS_ACTION); PendingIntent deliverPI = PendingIntent. getBroadcast (this, 0, deliverIntent, 0); sms. sendTextMessage (phone, null, msg, sentPI, deliverPI); // send

A null pointer exception is always reported in this row. The first response is that the variable has not been initialized and has been checked several times,

The mobile phone number is read from the database, msg is generated automatically, and sentPI and deliverPI are both good,

There is indeed no NULL pointer. You can find it again ..

I realized this morning that the length of the text message was incorrect.

The length of a text message is 70 characters. I counted it. It turns out that the content to be sent is too long.

Change to the following:

if (message.length() > 70) {ArrayList
 
   msgs = sms.divideMessage(message);for (String msg : msgs) {if (msg != null) {sms.sendTextMessage(phone, null, msg, sentPI,deliverPI);}}} else {sms.sendTextMessage(phone, null, message, sentPI, deliverPI);}
 


Determine the length of the text message. If the length is too long, it can be divided into multiple messages.

I cannot understand why a null pointer is reported when the text message is too long.

Author: jason0539

Weibo: http://weibo.com/2553717707

Blog: http://blog.csdn.net/jason0539 (reprinted please explain the source)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.