现在我启动联系人选择器来设置editTexts的文本。如果我使用ContactsContract.Contacts.DISPLAY_NAME
一切正常,我可以将其转换为String并将editText的文本设置为。但是当我尝试String phoneNumber = c.getString(c.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
应用程序崩溃时。
这是我的完整代码:
addFromContacts.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,
Contacts.CONTENT_URI);
startActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT);
}
});
}
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case (CONTACT_PICKER_RESULT) :
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
if (c.moveToFirst()) {
String name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
//String phoneNumber = c.getString(c.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
EditText lastName = (EditText)findViewById(R.id.last_name);
//EditText phoneNumberEditText = (EditText)findViewById(R.id.phonenumber);
//phoneNumberEditText.setText(phoneNumber);
lastName.setText(name);
// TODO Whatever you want to do with the selected contact name.
}
}
break;
}
}
注释掉的东西是不会起作用的代码。我在这里缺少什么?
我在清单中设置了权限。当我在String phoneNumber = c.getString(c.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
上调试并设置断点时,我在日志中得到了这个:
04-21 16:49:15.755: DEBUG/dalvikvm(2973): GC_EXPLICIT freed 1263 objects / 72136 bytes in 71ms
04-21 16:49:16.029: WARN/jdwp(4244): Debugger is telling the VM to exit with code=1
04-21 16:49:16.029: INFO/dalvikvm(4244): GC lifetime allocation: 2447 bytes
04-21 16:49:16.060: INFO/ActivityManager(1096): Process com.norbelle.cearch.ui (pid 4244) has died.
04-21 16:49:16.076: DEBUG/Zygote(1006): Process 4244 exited cleanly (1)
04-21 16:49:16.076: INFO/WindowManager(1096): WIN DEATH: Window{44a1a620 com.norbelle.cearch.ui/com.norbelle.cearch.ui.ContactsActivity paused=false}
04-21 16:49:16.083: INFO/WindowManager(1096): WIN DEATH: Window{44a61940 com.norbelle.cearch.ui/com.norbelle.cearch.ui.AddContactActivity paused=false}
04-21 16:49:16.107: INFO/ActivityManager(1096): Start proc com.norbelle.cearch.ui for activity com.norbelle.cearch.ui/.ContactsActivity: pid=4260 uid=10085 gids={3003}
04-21 16:49:16.177: INFO/UsageStats(1096): Unexpected resume of com.norbelle.cearch.ui while already resumed in com.norbelle.cearch.ui
04-21 16:49:16.443: INFO/ActivityManager(1096): Displayed activity com.norbelle.cearch.ui/.ContactsActivity: 348 ms (total 348 ms)
04-21 16:49:16.568: INFO/Gmail(1410): getStartSyncRequest: handledServerOpId: 2314649, upperFetchedConvoId: 1354702287953461248, lowerFetchedConvoId: 0, ackedClientOp: 71
04-21 16:49:17.162: DEBUG/Gmail(1410): handleLabelsValues: receiving set of 24 labels
04-21 16:49:17.224: INFO/Gmail(1410): checkLabelsSets changed the label sets to: included([^^out, ^r]), partial([^i, ^f, ^iim]), all([^i, ^g, ^f, ^vm, sent-items, ^k, ^iim, textron, ^all, ^io_im, ^sps, ^act, ^ig, ^b, [gmail]trash, ^p, ^o, notes, ^t, ^u, ^punsub, ^r, ^unsub, ^s])
04-21 16:49:17.310: DEBUG/dalvikvm(1410): GC_EXPLICIT freed 4017 objects / 166344 bytes in 57ms
04-21 16:49:17.318: INFO/Gmail(1410): MainSyncRequestProto: lowestBkwdConvoId: 0, highestHandledServerOp: 2314649, normalSync: true
04-21 16:49:17.380: INFO/Gmail(1410): Sending notification intent: Intent { act=android.intent.action.PROVIDER_CHANGED dat=content://gmail-ls/unread/^i (has extras) }
04-21 16:49:17.419: DEBUG/Gmail(1410): New email:mtpowers@gmail.com (42) mapSize:1 getAttention:true
04-21 16:49:17.419: DEBUG/Gmail(1410): Validating Notification:mtpowers@gmail.com (42) mapSize:1 tagLabel:^^unseen-^i getAttention:true
04-21 16:49:17.427: INFO/Gmail(1410): New email in mtpowers@gmail.com vibrateWhen:never, playing notification: content://settings/system/notification_sound
04-21 16:49:26.560: INFO/Gmail(1410): MainSyncRequestProto: lowestBkwdConvoId: 0, highestHandledServerOp: 2314649, normalSync: true
04-21 16:49:27.224: DEBUG/dalvikvm(1410): GC_EXPLICIT freed 5526 objects / 215128 bytes in 51ms
04-21 16:49:27.232: INFO/Gmail(1410): MainSyncRequestProto: lowestBkwdConvoId: 0, highestHandledServerOp: 2314706, normalSync: true
04-21 16:49:27.849: DEBUG/LocationMasfClient(1096): getNetworkLocation(): Returning cache location with accuracy 519.0
04-21 16:49:27.857: DEBUG/libgps(1096): GpsInterface_inject_location( 40.738950, -73.983473, 519.000 )
04-21 16:49:28.193: DEBUG/Gmail(1410): Inserting message 1366728204445131439. synced=true
04-21 16:49:28.209: DEBUG/Gmail(1410): Inserting message 1366728461587089057. synced=true
04-21 16:49:28.232: DEBUG/Gmail(1410): Inserting message 1366729438375781270. synced=true
04-21 16:49:28.248: DEBUG/Gmail(1410): Inserting message 1366729533671416361. synced=true
04-21 16:49:28.279: DEBUG/Gmail(1410): Inserting message 1366729635016437420. synced=true
04-21 16:49:28.302: DEBUG/Gmail(1410): Inserting message 1366729668021783921. synced=true
04-21 16:49:28.318: DEBUG/Gmail(1410): Inserting message 1366729722547125832. synced=true
04-21 16:49:28.341: DEBUG/Gmail(1410): Inserting message 1366729878768667123. synced=true
04-21 16:49:28.357: DEBUG/Gmail(1410): Inserting message 1366730036698027597. synced=true
04-21 16:49:28.419: DEBUG/dalvikvm(1410): GC_FOR_MALLOC freed 6073 objects / 514592 bytes in 48ms
04-21 16:49:28.427: DEBUG/Gmail(1410): Inserting message 1366730066831555384. synced=true
04-21 16:49:28.459: DEBUG/Gmail(1410): Inserting message 1366730097953170841. synced=true
04-21 16:49:28.474: DEBUG/Gmail(1410): Inserting message 1366730118220740527. synced=true
04-21 16:49:28.498: DEBUG/Gmail(1410): Inserting message 1366730216522177137. synced=true
04-21 16:49:28.513: DEBUG/Gmail(1410): Inserting message 1366733822002629861. synced=true
04-21 16:49:28.685: DEBUG/Gmail(1410): Inserting message 1366637621699659172. synced=true
04-21 16:49:28.709: DEBUG/Gmail(1410): Inserting message 1366643314643548418. synced=true
04-21 16:49:28.724: DEBUG/Gmail(1410): Inserting message 1366662749444780736. synced=true
04-21 16:49:28.740: DEBUG/Gmail(1410): Inserting message 1366662749523770774. synced=true
04-21 16:49:28.755: DEBUG/Gmail(1410): Inserting message 1366697462094134996. synced=true
04-21 16:49:28.818: DEBUG/dalvikvm(1410): GC_FOR_MALLOC freed 6038 objects / 516400 bytes in 50ms
04-21 16:49:28.826: DEBUG/Gmail(1410): Inserting message 1366729388290241830. synced=true
04-21 16:49:28.841: DEBUG/Gmail(1410): Inserting message 1366733808257099161. synced=true
04-21 16:49:29.068: DEBUG/dalvikvm(1410): GC_EXPLICIT freed 2349 objects / 134056 bytes in 50ms
04-21 16:49:29.076: INFO/Gmail(1410): MainSyncRequestProto: lowestBkwdConvoId: 0, highestHandledServerOp: 2314707, normalSync: true
04-21 16:49:29.146: INFO/Gmail(1410): Sending notification intent: Intent { act=android.intent.action.PROVIDER_CHANGED dat=content://gmail-ls/unread/^i (has extras) }
04-21 16:49:29.201: DEBUG/Gmail(1410): New email:mtpowers@gmail.com (43) mapSize:1 getAttention:true
04-21 16:49:29.201: DEBUG/Gmail(1410): Validating Notification:mtpowers@gmail.com (43) mapSize:1 tagLabel:^^unseen-^i getAttention:true
04-21 16:49:29.201: INFO/Gmail(1410): New email in mtpowers@gmail.com vibrateWhen:never, playing notification: content://settings/system/notification_sound
04-21 16:49:30.240: DEBUG/AndroidRuntime(4276): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
04-21 16:49:30.248: DEBUG/AndroidRuntime(4276): CheckJNI is OFF
04-21 16:49:30.248: DEBUG/dalvikvm(4276): creating instr width table
04-21 16:49:30.302: DEBUG/AndroidRuntime(4276): --- registering native functions ---
04-21 16:49:30.599: DEBUG/AndroidRuntime(4276): Shutting down VM
04-21 16:49:30.599: DEBUG/dalvikvm(4276): Debugger has detached; object registry had 1 entries
04-21 16:49:30.607: INFO/AndroidRuntime(4276): NOTE: attach of thread 'Binder Thread #3' failed
04-21 16:49:30.794: DEBUG/AndroidRuntime(4285): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
04-21 16:49:30.794: DEBUG/AndroidRuntime(4285): CheckJNI is OFF
04-21 16:49:30.794: DEBUG/dalvikvm(4285): creating instr width table
04-21 16:49:30.857: DEBUG/AndroidRuntime(4285): --- registering native functions ---
04-21 16:49:31.154: INFO/ActivityManager(1096): Force stopping package com.norbelle.cearch.ui uid=10085
04-21 16:49:31.169: INFO/Process(1096): Sending signal. PID: 4260 SIG: 9
04-21 16:49:31.193: INFO/UsageStats(1096): Unexpected resume of com.android.launcher while already resumed in com.norbelle.cearch.ui
04-21 16:49:31.193: INFO/ActivityManager(1096): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.norbelle.cearch.ui/.ContactsActivity }
04-21 16:49:31.193: INFO/WindowManager(1096): WIN DEATH: Window{44aab410 com.norbelle.cearch.ui/com.norbelle.cearch.ui.ContactsActivity paused=false}
04-21 16:49:31.232: DEBUG/AndroidRuntime(4285): Shutting down VM
04-21 16:49:31.232: DEBUG/jdwp(4285): Got wake-up signal, bailing out of select
04-21 16:49:31.232: DEBUG/dalvikvm(4285): Debugger has detached; object registry had 1 entries
04-21 16:49:31.255: INFO/AndroidRuntime(4285): NOTE: attach of thread 'Binder Thread #3' failed
04-21 16:49:31.365: INFO/ActivityManager(1096): Start proc com.norbelle.cearch.ui for activity com.norbelle.cearch.ui/.ContactsActivity: pid=4293 uid=10085 gids={3003}
04-21 16:49:31.373: WARN/InputManagerService(1096): Got RemoteException sending setActive(false) notification to pid 4260 uid 10085
04-21 16:49:31.529: WARN/ActivityThread(4293): Application com.norbelle.cearch.ui is waiting for the debugger on port 8100...
04-21 16:49:31.544: INFO/System.out(4293): Sending WAIT chunk
04-21 16:49:31.552: INFO/dalvikvm(4293): Debugger is active
04-21 16:49:31.748: INFO/System.out(4293): Debugger has connected
04-21 16:49:31.748: INFO/System.out(4293): waiting for debugger to settle...
04-21 16:49:31.951: INFO/System.out(4293): waiting for debugger to settle...
04-21 16:49:32.146: INFO/System.out(4293): waiting for debugger to settle...
04-21 16:49:32.349: INFO/System.out(4293): waiting for debugger to settle...
04-21 16:49:32.552: INFO/System.out(4293): waiting for debugger to settle...
04-21 16:49:32.756: INFO/System.out(4293): waiting for debugger to settle...
04-21 16:49:32.951: INFO/System.out(4293): waiting for debugger to settle...
04-21 16:49:33.154: INFO/System.out(4293): debugger has settled (1399)
04-21 16:49:33.927: INFO/ActivityManager(1096): Displayed activity com.norbelle.cearch.ui/.ContactsActivity: 2570 ms (total 2570 ms)
04-21 16:49:34.919: WARN/KeyCharacterMap(4293): Can't open keycharmap file
04-21 16:49:34.919: WARN/KeyCharacterMap(4293): Error loading keycharmap file '/system/usr/keychars/qtouch-touchscreen.kcm.bin'. hw.keyboards.65538.devname='qtouch-touchscreen'
04-21 16:49:34.919: WARN/KeyCharacterMap(4293): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
04-21 16:49:35.997: INFO/ActivityManager(1096): Starting activity: Intent { cmp=com.norbelle.cearch.ui/.AddContactActivity }
04-21 16:49:37.076: INFO/ActivityManager(1096): Displayed activity com.norbelle.cearch.ui/.AddContactActivity: 974 ms (total 974 ms)
04-21 16:49:37.943: INFO/ActivityManager(1096): Starting activity: Intent { act=android.intent.action.PICK dat=content://com.android.contacts/contacts cmp=com.android.contacts/.ContactsListActivity }
04-21 16:49:38.044: INFO/ContactsListActivity(3417): Called with action: android.intent.action.PICK
04-21 16:49:38.318: INFO/ActivityManager(1096): Displayed activity com.android.contacts/.ContactsListActivity: 292 ms (total 292 ms)
04-21 16:49:38.716: WARN/IInputConnectionWrapper(4293): showStatusIcon on inactive InputConnection
04-21 16:49:39.177: DEBUG/dalvikvm(3417): GC_EXTERNAL_ALLOC freed 1448 objects / 84752 bytes in 63ms
04-21 16:49:43.716: WARN/System.err(4100): java.net.SocketException: Connection reset by peer
04-21 16:49:43.716: WARN/System.err(4100): at org.apache.harmony.luni.platform.OSNetworkSystem.readSocketImpl(Native Method)
04-21 16:49:43.716: WARN/System.err(4100): at org.apache.harmony.luni.platform.OSNetworkSystem.read(OSNetworkSystem.java:358)
04-21 16:49:43.724: WARN/System.err(4100): at org.apache.harmony.luni.net.PlainSocketImpl.read(PlainSocketImpl.java:561)
04-21 16:49:43.724: WARN/System.err(4100): at org.apache.harmony.luni.net.SocketInputStream.read(SocketInputStream.java:88)
04-21 16:49:43.724: WARN/System.err(4100): at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:103)
04-21 16:49:43.724: WARN/System.err(4100): at org.apache.http.impl.io.AbstractSessionInputBuffer.readLine(AbstractSessionInputBuffer.java:191)
04-21 16:49:43.724: WARN/System.err(4100): at org.apache.http.impl.conn.DefaultResponseParser.parseHead(DefaultResponseParser.java:82)
04-21 16:49:43.732: WARN/System.err(4100): at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:174)
04-21 16:49:43.732: WARN/System.err(4100): at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:179)
04-21 16:49:43.732: WARN/System.err(4100): at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:235)
04-21 16:49:43.732: WARN/System.err(4100): at org.apache.http.impl.conn.AbstractClientConnAdapter.receiveResponseHeader(AbstractClientConnAdapter.java:259)
04-21 16:49:43.732: WARN/System.err(4100): at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:279)
04-21 16:49:43.740: WARN/System.err(4100): at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:121)
04-21 16:49:43.740: WARN/System.err(4100): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:410)
04-21 16:49:43.740: WARN/System.err(4100): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
04-21 16:49:43.748: WARN/System.err(4100): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
04-21 16:49:43.748: WARN/System.err(4100): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
04-21 16:49:43.748: WARN/System.err(4100): at android.app.linked.LinkedMessaging.HTTPRegister(LinkedMessaging.java:571)
04-21 16:49:43.748: WARN/System.err(4100): at android.app.linked.LinkedMessaging$14.run(LinkedMessaging.java:524)
04-21 16:49:43.748: WARN/MQTT(4100): Exception:java.net.SocketException: Connection reset by peer
04-21 16:49:43.748: WARN/LinkedMessaging(4100): HTTP Register: Null response
04-21 16:49:43.755: WARN/LinkedMessaging(4100): Reg Handler Msg: -1
04-21 16:49:46.834: WARN/LinkedService(4100): Debug Service Timer
04-21 16:49:51.018: WARN/ActivityManager(1096): Launch timeout has expired, giving up wake lock!
04-21 16:49:51.029: WARN/ActivityManager(1096): Activity idle timeout for HistoryRecord{44a90108 com.norbelle.cearch.ui/.AddContactActivity}
04-21 16:50:06.951: DEBUG/dalvikvm(2982): GC_EXPLICIT freed 88 objects / 4264 bytes in 65ms
04-21 16:50:12.037: DEBUG/dalvikvm(2989): GC_EXPLICIT freed 682 objects / 61392 bytes in 45ms
04-21 16:50:13.757: WARN/LinkedMessaging(4100): Registering Client: 0
04-21 16:50:13.757: WARN/MQTT(4100): In Register HTTP Client
04-21 16:50:13.757: WARN/MQTT(4100): In GetHttp:http://gps.deviollc.com/authenticate?num=12039695185&device=android&os=1.6
04-21 16:50:13.757: WARN/MQTT(4100): Executing Http Get
04-21 16:50:15.802: DEBUG/AlarmManagerService(1096): Kernel timezone updated to 240 minutes west of GMT
04-21 16:50:15.810: DEBUG/SystemClock(1175): Setting time of day to sec=1303419015
04-21 16:50:15.235: DEBUG/dalvikvm(1096): GC_EXTERNAL_ALLOC freed 16206 objects / 813128 bytes in 116ms
04-21 16:50:15.313: INFO/ActivityManager(1096): Start proc com.android.alarmclock for broadcast com.android.alarmclock/com.android.deskclock.AlarmInitReceiver: pid=4306 uid=10040 gids={}
04-21 16:50:15.391: INFO/ActivityThread(4306): Publishing provider com.android.deskclock: com.android.deskclock.AlarmProvider
04-21 16:50:16.336: DEBUG/dalvikvm(4100): GC_FOR_MALLOC freed 852 objects / 239448 bytes in 87ms