问题
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
回答1:
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 :)
回答2:
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);
}
回答3:
I had this problem when using special characters in the SMS Message. Changing to normal english characters worked for me.
回答4:
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);
来源:https://stackoverflow.com/questions/7832864/sendtextmessage-in-android-phone