sendTextMessage In android phone

2019-01-15 16:53发布

This is simple code send SMS.

47: SmsManager sms = SmsManager.getDefault();
48: sms.sendTextMessage("12345678901", null, "hello!", null, null);

But I can't run this code. This code run success in android version 2.1, but my phone, Samsung galaxy II, it occur NullPointException error. Tell me about that problem. Here is AndroidManifest.xml code and error log.

<uses-permission android:name="android.permission.SEND_SMS"/>

FATAL EXCEPTION: main
java.lang.RuntimeException:Unable to start activity ComponentInfo {ric.FPSProject.FamilyPositionSearch/ric.FPSProject.SinglePos.SinglePosMap}: java.lang.NullPointerException
  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2668)
  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2684)
  at android.app.ActivityThread.access$2300(ActivityThread.java:126)
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2038)
  at android.os.Handler.dispatchMessage(Handler.java:99)
  at android.os.Looper.loop(Looper.java:123)
  at android.app.ActivityThread.main(ActivityThread.java:4632)
  at java.lang.reflect.Method.invokeNative(Native Method)
  at java.lang.reflect.Method.invoke(Method.java:521)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
  at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
  at android.os.Parcel.readException(Parcel.java:1253)
  at android.os.Parcel.readException(Parcel.java:1235)
  at com.android.internal.telephony.ISms$Stub$Proxy.sendText(ISms.java:561)
  at android.telephony.SmsManager.sendTextMessage(SmsManager.java:109)
  **at ric.FPSProject.SinglePos.SinglePosMap.sendSMS(SinglePosMap.java:48)**
  at ric.FPSProject.SinglePos.SinglePosMap.initPersonData(SinglePosMap.java:273)
  at ric.FPSProject.SinglePos.SinglePosMap.onCreate(SinglePosMap.java:134)
  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2632)
  ... 11 more

标签: android sms
4条回答
beautiful°
2楼-- · 2019-01-15 17:36

I had this problem when using special characters in the SMS Message. Changing to normal english characters worked for me.

查看更多
爷的心禁止访问
3楼-- · 2019-01-15 17:43

The Null Exception is in the SMS lenght, you may use

try {}catch(Exception ex) {}

in your code in case you get null exception and try send "divided" sms using:

ArrayList<String> parts = sms.divideMessage(message);
sms.sendMultipartTextMessage(telephone, null, parts, null, null);
查看更多
混吃等死
4楼-- · 2019-01-15 17:46

give the pending intent in your 48th line as given bellow

String sent = "android.telephony.SmsManager.STATUS_ON_ICC_SENT";
PendingIntent piSent = PendingIntent.getBroadcast(smsActivity.this, 0,new Intent(sent), 0);

sms.sendTextMessage("12345678901", null, "hello!", piSent, null);

am sure it works :)

查看更多
Deceive 欺骗
5楼-- · 2019-01-15 17:57

This Problem also Occurs when your msg length exceeds normal text msg length(i.e 160 Character) you need to divide the msg into parts and send... Use this code it may help you

public static void sendSms(Activity curActivity, String phoneNumber, String msg) {
 SmsManager smsManager = SmsManager.getDefault(); 

 ArrayList<String> msgStringArray = smsManager.divideMessage(msg);      

 smsManager.sendMultipartTextMessage(phoneNumber, null, msgStringArray, null, null);
}
查看更多
登录 后发表回答