我正在尝试从Android中手机的本机联系人中检索联系人的姓名和电话号码。它得到了我的名字,但是在尝试获取该号码时崩溃。
这是我的代码:
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Contacts;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.TextView;
import static android.provider.ContactsContract.*;
public class MainActivity extends AppCompatActivity {
private Button btnPick;
private TextView txtName;
private TextView txtNum;
final int PICK_CONTACT_REQCODE = 1243;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(Integer.valueOf(requestCode).compareTo(Integer.valueOf(PICK_CONTACT_REQCODE)) == 0){
if(resultCode == RESULT_OK){
System.out.println("ChkNow returned Data is: "+data);
Uri cData = data.getData();
System.out.println("ChkNow contact Data is: "+cData);
Cursor cursor = managedQuery(cData,null,null,null,null);
System.out.println("ChkNow cursor Data is: "+cursor);
if(cursor.moveToFirst()){
String contactName = cursor.getString(cursor.getColumnIndex(CommonDataKinds.Phone.DISPLAY_NAME));
System.out.println("ChkNow contactName Data is: "+contactName);
String contactNum = cursor.getString(cursor.getColumnIndex(CommonDataKinds.Phone.NUMBER));
System.out.println("ChkNow contactNum Data is: "+contactNum);
txtName.setText(contactName);
txtNum.setText(contactNum);
}
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnPick = (Button) findViewById(R.id.btnPick);
txtName = (TextView)findViewById(R.id.txtName);
txtNum = (TextView)findViewById(R.id.txtNum);
btnPick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent pickContIntent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
startActivityForResult(pickContIntent, PICK_CONTACT_REQCODE);
}
});
}
}
它可以很好地打印名称并将其设置为文本,但是在尝试获取该号码时失败。
以下是崩溃日志:
08-06 22:28:00.660: E/ProActivieLaunch(5163): Sending Broadcast
08-06 22:28:00.665: E/ActivityManager(3796): TouchDown intent received, starting ActiveLaunch
08-06 22:28:00.694: E/Zygote(28580): isWhitelistProcess - Process is Whitelisted
08-06 22:28:00.695: E/libpersona(28580): scanKnoxPersonas
08-06 22:28:00.695: E/libpersona(28580): Couldn't open the File - /data/system/users/0/personalist.xml - No such file or directory
08-06 22:28:00.750: E/adbd(6256): recv: OPEN 0000010a 00000000 000b:6A 64 77 70 3A 32 38 35 38 30 00
08-06 22:28:00.750: E/adbd(6256): service_to_fd: jdwp:28580
08-06 22:28:00.750: E/adbd(6256): recv: OPEN 00000109 00000000 000b:6A 64 77 70 3A 32 38 35 38 30 00
08-06 22:28:00.750: E/adbd(6256): service_to_fd: jdwp:28580
08-06 22:28:01.297: E/Watchdog(3796): !@Sync 43 [2018-08-06 22:28:01.297]
08-06 22:28:02.396: E/ContactsProvider_EventLog(5030): Flush buffer to file cnt : 1 size : 0Kb duration : 1ms lastUpdatedAfter : 15908 ms mFlush_time_threasold : 2000 mCurrentSize : 101
08-06 22:28:02.802: E/CarrierMatchingUtils(8043): isEnableCarrierMatching: carrier_matching_status
08-06 22:28:02.821: E/E/StickerOrderManager(8043): there is no preload application request to install.
08-06 22:28:02.834: E/E/StickerOrderManager(8043): there is no preload application request to install.
08-06 22:28:02.838: E/CscParser(8043): fileName + /system/omc/INS/customer.xml
08-06 22:28:02.849: E/CscParser(8043): fileName + /system/omc/INS/customer.xml
08-06 22:28:02.863: E/CscParser(8043): fileName + /system/omc/INS/customer.xml
08-06 22:28:02.878: E/CscParser(8043): fileName + /system/omc/INS/customer.xml
08-06 22:28:02.889: E/CscParser(8043): fileName + /system/omc/INS/customer.xml
08-06 22:28:02.905: E/CscParser(8043): fileName + /system/omc/INS/customer.xml
08-06 22:28:02.921: E/CscParser(8043): fileName + /system/omc/INS/customer.xml
08-06 22:28:02.936: E/CscParser(8043): fileName + /system/omc/INS/customer.xml
08-06 22:28:02.956: E/CscParser(8043): fileName + /system/omc/INS/customer.xml
08-06 22:28:02.969: E/CscParser(8043): fileName + /system/omc/INS/customer.xml
08-06 22:28:02.987: E/CscParser(8043): fileName + /system/omc/INS/customer.xml
08-06 22:28:03.005: E/CscParser(8043): fileName + /system/omc/INS/customer.xml
08-06 22:28:03.018: E/CscParser(8043): fileName + /system/omc/INS/customer.xml
08-06 22:28:03.033: E/CscParser(8043): fileName + /system/omc/INS/customer.xml
08-06 22:28:03.049: E/CscParser(8043): fileName + /system/omc/INS/customer.xml
08-06 22:28:03.063: E/CscParser(8043): fileName + /system/omc/INS/customer.xml
08-06 22:28:03.731: E/CursorWindow(28580): Failed to read row 0, column -1 from a CursorWindow which has 1 rows, 46 columns.
08-06 22:28:03.735: E/AndroidRuntime(28580): FATAL EXCEPTION: main
08-06 22:28:03.735: E/AndroidRuntime(28580): Process: com.example.kit1007.contactsreader, PID: 28580
08-06 22:28:03.735: E/AndroidRuntime(28580): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1243, result=-1, data=Intent { dat=content://com.android.contacts/contacts/lookup/3414i1f153ea6890d4f95/13 flg=0x1 (has extras) }} to activity {com.example.kit1007.contactsreader/com.example.kit1007.contactsreader.MainActivity}: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
08-06 22:28:03.735: E/AndroidRuntime(28580): at android.app.ActivityThread.deliverResults(ActivityThread.java:4489)
08-06 22:28:03.735: E/AndroidRuntime(28580): at android.app.ActivityThread.handleSendResult(ActivityThread.java:4532)
08-06 22:28:03.735: E/AndroidRuntime(28580): at android.app.ActivityThread.-wrap20(Unknown Source:0)
08-06 22:28:03.735: E/AndroidRuntime(28580): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1752)
08-06 22:28:03.735: E/AndroidRuntime(28580): at android.os.Handler.dispatchMessage(Handler.java:105)
08-06 22:28:03.735: E/AndroidRuntime(28580): at android.os.Looper.loop(Looper.java:164)
08-06 22:28:03.735: E/AndroidRuntime(28580): at android.app.ActivityThread.main(ActivityThread.java:6938)
08-06 22:28:03.735: E/AndroidRuntime(28580): at java.lang.reflect.Method.invoke(Native Method)
08-06 22:28:03.735: E/AndroidRuntime(28580): at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
08-06 22:28:03.735: E/AndroidRuntime(28580): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
08-06 22:28:03.735: E/AndroidRuntime(28580): Caused by: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
08-06 22:28:03.735: E/AndroidRuntime(28580): at android.database.CursorWindow.nativeGetString(Native Method)
08-06 22:28:03.735: E/AndroidRuntime(28580): at android.database.CursorWindow.getString(CursorWindow.java:451)
08-06 22:28:03.735: E/AndroidRuntime(28580): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
08-06 22:28:03.735: E/AndroidRuntime(28580): at android.database.CursorWrapper.getString(CursorWrapper.java:137)
08-06 22:28:03.735: E/AndroidRuntime(28580): at com.example.kit1007.contactsreader.MainActivity.onActivityResult(MainActivity.java:37)
08-06 22:28:03.735: E/AndroidRuntime(28580): at android.app.Activity.dispatchActivityResult(Activity.java:7539)
08-06 22:28:03.735: E/AndroidRuntime(28580): at android.app.ActivityThread.deliverResults(ActivityThread.java:4485)
08-06 22:28:03.735: E/AndroidRuntime(28580): ... 9 more
08-06 22:28:03.767: E/PhoneWindow_APM :(3796): sendNotificationForAppPermissionMonitor callingPid = 3796, uid = 1000
08-06 22:28:03.767: E/PhoneWindow_APM :(3796): sendNotificationForAppPermissionMonitor 2
08-06 22:28:03.767: E/PhoneWindow_APM :(3796): sendNotificationForAppPermissionMonitor 21000_3796
08-06 22:28:03.767: E/PhoneWindow_APM :(3796): isCalledPackage return true
08-06 22:28:04.271: E/PBSessionCacheImpl(5328): sessionId[23434325384557348] not persisted.
08-06 22:28:04.769: E/ViewRootImpl(5536): sendUserActionEvent() returned.
08-06 22:28:05.076: E/ViewRootImpl(3796): sendUserActionEvent() returned.
08-06 22:28:05.809: E/Zygote(28632): isWhitelistProcess - Process is Whitelisted
08-06 22:28:05.811: E/libpersona(28632): scanKnoxPersonas
08-06 22:28:05.811: E/libpersona(28632): Couldn't open the File - /data/system/users/0/personalist.xml - No such file or directory
08-06 22:28:06.424: E/ContactsProvider_EventLog(5030): Flush buffer to file cnt : 2 size : 3Kb duration : 7ms lastUpdatedAfter : 4028 ms mFlush_time_threasold : 2000 mCurrentSize : 1901
哪里出错了?