我正在尝试循环遍历数字列表并向他们发送消息,但不知怎的,我的代码抛出了空指针异常,我无法弄清楚问题出在哪里。
这里是代码
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
(当在应用程序中抛出异常时..日志不显示任何内容)
在我尝试发送msg并抛出异常后,这是ADb日志
任何解决方案?
答案 0 :(得分:0)
一个愉快的睡眠帮助了我很多,我发现异常被抛出,因为我试图在空白数字上发信息,即“”空间
,解决方案正在使用
if(Number.get(i).trim().length() > 0)
而不是
if(Number.get(i).length() > 0)