android - 当我在 android 中使用发送多部分文本消息发送消息时出现 NullPointerException?

标签 android sendmessage

您好,当我发送消息时收到 NullPointerException 我无法理解为什么会发生这种情况。我在下面发布了我的代码:-

BroadCastReceiver:-

package z.z.z;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;

public class SMSReceiver extends BroadcastReceiver  
{
    private SmsMessage[] msgs;
    private String strNo,strMsgText;

    @Override
    public void onReceive(Context context, Intent intent) 
    {
        try
        {
            Bundle bundle = intent.getExtras();        
            msgs = null;
            String str = "";         

            if (bundle != null)
            {
                Object[] pdus = (Object[]) bundle.get("pdus");
                msgs = new SmsMessage[pdus.length];
                for (int i=0; i<msgs.length; i++)
                {
                    msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
                    str += "SMS from " + msgs[i].getOriginatingAddress();                     
                    str += ":";
                    str += msgs[i].getMessageBody().toString();
                    str += "\n";
                    strNo = msgs[i].getOriginatingAddress();    
                    strMsgText = msgs[i].getDisplayMessageBody();

                }

                context.startService(new Intent(context,IncomingSMSService.class));

            }
        }
        catch (Exception e) 
        {
            e.printStackTrace();
        }
    }

}

传入短信服务:-

package z.z.z;

import java.util.ArrayList;

import z.z.z.Global;
import z.z.z.SMSService;

import android.app.Service;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.IBinder;
import android.util.Log;

public class IncomingSMSService extends Service  
{

    private String TAG = "IncomingSMSService";

    @Override
    public IBinder onBind(Intent intent)
    {
        return null;
    }

    @Override
    public void onCreate() 
    {
        /* start service */

        Log.d(TAG ,"****** in service onCreate  **----- ");
        super.onCreate();

        try
        {
            Log.d(TAG ,"****** in service try  **----- ");
            /* Read SMS from INBOX  */
            Uri uriSMSURI = Uri.parse("content://sms/inbox");      
            Cursor cur = getContentResolver().query(uriSMSURI, null, "read = 0", null,null);      
            String sms = "";      
            while (cur.moveToNext()) 
            {          
                    sms += "From :" + cur.getString(2) + " : " + cur.getString(11)+"\n";
    //              Log.d(TAG, "in SMS Service sms in loop :: "+ sms);
            }
            Log.d(TAG, "in SMS Service sms :: "+ sms);


            Log.d(TAG ,"********** reverseNum ******  "+ Global.destNo);
            ArrayList<String> ayyMsg = new ArrayList<String>();
            ayyMsg.add(sms);
            SMSService.sendSMS(getBaseContext(), ayyMsg, Global.destNo);
        }
        catch (Exception e) 
        {
            e.printStackTrace();
        }
    }}

