smsManager.sendTextMessage崩溃的应用程序

时间:2012-10-10 11:57:37

标签: android sms

我正在尝试从我的应用发送短信,但它正在崩溃。

logcat的:

10-10 17:23:08.420: ERROR/AndroidRuntime(279): FATAL EXCEPTION: main
    java.lang.NullPointerException
    at android.os.Parcel.readException(Parcel.java:1253)
    at android.os.Parcel.readException(Parcel.java:1235)
    at com.android.internal.telephony.ISms$Stub$Proxy.sendText(ISms.java:369)
    at android.telephony.SmsManager.sendTextMessage(SmsManager.java:87)
    at com.example.SendAsSmsActivity.onClickSend(SendAsSmsActivity.java:101)
    at com.example.SendAsSmsActivity$1.onClick(SendAsSmsActivity.java:39)
    at android.view.View.performClick(View.java:2408)
    at android.view.View$PerformClick.run(View.java:8816)
    at android.os.Handler.handleCallback(Handler.java:587)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:123)
    at android.app.ActivityThread.main(ActivityThread.java:4627)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:521)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    at dalvik.system.NativeStart.main(Native Method)

代码类似于 SMS manager example

3 个答案:

答案 0 :(得分:2)

你能写下你的代码吗?

此代码适用于我:

   private void sendSMS(String phoneNumber, String message)
   {
       SmsManager sms = SmsManager.getDefault();
       sms.sendTextMessage(phoneNumber, null, message, null, null);
    }

在清单中:

<uses-permission android:name="android.permission.SEND_SMS"/>

答案 1 :(得分:1)

使用这样的东西发送短信...

 //---sends an SMS message to another device---
@SuppressWarnings("deprecation")
private void sendSMS(String phoneNumber, String message)
{        
    Log.v("phoneNumber",phoneNumber);
    Log.v("MEssage",message);
 //   PendingIntent pi = PendingIntent.getActivity(this, 0,
 //       new Intent(this, Main.class), 0);                
    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(phoneNumber, null, message, null, null);        
}   

简单地称呼它......

sendSMS("+4956421246", "test");

答案 2 :(得分:0)

试试这个

public void send_sms(String Docket_no,String mob_no)
{
    String phoneNo = mob_no;
    String sms = "Docket succesfully added.  Docket No :" + Docket_no;


    try {
        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage(phoneNo, null, sms, null, null);
        Toast.makeText(getApplicationContext(), "SMS Sent!",
                Toast.LENGTH_LONG).show();
    } catch (Exception e) {
        Toast.makeText(getApplicationContext(),
                "SMS faild, please try again later!",
                Toast.LENGTH_LONG).show();
        e.printStackTrace();
    }   
}