-1

Am trying to send my location to my friend using Intent class. Everything works fine, but when I tried to send sms.sendTextMessage, it throws RunTime exception.

>java.lang.RuntimeException: Failure delivering result ResultInfo{who=null,  
 request=1, result=-1, data=Intent { 
   dat=content://com.android.contacts/contacts/lookup/2513i64a4c0af8e7588fa.1412i34  
 /34 flg=0x1 }} to activity  
{com.login.thinkpad.login/com.login.thinkpad.login.SecondActivity}:  
 java.lang.NullPointerException: Attempt to get length of null array

Could you help me how to send my location over sms service? Thanks in advance.This is my code.

public class SecondActivity extends AppCompatActivity {
    TextView youLoginIn;
    Button Map;
    private static Intent chooser;
     private final int PICK_CONTACT = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_activity);
        youLoginIn = (TextView)findViewById(R.id.youLogined);
        Intent intent = getIntent();
        intent.getStringExtra("SpeicalKey");
        Map = (Button)findViewById(R.id.Map);
     }

    public void mClicked(View view){

        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("google.navigation:q=a+street+address")); 
        chooser=Intent.createChooser(intent,"Launch Maps");

       //        if (intent.resolveActivity(getPackageManager()) != null) {
       //            startActivity(chooser);
       //        }

    }


    public void callContacts(View v) {

        Intent intent = new Intent(Intent.ACTION_PICK,    
     ContactsContract.Contacts.CONTENT_URI);
        startActivityForResult(intent, PICK_CONTACT);
    }

    @Override
    protected void onActivityResult(int reqCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(reqCode, resultCode, data);

        if(reqCode == PICK_CONTACT) {
            if(resultCode == ActionBarActivity.RESULT_OK) {
                Uri contactData = data.getData();
                Cursor c = getContentResolver().query(contactData, null, null, null, null);

                if(c.moveToFirst()) {
                    String name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
                    Toast.makeText(this, "You've picked:" + name, Toast.LENGTH_LONG).show();

                    //send your location to that name choosen.
                    SmsManager sms = SmsManager.getDefault();
                    sms.sendTextMessage(name,null, String.valueOf(chooser),null,null);  // Exception occur here. 
                }
            }
        }
    }

}
Steven_art
  • 153
  • 10

1 Answers1

0

sendTextMessage takes destination address, you supplied the name!

http://developer.android.com/reference/android/telephony/gsm/SmsManager.html

See How to get contacts' phone number in Android

Community
  • 1
  • 1
kelalaka
  • 5,064
  • 5
  • 27
  • 44
  • This is not what am looking for. – Steven_art Jan 20 '16 at 23:10
  • Public final void sendDataMessage (String destinationAddress, String scAddress, short destinationPort, byte[] data, PendingIntent sentIntent, PendingIntent deliveryIntent) destinationAddress the address to send the message to it is phone number of the person not the name you get by ContactsContract.Contacts.DISPLAY_NAME – kelalaka Jan 23 '16 at 14:16
  • yes that is true. I just solved after while. Thanks. – Steven_art Aug 20 '16 at 08:40