当我发送短信时,我收到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(){
请告诉我代码中的问题是什么?
答案 0 :(得分:3)
将getBaseContext()
替换为arg0
你这里有错误。
onReceive
为您提供onReceive
中的背景信息。然后您不应使用getBaseContext()
public void onReceive(Context arg0, Intent arg1) { <--- Use this context i.e arg0
switch (getResultCode()){
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS sent", Toast.LENGTH_SHORT).show();
break;
答案 1 :(得分:1)
我认为你在{togceive(Context arg0,Intent arg1)intast里面should use arg0 (which is Context ) instead of getBaseContext()
......
也改变了
中的getBaseContextPendingIntent.getBroadcast(getBaseContext(), 0
<your_actvity_name>.this
答案 2 :(得分:0)
尝试在onStart()
private SMSReceiver smsReceiver;
@Override
public void onStart()
{
super.onStart();
smsReceiver = new SMSReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(SENT);
registerReceiver(smsReceiver, intentFilter);
//Start our own service
Intent intent = new Intent(Activity.this,
SMSReceiver.class);
startService(intent);
}
在单独的课程中接收
private class SMSReceiver extends 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(), "Invalid PhoneNumber",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(), "No service",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(), "Null PDU",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(), "Radio off",
Toast.LENGTH_SHORT).show();
break;
}
}
}
别忘了在onStop()
取消注册接收器 @Override
protected void onStop() {
super.onStop();
unregisterReceiver(smsReceiver);
}
它非常适合我。我希望它会对你有所帮助。
答案 3 :(得分:0)
因为您正在使用ActivityGroup,所以请使用此回复How to get child activity in activity group?来获取LineDiscountActivity
的实例,而不是在new LineDiscountActivity()
类<{1}}的{{1}}调用onClick
你永远不应该使用“new”来创建派生自Activity的类的实例,因为这样的实例没有正确启动。