从CDMA发送短信(htc evo)会抛出NullPointer异常

时间:2011-07-21 09:28:06

标签: android

我正在做一个应用,我必须从cdma手机发送短信,但它会抛出空指针异常。我正在使用

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,port都来自我的xml布局文件的Edittext字段。 我在这里错过了什么? 帮助感谢。

1 个答案:

答案 0 :(得分: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);
}