0

我正在尝试遍历一个数字列表并向它们发送一条消息,但不知何故我的代码抛出了一个空指针异常,我无法弄清楚问题出在哪里。

这里的代码

import java.util.ArrayList;
import java.util.List;

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

public class MSGHandler {
    String SENT = "SMS_SENT";
    String DELIVERED = "SMS_DELIVERED";
    public BroadcastReceiver smsDeliveredReceiver,smsSentReceiver;


public MSGHandler(Context c,List<String> Name,List<String> Number,String Message)
{
    int Limit = Name.size();
    for(int i = 0; i < Limit;i++)
    {
        if(Number.get(i).length() > 0)
        {
            SmsManager sms = SmsManager.getDefault();
             PendingIntent piSent=PendingIntent.getBroadcast(c, 0, new Intent(SENT), 0);
                PendingIntent piDelivered=PendingIntent.getBroadcast(c, 0, new Intent(DELIVERED),0);

                //---when the SMS has been sent---
                smsSentReceiver = new BroadcastReceiver(){
                    @Override
                    public void onReceive(Context arg0, Intent arg1) {
                        switch (getResultCode())
                        {
                            case Activity.RESULT_OK:

                                break;
                            case SmsManager.RESULT_ERROR_GENERIC_FAILURE:

                                break;
                            case SmsManager.RESULT_ERROR_NO_SERVICE:

                                break;
                            case SmsManager.RESULT_ERROR_NULL_PDU:

                                break;
                            case SmsManager.RESULT_ERROR_RADIO_OFF:

                                break;
                        }
                    }
                };

                //---when the SMS has been delivered---
                smsDeliveredReceiver = new BroadcastReceiver(){
                    @Override
                    public void onReceive(Context arg0, Intent arg1) {
                        switch (getResultCode())
                        {
                            case Activity.RESULT_OK:

                                break;
                            case Activity.RESULT_CANCELED:

                                break;                        
                        }
                    }
                };
                c.registerReceiver(smsSentReceiver, new IntentFilter("SMS_SENT"));
                c.registerReceiver(smsDeliveredReceiver, new IntentFilter("SMS_DELIVERED"));
                sms.sendTextMessage(Number.get(i), null, Name.get(i)+Message, piSent, piDelivered);  

        }
    }
}
}

我在单击按钮的活动中调用此类。像这样

Dialog Alert = new Dialog(MainActivity.this);
            try
            {
                MSGHandler sms = new MSGHandler(MainActivity.this,DBHandler.Contacts_Name_List,DBHandler.Contacts_Number_List,"HII!!!!");
            }catch(Exception e)
            {
                Alert.setTitle(e.toString());
            }
            Alert.show();

我正在使用 Eclipse 并将我的设备用作模拟器

这是日志

 error opening trace file: No such file or directory (2)

  loaded /system/lib/egl/libEGL_mali.so

 loaded /system/lib/egl/libGLESv1_CM_mali.so

 loaded /system/lib/egl/libGLESv2_mali.so
  Enabling debug mode 0

(当应用程序中抛出异常时..日志不显示任何内容)

这是我尝试发送味精并引发异常后的 ADb 日志

在此处输入图像描述

任何解决方案?

4

1 回答 1

0

好吧,睡个好觉对我有很大帮助,我发现抛出异常是因为我试图在一个空白数字上发送消息,即“”一个空格

并且解决方案正在使用

 if(Number.get(i).trim().length() > 0)

代替

 if(Number.get(i).length() > 0)
于 2014-03-22T06:40:18.497 回答