sendMultipartTextMessage ISms $ Stub $ Proxy.sendMultipartTextOnSubscription NullPointerException

时间:2012-03-07 08:07:35

标签: android broadcastreceiver android-pendingintent

我得到以下代码的nullpointerexception:

我的短信超过160个字符,所以我使用divide()然后添加一个发送的PendingIntent来分割它们。

有时会出现此异常。我的手机号码和短信数据不为空;

   String SENT = "SMS_SENT";

    PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(
            SENT), 0);

    // ---when the SMS has been sent---
    BroadcastReceiver sentSMSStatusReciever = new BroadcastReceiver() {
        @Override
        public void onReceive(Context arg0, Intent arg1) {
            switch (getResultCode()) {
                   //TOAST
            }
            unregisterReceiver(this);
        }
    };
    registerReceiver(sentSMSStatusReciever, new IntentFilter(SENT));

    SmsManager smsManager = SmsManager.getDefault();

    // since message is greater than 160 chars
    ArrayList smsContructedList = smsManager.divideMessage("SMS Content goes here ....");

    ArrayList<PendingIntent> sentPIList = new ArrayList<PendingIntent>(1);

    sentPIList.add(sentPI);

    smsManager.sendMultipartTextMessage("Mobile Number goes here", null,
            smsContructedList, sentPIList, null);

日志:

java.lang.NullPointerException
E/AndroidRuntime( 4527):    at android.os.Parcel.readException(Parcel.java:1328)
E/AndroidRuntime( 4527):    at android.os.Parcel.readException(Parcel.java:1276)
E/AndroidRuntime( 4527):    at com.android.internal.telephony.ISms$Stub$Proxy.sendMultipartTextOnSubscription(ISms.java:1351)
E/AndroidRuntime( 4527):    at android.telephony.SmsManager.sendMultipartTextMessage(SmsManager.java:354)

1 个答案:

答案 0 :(得分:3)

添加:

ArrayList<String> messageArray = smsManager.divideMessage(myMessage);
ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>();
for (int i = 0; i < messageArray.size(); i++)
sentIntents.add(sentPI);
smsManager.sendMultipartTextMessage(sendTo,null,messageArray,sentIntents, null);