0

当我发送短信时,我收到了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(){

请告诉我我的代码有什么问题?

4

4 回答 4

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;
于 2012-06-20T10:33:08.633 回答
1

我想你should use arg0 (which is Context ) instead of getBaseContext()里面 onReceive(Context arg0, Intent arg1) in toast ......

也改变getBaseContext

PendingIntent.getBroadcast(getBaseContext(), 0  

<your_actvity_name>.this

于 2012-06-20T10:33:20.973 回答
1

由于您使用的是 ActivityGroup 使用此答案如何在活动组中获取子活动?获取实例而不是在课堂上LineDiscountActivity调用 new LineDiscountActivity()onClickBatchActivity

你永远不应该使用“new”来创建从 Activity 派生的类的实例,因为这样的实例没有正确启动。

于 2012-06-20T11:24:42.497 回答
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);  
     }

它对我来说非常有用。我希望它会对你有所帮助。

于 2012-06-20T11:03:57.810 回答