2011-07-21 72 views
0

我正在做一個應用程序,我必須從CDMA手機發送短信,但它會引發空指針異常。我使用從CDMA發送短信(htc evo)拋出空指針異常

smsmanager.sendDataMessage(phonenumber, null, (short)1001, msgStr.getBytes(), sentPI, deliveredPI); 

它拋出如下錯誤: -

07-21 14:53:26.875: ERROR/AndroidRuntime(8645): FATAL EXCEPTION: main 
07-21 14:53:26.875: ERROR/AndroidRuntime(8645): java.lang.NullPointerException 
07-21 14:53:26.875: ERROR/AndroidRuntime(8645):  at android.os.Parcel.readException(Parcel.java:1253) 
07-21 14:53:26.875: ERROR/AndroidRuntime(8645):  at android.os.Parcel.readException(Parcel.java:1235) 
07-21 14:53:26.875: ERROR/AndroidRuntime(8645):  at com.android.internal.telephony.ISms$Stub$Proxy.sendData(ISms.java:558) 
07-21 14:53:26.875: ERROR/AndroidRuntime(8645):  at android.telephony.SmsManager.sendDataMessage(SmsManager.java:524) 
07-21 14:53:26.875: ERROR/AndroidRuntime(8645):  at prototype.smsonport.SmsOnPortActivity.sendSMS(SmsOnPortActivity.java:139) 
07-21 14:53:26.875: ERROR/AndroidRuntime(8645):  at prototype.smsonport.SmsOnPortActivity.onClick(SmsOnPortActivity.java:38) 
07-21 14:53:26.875: ERROR/AndroidRuntime(8645):  at android.view.View.performClick(View.java:2408) 
07-21 14:53:26.875: ERROR/AndroidRuntime(8645):  at android.view.View$PerformClick.run(View.java:8817) 
07-21 14:53:26.875: ERROR/AndroidRuntime(8645):  at android.os.Handler.handleCallback(Handler.java:587) 
07-21 14:53:26.875: ERROR/AndroidRuntime(8645):  at android.os.Handler.dispatchMessage(Handler.java:92) 
07-21 14:53:26.875: ERROR/AndroidRuntime(8645):  at android.os.Looper.loop(Looper.java:144) 
07-21 14:53:26.875: ERROR/AndroidRuntime(8645):  at android.app.ActivityThread.main(ActivityThread.java:4937) 
07-21 14:53:26.875: ERROR/AndroidRuntime(8645):  at java.lang.reflect.Method.invokeNative(Native Method) 
07-21 14:53:26.875: ERROR/AndroidRuntime(8645):  at java.lang.reflect.Method.invoke(Method.java:521) 
07-21 14:53:26.875: ERROR/AndroidRuntime(8645):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
07-21 14:53:26.875: ERROR/AndroidRuntime(8645):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
07-21 14:53:26.875: ERROR/AndroidRuntime(8645):  at dalvik.system.NativeStart.main(Native Method) 

下面是函數

 String SENT = "SMS_SENT"; 
    String DELIVERED = "SMS_DELIVERED"; 

    PendingIntent senDPI = PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent(SENT), 
      0); 

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

    SmsManager sms = SmsManager.getDefault(); 

    short s = Short.parseShort(portStr); 
    short port = s; 

    Log.d("Data", "Sending SMS TO :: "+phonenumber); 
    Log.d("Data", "Sending SMS MESSAGE :: "+msgStr); 
    Log.d("Data", "Sending SMS on PORT :: "+port); 

    sms.sendDataMessage(phonenumber, null, port, msgStr.getBytes(), senDPI, deliveredPI); 

所有PHONENUMBER,msgStr,港口來源於我的XML佈局文件的EDITTEXT領域。 我在這裏錯過了什麼? 幫助讚賞。

+0

那一行代碼不足以診斷您的問題。你需要發佈更多的代碼。 – Squonk

+0

在你的日誌中你有'toStr' - 是電話號碼嗎?如果是這樣,它從哪裏來的代碼?另外,'phonenumber'在'sendDataMessage(...)'的調用中來自哪裏? – Squonk

+0

此外,來自API DEMO 8的SMS消息示例代碼也不適用於htc evo CDMA電話 – user755499

回答

2
send.setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      String phoneNo = "Phone number"; 
      String message = "Content of the msg"; 
      if (phoneNo.length() > 0 && message.length() > 0) { 
       TelephonyManager telMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); 
       int simState = telMgr.getSimState(); 
       switch (simState) { 
       case TelephonyManager.SIM_STATE_ABSENT: 
        displayAlert(); 
        break; 
       case TelephonyManager.SIM_STATE_NETWORK_LOCKED: 
        // do something 
        break; 
       case TelephonyManager.SIM_STATE_PIN_REQUIRED: 
        // do something 
        break; 
       case TelephonyManager.SIM_STATE_PUK_REQUIRED: 
        // do something 
        break; 
       case TelephonyManager.SIM_STATE_READY: 
        // do something 
        sendSMS(phoneNo, message); 
        break; 
       case TelephonyManager.SIM_STATE_UNKNOWN: 
        // do something 
        break; 
       } 

      } else { 
       Toast.makeText(getBaseContext(), 
         "Please enter both phone number and message.", 
         Toast.LENGTH_SHORT).show(); 
      } 

     } 



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

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

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

    // ---when the SMS has been sent--- 

    registerReceiver(new BroadcastReceiver() { 

     @Override 
     public void onReceive(Context arg0, Intent arg1) { 
      switch (getResultCode()) { 
      case Activity.RESULT_OK: 
       Toast.makeText(YourActivity.this, "SMS sent", 
         Toast.LENGTH_SHORT).show(); 
       break; 
      case SmsManager.RESULT_ERROR_GENERIC_FAILURE: 
       Toast.makeText(YourActivity.this, "Generic failure", 
         Toast.LENGTH_SHORT).show(); 
       break; 
      case SmsManager.RESULT_ERROR_NO_SERVICE: 
       Toast.makeText(YourActivity.this, "No service", 
         Toast.LENGTH_SHORT).show(); 
       break; 
      case SmsManager.RESULT_ERROR_NULL_PDU: 
       Toast.makeText(YourActivity.this, "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(YourActivity.this, "SMS delivered", 
         Toast.LENGTH_SHORT).show(); 
       break; 
      case Activity.RESULT_CANCELED: 
       Toast.makeText(YourActivity.this, "SMS not delivered", 
         Toast.LENGTH_SHORT).show(); 
       break; 
      } 
     } 
    }, new IntentFilter(DELIVERED)); 

    SmsManager sms = SmsManager.getDefault(); 
    sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI); 
    Intent smsintent = new Intent(YourActivity.this, SecondActivity.Activity); 
    startActivity(smsintent); 
} 
+1

這將如何有用? – Squonk

+0

@ jack-sparrow你發佈的鏈接是關於如何閱讀contacts.My問題是從CDMA手機發送短信引發空指針異常。 – user755499

+0

我已經更新了我的答案。它會工作。 – Manikandan