2011-06-14 168 views
0

當發送一些字符串文本作爲一個短信給其他模擬器時,Iam得到一個空指針異常。我有一個按鈕,點擊它會將textView中的文本作爲一個短信發送給其他模擬器。android sms空指針異常

這裏的TextView和Button類

public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.ticketprocess); 




     SecureMessagesActivity sma = new SecureMessagesActivity(); 

     message = sma.getMessageBody(); 
     tv= (TextView) findViewById(R.id.textView1); 
      tv.setText(message); 



     submit = (Button) findViewById(R.id.button1); 
     submit.setOnClickListener(new OnClickListener() { 
public void onClick(View v) { 

    SendSms sms = new SendSms(); 
    sms.sendSMS("15555215556", message); 

}}); 

    } 

的代碼,這裏是sendsms類上點擊按鈕設備代碼

public class SendSms extends Activity { 

    public void sendSMS(String phoneNumber, String message) 
    {   
     String SENT = "SMS_SENT"; 
     String DELIVERED = "SMS_DELIVERED"; 

     PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, 
      new Intent(SENT), 0); 

     PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, 
      new Intent(DELIVERED), 0); 

     //---when the SMS has been sent--- 
     registerReceiver(new BroadcastReceiver(){ 
      @SuppressWarnings("deprecation") 
      @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(); 
         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; 
       } 
      } 
     }, 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(); 
         break;       
       } 
      } 
     }, new IntentFilter(DELIVERED));   

     SmsManager sms = SmsManager.getDefault(); 
     sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);   
    } 

} 

現在得到強制關閉的消息,在錯誤logcat是

06-14 17:47:47.249: ERROR/AndroidRuntime(8403): FATAL EXCEPTION: main 
06-14 17:47:47.249: ERROR/AndroidRuntime(8403): java.lang.NullPointerException 
06-14 17:47:47.249: ERROR/AndroidRuntime(8403):  at android.content.ContextWrapper.getPackageName(ContextWrapper.java:120) 
06-14 17:47:47.249: ERROR/AndroidRuntime(8403):  at android.app.PendingIntent.getBroadcast(PendingIntent.java:226) 
06-14 17:47:47.249: ERROR/AndroidRuntime(8403):  at com.mypackage.ULWAFMS1.SendSms.sendSMS(SendSms.java:38) 
06-14 17:47:47.249: ERROR/AndroidRuntime(8403):  at com.mypackage.ULWAFMS1.page1$1.onClick(page1.java:43) 
06-14 17:47:47.249: ERROR/AndroidRuntime(8403):  at android.view.View.performClick(View.java:2408) 
06-14 17:47:47.249: ERROR/AndroidRuntime(8403):  at android.view.View$PerformClick.run(View.java:8816) 
06-14 17:47:47.249: ERROR/AndroidRuntime(8403):  at android.os.Handler.handleCallback(Handler.java:587) 
06-14 17:47:47.249: ERROR/AndroidRuntime(8403):  at android.os.Handler.dispatchMessage(Handler.java:92) 
06-14 17:47:47.249: ERROR/AndroidRuntime(8403):  at android.os.Looper.loop(Looper.java:123) 
06-14 17:47:47.249: ERROR/AndroidRuntime(8403):  at android.app.ActivityThread.main(ActivityThread.java:4627) 
06-14 17:47:47.249: ERROR/AndroidRuntime(8403):  at java.lang.reflect.Method.invokeNative(Native Method) 
06-14 17:47:47.249: ERROR/AndroidRuntime(8403):  at java.lang.reflect.Method.invoke(Method.java:521) 
06-14 17:47:47.249: ERROR/AndroidRuntime(8403):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
06-14 17:47:47.249: ERROR/AndroidRuntime(8403):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
06-14 17:47:47.249: ERROR/AndroidRuntime(8403):  at dalvik.system.NativeStart.main(Native Method) 
+0

您是否在AndroidManifest.xml中詢問權限? – BadSkillz 2011-06-14 12:43:41

+0

檢查ID的textView和按鈕...確保他們不返回空.. – ngesh 2011-06-14 12:45:19

+0

是的,我忘記了在清單文件中添加發送權限。謝謝大家。 – TechnocraT 2011-06-15 04:48:36

回答

2

問題發生在你正在創建你的地方sendSms方法開始時的對象。你正在實施SendSMS作爲一項活動,但我認爲它不是必須的,並且它沒有被正確初始化爲一個活動,所以你看到了這個失敗。

拿出從sendsms類extends Activity,並改變你的sendSms方法來啓動如下:

public class SendSms { 

    public void sendSMS(Context context, String phoneNumber, String message) 
    {   
     String SENT = "SMS_SENT"; 
     String DELIVERED = "SMS_DELIVERED"; 

     PendingIntent sentPI = PendingIntent.getBroadcast(context, 0, 
      new Intent(SENT), 0); 

     PendingIntent deliveredPI = PendingIntent.getBroadcast(context, 0, 
      new Intent(DELIVERED), 0); 

現在,你需要把它從你的onClickListener調用爲sendSms(this, "15555215556", message);;

+0

在他的代碼中,他正在使用一個按鈕發送短信 – BadSkillz 2011-06-14 12:52:32

+0

馬克是正確的,我想.. – ngesh 2011-06-14 12:56:38

+0

是的,現在他是:P – BadSkillz 2011-06-14 12:59:01