我正在尝试使用Java代码发送短信。发送英文短信时工作正常。但是当我尝试下面给出的俄罗斯信息时失败了。它抛出NullPointerException
。
String message = "Здравствуйтe, сейчас я не могу ответить на Ваш звонок. Перезвоните через 10 минут, если Вам не ответили. Спасибо";
PendingIntent pi = PendingIntent.getActivity(this, 0,
new Intent(this, RussianSmsProblemActivity.class), 0);
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage("5556", null, message,sentPI,deliveredPI);//getting Null Pointer Exception here
代码没有问题,NullpointerException来自Android API。请一些专家来指导这个问题。
这是异常堆栈跟踪...
12-15 16:48:32.994: ERROR/AndroidRuntime(347): FATAL EXCEPTION: main
12-15 16:48:32.994: ERROR/AndroidRuntime(347): java.lang.NullPointerException
12-15 16:48:32.994: ERROR/AndroidRuntime(347): at android.os.Parcel.readException(Parcel.java:1328)
12-15 16:48:32.994: ERROR/AndroidRuntime(347): at android.os.Parcel.readException(Parcel.java:1276)
12-15 16:48:32.994: ERROR/AndroidRuntime(347): at com.android.internal.telephony.ISms$Stub$Proxy.sendText(ISms.java:369)
12-15 16:48:32.994: ERROR/AndroidRuntime(347): at android.telephony.SmsManager.sendTextMessage(SmsManager.java:87)
12-15 16:48:32.994: ERROR/AndroidRuntime(347): at android.telephony.gsm.SmsManager.sendTextMessage(SmsManager.java:79)
12-15 16:48:32.994: ERROR/AndroidRuntime(347): at com.test.RussianSmsProblemActivity.sendSms(RussianSmsProblemActivity.java:102)
12-15 16:48:32.994: ERROR/AndroidRuntime(347): at com.test.RussianSmsProblemActivity$1.onClick(RussianSmsProblemActivity.java:39)
12-15 16:48:32.994: ERROR/AndroidRuntime(347): at android.view.View.performClick(View.java:2485)
12-15 16:48:32.994: ERROR/AndroidRuntime(347): at android.view.View$PerformClick.run(View.java:9080)
12-15 16:48:32.994: ERROR/AndroidRuntime(347): at android.os.Handler.handleCallback(Handler.java:587)
12-15 16:48:32.994: ERROR/AndroidRuntime(347): at android.os.Handler.dispatchMessage(Handler.java:92)
12-15 16:48:32.994: ERROR/AndroidRuntime(347): at android.os.Looper.loop(Looper.java:123)
12-15 16:48:32.994: ERROR/AndroidRuntime(347): at android.app.ActivityThread.main(ActivityThread.java:3683)
12-15 16:48:32.994: ERROR/AndroidRuntime(347): at java.lang.reflect.Method.invokeNative(Native Method)
12-15 16:48:32.994: ERROR/AndroidRuntime(347): at java.lang.reflect.Method.invoke(Method.java:507)
12-15 16:48:32.994: ERROR/AndroidRuntime(347): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-15 16:48:32.994: ERROR/AndroidRuntime(347): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-15 16:48:32.994: ERROR/AndroidRuntime(347): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:3)
我认为这种情况正在发生,因为俄语字符在UTF-8中消耗2
个字节。因此消息超出了160
字节限制。请尝试使用sendMultiPartMessage()
。
答案 1 :(得分:1)
在将其交给SmsManager之前,您是否尝试以不同的方式对String message
进行编码?