发送短信

    package z.z.z;

        import java.util.ArrayList;

        import z.z.z.common.RMAReceiver;

        import android.app.PendingIntent;
        import android.content.Context;
        import android.content.Intent;
        import android.content.IntentFilter;
        import android.telephony.SmsManager;
        import android.util.Log;

        public class SMSService 
        {

            static String TAG = "SMSService";
            static String SENT = "SMS_SENT";
            static String DELIVERED = "SMS_DELIVERED";

        //  static Context mContext;
            static RMAReceiver rmaReceiver = null;
            public static void sendSMS(Context context,ArrayList<String> message, String destNumber)
            {   
                try
                {



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

                    PendingIntent deliveredPI = PendingIntent.getBroadcast(context, 0,new Intent(DELIVERED), PendingIntent.FLAG_CANCEL_CURRENT);

                    ArrayList<PendingIntent> ayySentPI = new ArrayList<PendingIntent>();
                    ayySentPI.add(sentPI);

                    ArrayList<PendingIntent> ayyDeliveredPI = new ArrayList<PendingIntent>();
                    ayyDeliveredPI.add(deliveredPI);

                    receiver(context);

                    SmsManager sms = SmsManager.getDefault();

                    sms.sendMultipartTextMessage(destNumber, null, message, ayySentPI, ayyDeliveredPI);

                }
                catch (Exception e) 
                {
                    e.printStackTrace();
                }
            }

**receiver**:-

public static void receiver(Context context)
    {
        rmaReceiver = RMAReceiver.getSingleInstance();
        context.registerReceiver(rmaReceiver, new IntentFilter(SENT));

        //---when the SMS has been delivered---
        context.registerReceiver(rmaReceiver, new IntentFilter(DELIVERED));  
    }

RMAR接收器

package z.z.z;

import z.z.z.SMSService;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsManager;
import android.widget.Toast;

public class RMAReceiver extends BroadcastReceiver 
{
    private static RMAReceiver singleInstance = null;
    String msg;

    private RMAReceiver()
    {       
    }

    public static RMAReceiver getSingleInstance()
    {
        if(singleInstance == null) singleInstance = new RMAReceiver();
        return singleInstance;
    }

    @Override
    public void onReceive(Context context, Intent intent) 
    {
        System.out.println("Result Code: "+getResultCode());
//      resultCode = getResultCode();
        switch (getResultCode())
        {
            case Activity.RESULT_OK:
                msg = "SMS sent/delivered";
//              Toast.makeText(context, "SMS sent/delivered", 
//                      Toast.LENGTH_SHORT).show();
//              SMSService.sendSMS(context, "SMS sent/delivered", Global.confirmNo);
                break;
            case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                msg = "Generic failure";
//              Toast.makeText(context, "Generic failure", 
//                      Toast.LENGTH_SHORT).show();
//              SMSService.sendSMS(context, "Generic failure", Global.confirmNo);
                break;
            case SmsManager.RESULT_ERROR_NO_SERVICE:
                msg = "No service";
//              Toast.makeText(context, "No service", 
//                      Toast.LENGTH_SHORT).show();
//              SMSService.sendSMS(context, "No service", Global.confirmNo);
                break;
            case SmsManager.RESULT_ERROR_NULL_PDU:
                msg = "Null PDU";
//              Toast.makeText(context, "Null PDU", 
//                      Toast.LENGTH_SHORT).show();
//              SMSService.sendSMS(context, "No service", Global.confirmNo);
                break;
            case SmsManager.RESULT_ERROR_RADIO_OFF:
                msg = "Radio off";
//              Toast.makeText(context, "Radio off", 
//                      Toast.LENGTH_SHORT).show();
//              SMSService.sendSMS(context, "No service", Global.confirmNo);
                break;
            case Activity.RESULT_CANCELED:
                msg = "SMS not delivered";
//              Toast.makeText(context, "SMS not delivered", 
//                      Toast.LENGTH_SHORT).show();
//              SMSService.sendSMS(context, "No service", Global.confirmNo);
                break;
        }

        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
        SMSService.sendSMS(context, msg, Global.confirmNo);



    }


}

我的 LogCat 错误:-

11-16 18:26:34.661: WARN/System.err(4990): java.lang.NullPointerException
11-16 18:26:34.673: WARN/System.err(4990):     at android.os.Parcel.readException(Parcel.java:1266)
11-16 18:26:34.673: WARN/System.err(4990):     at android.os.Parcel.readException(Parcel.java:1248)
11-16 18:26:34.673: WARN/System.err(4990):     at com.android.internal.telephony.ISms$Stub$Proxy.sendText(ISms.java:526)
11-16 18:26:34.673: WARN/System.err(4990):     at android.telephony.SmsManager.sendTextMessage(SmsManager.java:109)
11-16 18:26:34.673: WARN/System.err(4990):     at android.telephony.SmsManager.sendMultipartTextMessage(SmsManager.java:263)
11-16 18:26:34.673: WARN/System.err(4990):     at z.z.z.service.SMSService.sendSMS(SMSService.java:109)
11-16 18:26:34.673: WARN/System.err(4990):     at z.z.z.receiver.IncomingSMSService.onStart(IncomingSMSService.java:64)
11-16 18:26:34.677: WARN/System.err(4990):     at android.app.Service.onStartCommand(Service.java:420)
11-16 18:26:34.677: WARN/System.err(4990):     at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3053)
11-16 18:26:34.677: WARN/System.err(4990):     at android.app.ActivityThread.access$3600(ActivityThread.java:125)
11-16 18:26:34.677: WARN/System.err(4990):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2096)
11-16 18:26:34.677: WARN/System.err(4990):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-16 18:26:34.677: WARN/System.err(4990):     at android.os.Looper.loop(Looper.java:123)
11-16 18:26:34.677: WARN/System.err(4990):     at android.app.ActivityThread.main(ActivityThread.java:4627)
11-16 18:26:34.677: WARN/System.err(4990):     at java.lang.reflect.Method.invokeNative(Native Method)
11-16 18:26:34.677: WARN/System.err(4990):     at java.lang.reflect.Method.invoke(Method.java:521)
11-16 18:26:34.677: WARN/System.err(4990):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:871)
11-16 18:26:34.681: WARN/System.err(4990):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)
11-16 18:26:34.681: WARN/System.err(4990):     at dalvik.system.NativeStart.main(Native Method)

这是我的代码,当收到任何消息时,我的 BroadCastReceiver 就会调用。从那时起,我开始了从收件箱获取未读消息的服务,并将这些消息作为消息发送。

我从收件箱收到了消息,但是当我将该消息作为消息发送时,我收到了“Nullpointer Exception”。

谁能告诉我哪里错了?

编辑:

我发现当我在消息中发送文本长度时,它会给我 NullPointer 异常。当我在谷歌上搜索时,我发现问题是通过使用 sendMultipartTextMessage 而不是使用 sendTextMessage 解决的。但我已经使用了该方法,所以为什么会出现此错误?

谢谢。

最佳答案

你好,我收到了 solution .我很高兴与大家分享。我用过

ArrayList messages = sms.divideMessage(message);

SmsManager sms = SmsManager.getDefault();
ArrayList<String> messages = sms.divideMessage(message);
sms.sendMultipartTextMessage(destNumber, null, messages, ayySentPI, ayyDeliveredPI);

关于android - 当我在 android 中使用发送多部分文本消息发送消息时出现 NullPointerException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8151877/

相关文章:

java - 如何修复 'android.os.NetworkOnMainThreadException' ?

java - 如何从 Activity 类之外更改 android 墙纸?

java - 奇怪的 SocketExceptions : connection reset and Connection timed out

java - 在 eclipse java android 中找不到启动器 Activity

android - AudioTrack 的全局死亡

c++ - MFC 向按钮发送消息(子级到父级)

c++ - 获取垃圾文本。如何将字符数组变量转换为 LPARAM 类型?

c# - 在不活动的应用程序中模拟鼠标移动/单击/按键

erlang - 在erlang中通过变量和 "direct"传递的消息

vb.net - 使用 SendMessage 将文本从 VB 发送到 Delphi 应用程序