因此,在用户选择联系后,我将按照以下代码获取联系人数量:
public void openSmsWindowToContactNumber(Intent data, String smsBody) {
// Get the URI that points to the selected contact
Uri contactUri = data.getData();
// We only need the NUMBER column, because there will be only one row in the result
String[] projection = {Phone.NUMBER};
// Perform the query on the contact to get the NUMBER column
// We don't need a selection or sort order (there's only one result for the given URI)
// CAUTION: The query() method should be called from a separate thread to avoid blocking
// your app's UI thread. (For simplicity of the sample, this code doesn't do that.)
// Consider using CursorLoader to perform the query.
Cursor cursor = BaseActivity.this.getContentResolver()
.query(contactUri, projection, null, null, null);
cursor.moveToFirst();
int column = cursor.getColumnIndex(Phone.NUMBER);
String number = cursor.getString(column);
Intent smsIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:" + number));
smsIntent.putExtra("sms_body", smsBody);
startActivity(smsIntent);
}
在设备上运行时,联想A916(A916)操作系统:Android 4.4我收到此错误:
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=75502, result=-1, data=Intent { dat=content://com.android.contacts/data/240 flg=0x1 }} to activity {com.sm.proba/com.sm.proba.ClassPage}: java.lang.NullPointerException
at android.app.ActivityThread.deliverResults(ActivityThread.java:3553)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3596)
at android.app.ActivityThread.access$1300(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1369)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5292)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at
游标将为null,找不到联系人数据。我开始用以下方式选择联系意图:
Intent pickContactIntent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts"));
pickContactIntent.setType(Phone.CONTENT_TYPE);
startActivityForResult(pickContactIntent, REQUEST_CODE_PICK_CONTACTS);
有人知道如何解决这个问题吗?