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()
at
onClick
in
BatchActivity
class



您不应该使用“new”来创建从Activity派生的类的实例,因为此类实例未正确启动。

@Pairaba请参阅LineDiscountActivity.java的第1498行。此行中的某些内容为null请尝试用getApplicationContext()替换getBaseContext()你试过调试你的代码并查看哪个元素是空的吗?!这行写的是'registerReceiver(new BroadcastReceiver(){'null
if(sendSMS.equals(“1”)
,然后是
sendSMS(arrPhone,sms.toString())
…我不明白…once
sendSMS
是字符串,而另一个时间是methode?@Pairaba请参阅LineDefficity.java的第1498行。此行中有一些内容为空请尝试用getApplicationContext()替换getBaseContext()你试过调试你的代码并查看哪个元素是空的吗?!这行写的是'registerReceiver(new BroadcastReceiver(){'null
if(sendSMS.equals(“1”)
,然后是
sendSMS(arrPhone,sms.toString())
…我不明白…once
sendSMS
是字符串,而另一个时间是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);  
     }