Frage

I was trying to write an app to send a SMS when a button clicked. so I wrote a class that implements OnClickListener interface and in the onClick methode I wrote this code:

         PendingIntent pi = PendingIntent.getActivity(context, 0,
     new Intent(context, ContactsActivity.class), 0);
     SmsManager sms = SmsManager.getDefault();
     sms.sendTextMessage(number, null, text, pi, null);

in here context is the activity that I got an instance of my OnClickListener. so my app crashes in the last line!!

I even putted:

        number = number.replace(" ", "");
    number = number.replace("-", "");

before the code to make the number that I've got from contact list look correct! but didn't help!

and I've checked my number is correct!

p.s. I added these permissions to the manifest:

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

UPDATE: log details:

01-26 21:01:39.959: E/AndroidRuntime(6470): FATAL EXCEPTION: main 01-26 21:01:39.959: E/AndroidRuntime(6470): java.lang.IllegalArgumentException: Invalid message body 01-26 21:01:39.959: E/AndroidRuntime(6470): at android.telephony.SmsManager.sendTextMessage(SmsManager.java:82) 01-26 21:01:39.959: E/AndroidRuntime(6470): at com.saeedFri.groupsms.SendClickListeneer.sendSMS(SendClickListeneer.java:58) 01-26 21:01:39.959: E/AndroidRuntime(6470): at com.saeedFri.groupsms.SendClickListeneer.sendSMSes(SendClickListeneer.java:38) 01-26 21:01:39.959: E/AndroidRuntime(6470): at com.saeedFri.groupsms.SendClickListeneer.onClick(SendClickListeneer.java:32) 01-26 21:01:39.959: E/AndroidRuntime(6470): at android.view.View.performClick(View.java:4240) 01-26 21:01:39.959: E/AndroidRuntime(6470): at android.view.View$PerformClick.run(View.java:17721) 01-26 21:01:39.959: E/AndroidRuntime(6470): at android.os.Handler.handleCallback(Handler.java:730) 01-26 21:01:39.959: E/AndroidRuntime(6470): at android.os.Handler.dispatchMessage(Handler.java:92) 01-26 21:01:39.959: E/AndroidRuntime(6470): at android.os.Looper.loop(Looper.java:137) 01-26 21:01:39.959: E/AndroidRuntime(6470): at android.app.ActivityThread.main(ActivityThread.java:5103) 01-26 21:01:39.959: E/AndroidRuntime(6470): at java.lang.reflect.Method.invokeNative(Native Method) 01-26 21:01:39.959: E/AndroidRuntime(6470): at java.lang.reflect.Method.invoke(Method.java:525) 01-26 21:01:39.959: E/AndroidRuntime(6470): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 01-26 21:01:39.959: E/AndroidRuntime(6470): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 01-26 21:01:39.959: E/AndroidRuntime(6470): at dalvik.system.NativeStart.main(Native Method)

thanks in advance.

War es hilfreich?

Lösung

The sendTextMessage method will throw an IllegalArgumentException if the first or third parameter is empty.

As seen in the documentation...

public void sendTextMessage (String destinationAddress, String scAddress,
String text, PendingIntent sentIntent, PendingIntent deliveryIntent)

Throws
IllegalArgumentException    if destinationAddress or text are empty

Ensure that the the first and third parameters are not empty. Thatis most likely the problem.

In your question you have said you made sure your number variable is correct. In that case, your text variable must be empty. Post your declaration of it.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top