0

I am making a app in which i am accessing phone book and then select a contact to send a SMS to that particular recipient, but whenever i am trying to send a SMS getting error message which i have given in Catch block of my code for in case of failure:" "SMS failed, please try again later!""

Note: I have tried this on real device also, and getting same problem there...as well.

Contact.java::

  public interface Contact {

public int getId();

public String getName();

public List<RawContact> getRawContacts();

}

BirthdayEditor.java::

public class BirthdayEditor extends Activity implements OnItemClickListener {
private static final int CREATE_BIRTHDAY = 0;
private static final int EDIT_BIRTHDAY = 1;
private static final int DELETE_BIRTHDAY = 2;
private ImageButton saveGreeting;

public static final String CONTACT_ID = "contact_id";
private static final int NO_CONTACT_ID = Integer.MIN_VALUE;

private TextView name;
private ListView list;
private Database db;
private Contact contact;

ImageButton buttonSend;
EditText textSMS;

private Map<String, AuthenticatorDescription> map = new LinkedHashMap<String, AuthenticatorDescription>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.editor);

    // bind GUI components
    this.name = (TextView) findViewById(R.id.editor_name);
    this.list = (ListView) findViewById(R.id.editor_list);

    // check if contact id is valid
    this.db = new Database(getContentResolver());
    final int contactId = getIntent().getIntExtra(CONTACT_ID, NO_CONTACT_ID);
    this.contact = this.db.getContact(contactId);
    if (this.contact == null) {
        finish();
    }
    this.name.setText(this.contact.getName());

    // pre-load information about all account types
    AuthenticatorDescription[] authTypes = AccountManager.get(this).getAuthenticatorTypes();
    for (AuthenticatorDescription authDesc : authTypes) {
        this.map.put(authDesc.type, authDesc);
    }

    // bind list events
    this.list.setOnItemClickListener(this);
    this.list.setOnCreateContextMenuListener(this);

    // create the GUI
    updateView();               

    buttonSend = (ImageButton) findViewById(R.id.buttonSend);
    textSMS = (EditText) findViewById(R.id.editTextSMS);
    buttonSend.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

          String recepient = contact.getName().toString();
              Log.d(LOG_TAG, "onClick(" + recepient + ")");
          String sms = textSMS.getText().toString();

          try {
            SmsManager smsManager = SmsManager.getDefault();
            smsManager.sendTextMessage(recepient, 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();
          }

        }
    });
}

Logcat Says:

04-01 14:12:29.211: D/BirthdayEditor(950): ButtonSend onClick RECEPIENT Stephen
04-01 14:12:29.211: D/BirthdayEditor(950): ButtonSend onClick MESSAGE Congrats
04-01 14:12:29.251: W/System.err(950): java.lang.NullPointerException
04-01 14:12:29.261: W/System.err(950):  at android.os.Parcel.readException(Parcel.java:1431)
04-01 14:12:29.261: W/System.err(950):  at android.os.Parcel.readException(Parcel.java:1379)
04-01 14:12:29.261: W/System.err(950):  at com.android.internal.telephony.ISms$Stub$Proxy.sendText(ISms.java:434)
04-01 14:12:29.261: W/System.err(950):  at android.telephony.SmsManager.sendTextMessage(SmsManager.java:87)
04-01 14:12:29.261: W/System.err(950):  at com.chr.tatu.sample.friendslist.contacts.BirthdayEditor$2.onClick(BirthdayEditor.java:110)
04-01 14:12:29.261: W/System.err(950):  at android.view.View.performClick(View.java:4202)
04-01 14:12:29.272: W/System.err(950):  at android.view.View$PerformClick.run(View.java:17340)
04-01 14:12:29.272: W/System.err(950):  at android.os.Handler.handleCallback(Handler.java:725)
04-01 14:12:29.272: W/System.err(950):  at android.os.Handler.dispatchMessage(Handler.java:92)
04-01 14:12:29.272: W/System.err(950):  at android.os.Looper.loop(Looper.java:137)
04-01 14:12:29.272: W/System.err(950):  at android.app.ActivityThread.main(ActivityThread.java:5039)
04-01 14:12:29.272: W/System.err(950):  at java.lang.reflect.Method.invokeNative(Native Method)
04-01 14:12:29.272: W/System.err(950):  at java.lang.reflect.Method.invoke(Method.java:511)
04-01 14:12:29.272: W/System.err(950):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-01 14:12:29.272: W/System.err(950):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)

Manifest.xml:

I have given permission to Send SMS

  <uses-permission 
    android:name="android.permission.SEND_SMS" />
Jimmy Hill
  • 216
  • 2
  • 5
  • 15
  • have you add `SEND_SMS` permission in your `AndroidManifest.xml` file ? – Bishan Apr 01 '13 at 08:29
  • @Bishan yes i have given already, check my question now – Jimmy Hill Apr 01 '13 at 08:32
  • Error is occurred from Line 106 in BirthdayEditor.java class. what is the code in Line 106 ? – Bishan Apr 01 '13 at 08:36
  • You have set contact name as recipient by `String recepient = contact.getName().toString();` . but you have to set contact number as recipient. correct it and give a try. – Bishan Apr 01 '13 at 09:07
  • @Bishan yes i want to set contact number in place on name but don't know how can i do this??? – Jimmy Hill Apr 01 '13 at 09:11
  • as you get contact name by `contact.getName()`, isn't there any method to get contact number ? – Bishan Apr 01 '13 at 09:16
  • may be answers on [this question](http://stackoverflow.com/questions/13131613/call-contacts-choose-contact-to-send-sms-to-then-send-sms) helpful to you – Bishan Apr 01 '13 at 09:25
  • try type a hardcoded phone number and then check wether it work or not. I think u have problem in getting the phone number for phone contact – Vipin Sahu Apr 01 '13 at 09:31

1 Answers1

1

@JimmyHill

Still you are getting name of recepient, because you are using getName() method of your Contact.java Class below line:

    String recepient = contact.getName().toString();     

but to get number in place of name use getRawContacts() you just need to replace above line with below one, because by using this one you can get a Raw Contact from Contact List in your code, just check Contact.java once more:

    String recepient = contact.getRawContacts().toString();

I believe that it will work for you, if any problem occurs, let me know...

ASMUIRTI
  • 1,332
  • 10
  • 20