Android 发送SMS时出现NullPointerException
当我发送短信时,我收到的是Android 发送SMS时出现NullPointerException,android,sms,nullpointerexception,Android,Sms,Nullpointerexception,当我发送短信时,我收到的是NullPointerException。我认为这可能是上下文中的问题。我正在TabActivityGroup中使用此选项 这是我的代码: if(sendSMS.equals("1")) { double invoiceVal = getInvoiceValue(); String phone = getPhoneNo(); String[] arrPhone = phone.split(";"); System.out.prin
NullPointerException
。我认为这可能是上下文中的问题。我正在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;
}
我尝试过使用SalesActivityGroup.group.getApplicationContext()
,SalesActivityGroup.group.getParent()
,但直到出现同样的错误
NullPointerException
行号1498
位置是:registerReceiver(新广播接收器(){
请告诉我代码中有什么问题?将getBaseContext()
替换为arg0
这里有错误
onReceive
在onReceive
中为您提供上下文。那么您不应该使用getBaseContext()
public void onReceive(Context arg0,Intent arg1){将getBaseContext()替换为arg0
这里有错误
onReceive
在onReceive
中为您提供上下文。那么您不应该使用getBaseContext()
public void onReceive(Context arg0,Intent arg1){我认为您应该在toast中的onReceive(Context arg0,Intent arg1)内部使用arg0(Context)而不是getBaseContext()
还可以在中更改getBaseContext
PendingIntent.getBroadcast(getBaseContext(), 0
使用。这个我认为应该在toast中的onReceive(Context arg0,Intent arg1)内部使用arg0(即上下文)而不是getBaseContext()
还可以在中更改getBaseContext
PendingIntent.getBroadcast(getBaseContext(), 0
使用。此尝试在onStart()中启动接收器
在一个单独的班级里接受它
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()中注销接收器
它对我来说工作得很好。我希望它能帮助你。试着用你的onStart()启动你的接收器。
在一个单独的班级里接受它
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()中注销接收器
它对我来说非常有效。我希望它能对您有所帮助。既然您正在使用ActivityGroup,请使用此答案获取LineDiscountActivity
的实例,而不是在BatchActivity
类中的onClick
处调用new LineDiscountActivity()
您不应该使用“new”来创建从Activity派生的类的实例,因为此类实例未正确初始化。因为您使用的是ActivityGroup,所以请使用此答案来获取LineDiscountActivity
的实例,而不是调用new LineDiscountActivity()
atonClick
inBatchActivity
class
您不应该使用“new”来创建从Activity派生的类的实例,因为此类实例未正确启动。@Pairaba请参阅LineDiscountActivity.java的第1498行。此行中的某些内容为null请尝试用getApplicationContext()替换getBaseContext()你试过调试你的代码并查看哪个元素是空的吗?!这行写的是'registerReceiver(new BroadcastReceiver(){'nullif(sendSMS.equals(“1”)
,然后是sendSMS(arrPhone,sms.toString())
…我不明白…oncesendSMS
是字符串,而另一个时间是methode?@Pairaba请参阅LineDefficity.java的第1498行。此行中有一些内容为空请尝试用getApplicationContext()替换getBaseContext()你试过调试你的代码并查看哪个元素是空的吗?!这行写的是'registerReceiver(new BroadcastReceiver(){'nullif(sendSMS.equals(“1”)
,然后是sendSMS(arrPhone,sms.toString())
…我不明白…oncesendSMS
是字符串,而另一个时间是methode?我在这里收到了错误registerReceiver(new BroadcastReceiver(){
。无论如何,根据你的建议,我做了并尝试了注释Toast
消息,即使是因为同样的错误。你注释了所有Toast消息并检查了吗?@vipulsah lol,lol,此代码取自你的答案…这是错误的:)@SelvinLOL我忍不住笑了:D.可能是她在其他地方出了问题我在当前活动类中使用了该方法。她可能在其他活动中使用了该方法是的。我对所有Toast
消息进行了注释。尽管我收到了相同的错误,但我在此处收到了错误registerReceiver(new BroadcastReceiver(){
。无论如何,根据你的建议,我做了并尝试了注释Toast
消息,即使是因为同样的错误。你注释了所有Toast消息并检查了吗?@vipulsah lol,lol,此代码取自你的答案…这是错误的:)@SelvinLOL我忍不住笑了:D.可能是她在其他地方出了问题我在当前活动类中使用了该方法。她可能在其他活动中使用了该方法是的。我对所有Toast
消息进行了注释。尽管我收到了相同的错误,但我在此处收到了错误registerReceiver(new BroadcastReceiver(){
。无论如何,根据你的建议,我做了并尝试了注释Toast
消息,尽管是由于同样的错误。我在这里得到了错误registerReceiver(new BroadcastReceiver(){
。无论如何,根据你的建议,我做了并尝试了注释Toast
消息,尽管是由于同样的错误。
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;
}
}
}
@Override
protected void onStop() {
super.onStop();
unregisterReceiver(smsReceiver);
}