使用短信服务

时间:2016-01-20 18:45:16

标签: android android-intent android-service

我试图使用Intent类将我的位置发送给我的朋友。一切正常,但当我尝试发送 sms.sendTextMessage 时,它会抛出RunTime异常。

>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

你能帮助我如何通过短信服务发送我的位置吗?提前谢谢。这是我的代码。

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. 
                }
            }
        }
    }

}

1 个答案:

答案 0 :(得分:0)