Saturday, April 12, 2014

Get a contact number returns an exception


posts by cailingyun0129 on 2013-05-10 17:15:37 Edit
The code below to get the contact's name is no problem, but when you get a contact number abnormalities launch anomalies are 05-10 08:32:38.327: W / dalvikvm (446): threadid = 1: thread exiting with uncaught exception ( group = 0x40015560)
05-10 08:32:38.327: E / AndroidRuntime (446): FATAL EXCEPTION: main
05-10 08:32:38.327: E / AndroidRuntime (446): java.lang.RuntimeException: Failure delivering result ResultInfo {who = null, request = 2, result = -1, data = Intent {dat = content :/ / com.android.contacts/contacts/lookup/0r1-D394DF18CEDF4081/1 flg = 0x1 (has extras)}} to activity {com.example.sendsms / com.example.sendsms.MainActivity}: java.lang.IllegalArgumentException: column ' data1 'does not exist
05-10 08:32:38.327: E / AndroidRuntime (446): at android.app.ActivityThread.deliverResults (ActivityThread.java: 2532)
05-10 08:32:38.327: E / AndroidRuntime (446): at android.app.ActivityThread.handleSendResult (ActivityThread.java: 2574)
05-10 08:32:38.327: E / AndroidRuntime (446): at android.app.ActivityThread.access $ 2000 (ActivityThread.java: 117)
05-10 08:32:38.327: E / AndroidRuntime (446): at android.app.ActivityThread $ H.handleMessage (ActivityThread.java: 961)
05-10 08:32:38.327: E / AndroidRuntime (446): at android.os.Handler.dispatchMessage (Handler.java: 99)
05-10 08:32:38.327: E / AndroidRuntime (446): at android.os.Looper.loop (Looper.java: 123)
05-10 08:32:38.327: E / AndroidRuntime (446): at android.app.ActivityThread.main (ActivityThread.java: 3683)
05-10 08:32:38.327: E / AndroidRuntime (446): at java.lang.reflect.Method.invokeNative (Native Method)
05-10 08:32:38.327: E / AndroidRuntime (446): at java.lang.reflect.Method.invoke (Method.java: 507)
05-10 08:32:38.327: E / AndroidRuntime (446): at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java: 839)
05-10 08:32:38.327: E / AndroidRuntime (446): at com.android.internal.os.ZygoteInit.main (ZygoteInit.java: 597)
05-10 08:32:38.327: E / AndroidRuntime (446): at dalvik.system.NativeStart.main (Native Method)
05-10 08:32:38.327: E / AndroidRuntime (446): Caused by: java.lang.IllegalArgumentException: column 'data1' does not exist
05-10 08:32:38.327: E / AndroidRuntime (446): at android.database.AbstractCursor.getColumnIndexOrThrow (AbstractCursor.java: 314)
05-10 08:32:38.327: E / AndroidRuntime (446): at android.database.CursorWrapper.getColumnIndexOrThrow (CursorWrapper.java: 99)
05-10 08:32:38.327: E / AndroidRuntime (446): at com.example.sendsms.MainActivity.onActivityResult (MainActivity.java: 54)
05-10 08:32:38.327: E / AndroidRuntime (446): at android.app.Activity.dispatchActivityResult (Activity.java: 3908)
05-10 08:32:38.327: E / AndroidRuntime (446): at android.app.ActivityThread.deliverResults (ActivityThread.java: 2528)
05-10 08:32:38.327: E / AndroidRuntime (446): ... 11 more
Code is as follows:
 package com.example.sendsms; 

import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.database.Cursor;
import android.telephony.SmsManager;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
private EditText smsBodyEditText;
private Button sendButton;
private String messageString;
private static final int PICK_CONTACT_SUBACTIVITY = 2;
@ Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
smsBodyEditText = (EditText) findViewById (R.id.smsBody);
sendButton = (Button) findViewById (R.id.send);
sendButton.setOnClickListener (new OnClickListener () {

@ Override
public void onClick (View v) {
/ / TODO Auto-generated method stub
/ / Uri uri = Uri.parse ("content :/ / com.android.contacts / contacts");
Uri uri = ContactsContract.Contacts.CONTENT_URI;
Intent intent = new Intent (Intent.ACTION_PICK, uri);
/ / Intent.setType (Phone.CONTENT_TYPE);
startActivityForResult (intent, PICK_CONTACT_SUBACTIVITY);
}
});
}

@ Override
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
/ / TODO Auto-generated method stub
switch (requestCode) {
case PICK_CONTACT_SUBACTIVITY:
Uri resultUri = data.getData ();
if (resultUri! = null)
{
try {
Cursor cursor = managedQuery (resultUri, null, null, null, null);
cursor.moveToFirst ();
String strName = cursor.getString (cursor.getColumnIndexOrThrow (Phone.DISPLAY_NAME));
String strNum = cursor.getString (cursor.getColumnIndexOrThrow (Phone.NUMBER));
String strDestAddress = strNum;
messageString = smsBodyEditText.getText () toString ();.
SmsManager smsManager = SmsManager.getDefault ();
PendingIntent mIntent = PendingIntent.getBroadcast (MainActivity.this, 0, new Intent (), 0);
smsManager.sendTextMessage (strDestAddress, null, messageString, mIntent, null);
Toast.makeText (this, "information has been sent to:" + strName, Toast.LENGTH_SHORT). Show ();
}
catch (Exception e) {
smsBodyEditText.setText (e.toString ());
e.printStackTrace ();
}
}
break;

default:
break;
}
super.onActivityResult (requestCode, resultCode, data);
}
<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
Do not have permission, you should join READ_CONTACTS Permissions
Reply:
cited a floor studiovc reply:
do not have permission, you should join READ_CONTACTS permissions
Brother, I'm sure added permissions, and read the name of the contact is not a problem, but later found together intent.setType (Phone.CONTENT_TYPE); phrase on it, I wonder why?
Reply:
uri replaced Phone.CONTENT_URI

No comments:

Post a Comment