当我发送短信时,我收到了NullPointerException
。我认为可能是context
. 我在TabActivityGroup
.
这是我的代码:
if(sendSMS.equals("1")) {
double invoiceVal = getInvoiceValue();
String phone = getPhoneNo();
String[] arrPhone = phone.split(";");
System.out.println("==arrPhone==" +arrPhone.length);
StringBuffer sms = new StringBuffer();
sms.append("Miscellaneous Sale");
sms.append("\n");
sms.append("\n");
sms.append("Territory: " + territoryCode +"\n");
sms.append("Sales Rep: " + strExecutive +"\n");
sms.append("Route Code: " + routeCode +"\n");
sms.append("Retailer Code: " + retailCode +" " +retailerName +"\n");
sms.append("Invoice No: " + printInPrefix+"\\"+printInvoiceNo +"\n");
sms.append("Invoice Value: " + df.format(invoiceVal) +"\n");
sendSMS(arrPhone, sms.toString());
}
String sendSMS(String[] arrPhone, String message){
String SENT = "SMS_SENT";
smsStatus ="YES";
String DELIVERED = "SMS_DELIVERED";
String phoneNumber = "";
try {
for (int i = 0; i < arrPhone.length; i++) {
phoneNumber = arrPhone[i];
System.out.println("==phoneNumber==" +phoneNumber);
PendingIntent sentPI = PendingIntent.getBroadcast(getBaseContext(), 0, new Intent(SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(getBaseContext(), 0,new Intent(DELIVERED), 0);
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()){
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS sent", Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(), "Generic failure", Toast.LENGTH_SHORT).show();
smsStatus ="NO";
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(), "No service", Toast.LENGTH_SHORT).show();
smsStatus ="NO";
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(), "Null PDU", Toast.LENGTH_SHORT).show();
smsStatus ="NO";
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(), "Radio off", Toast.LENGTH_SHORT).show();
smsStatus ="NO";
break;
}
}
}, new IntentFilter(SENT));
//---when the SMS has been delivered---
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()){
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS delivered", Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(), "SMS not delivered", Toast.LENGTH_SHORT).show();
smsStatus ="NO";
break;
}
}
}, new IntentFilter(DELIVERED));
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
}
} catch (Exception e) {
e.printStackTrace();
smsStatus ="NO";
}
return smsStatus;
}
错误:
06-21 15:46:31.335: W/System.err(7510): java.lang.NullPointerException
06-21 15:46:31.345: W/System.err(7510): at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:349)
06-21 15:46:31.345: W/System.err(7510): at xont.ventura.controller.sales.LineDiscountActivity.sendSMS(LineDiscountActivity.java:1498)
06-21 15:46:31.345: W/System.err(7510): at xont.ventura.controller.sales.LineDiscountActivity.saveInvoice(LineDiscountActivity.java:1354)
06-21 15:46:31.345: W/System.err(7510): at xont.ventura.controller.sales.BatchActivity$2$1.onClick(BatchActivity.java:164)
06-21 15:46:31.345: W/System.err(7510): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:193)
06-21 15:46:31.345: W/System.err(7510): at android.os.Handler.dispatchMessage(Handler.java:130)
06-21 15:46:31.355: W/System.err(7510): at android.os.Looper.loop(Looper.java:154)
06-21 15:46:31.355: W/System.err(7510): at android.app.ActivityThread.main(ActivityThread.java:4668)
06-21 15:46:31.355: W/System.err(7510): at java.lang.reflect.Method.invokeNative(Native Method)
06-21 15:46:31.355: W/System.err(7510): at java.lang.reflect.Method.invoke(Method.java:552)
06-21 15:46:31.355: W/System.err(7510): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:917)
06-21 15:46:31.355: W/System.err(7510): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
06-21 15:46:31.355: W/System.err(7510): at dalvik.system.NativeStart.main(Native Method)
我试过使用SalesActivityGroup.group.getApplicationContext()
, SalesActivityGroup.group.getParent()
, 但是直到我得到同样的错误。
NullPointerException
行没有1498
地方是: registerReceiver(new BroadcastReceiver(){
请告诉我我的代码有什么问题?