简体   繁体   English

在Android开发中读取通讯录时出现错误

[英]Getting error while reading contacts in android development

I'm new to android development. 我是android开发的新手。 I'm developing in eclipse and java. 我正在用Eclipse和Java开发。 I'm trying to read contacts and do some string manupulation like substring() or contains (). 我正在尝试读取联系人并进行一些字符串操作,例如substring()或contains()。 i did the following steps, 我做了以下步骤,

  1. Permission added ( ) 添加了权限()
  2. Code to read contacts : 读取联系人的代码:

     Cursor people=getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); while(people.moveToNext()) { int id=people.getColumnIndex(PhoneLookup.DISPLAY_NAME); String name= people.getString(id); if(name!="") { edittext1.setText(name.substring(2,3)); } } 

Here i'm getting error because of substring. 在这里我由于子字符串而出错。 I'm getting error if i use any string class methods. 如果我使用任何字符串类方法,都会出错。 But if assign some hardcoded value in name,then i'm not getting any error. 但是,如果在名称中分配一些硬编码的值,那么我不会收到任何错误。 ie. 即。

       String name= "John";  // people.getString(id);

Error messages : 错误讯息:

08-19 22:13:03.467: E/AndroidRuntime(4265): FATAL EXCEPTION: main
08-19 22:13:03.467: E/AndroidRuntime(4265): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1111, result=-1, data=Intent { (has extras) }} to activity {com.ravi.vrs/com.ravi.vrs.Voice}: java.lang.NullPointerException
08-19 22:13:03.467: E/AndroidRuntime(4265):     at android.app.ActivityThread.deliverResults(ActivityThread.java:2553)
08-19 22:13:03.467: E/AndroidRuntime(4265):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:2595)
08-19 22:13:03.467: E/AndroidRuntime(4265):     at android.app.ActivityThread.access$2000(ActivityThread.java:121)
08-19 22:13:03.467: E/AndroidRuntime(4265):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:973)
08-19 22:13:03.467: E/AndroidRuntime(4265):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-19 22:13:03.467: E/AndroidRuntime(4265):     at android.os.Looper.loop(Looper.java:130)
08-19 22:13:03.467: E/AndroidRuntime(4265):     at android.app.ActivityThread.main(ActivityThread.java:3701)
08-19 22:13:03.467: E/AndroidRuntime(4265):     at java.lang.reflect.Method.invokeNative(Native Method)
08-19 22:13:03.467: E/AndroidRuntime(4265):     at java.lang.reflect.Method.invoke(Method.java:507)
08-19 22:13:03.467: E/AndroidRuntime(4265):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
08-19 22:13:03.467: E/AndroidRuntime(4265):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
08-19 22:13:03.467: E/AndroidRuntime(4265):     at dalvik.system.NativeStart.main(Native Method)
08-19 22:13:03.467: E/AndroidRuntime(4265): Caused by: java.lang.NullPointerException
08-19 22:13:03.467: E/AndroidRuntime(4265):     at com.ravi.vrs.Voice.onActivityResult(Voice.java:71)
08-19 22:13:03.467: E/AndroidRuntime(4265):     at android.app.Activity.dispatchActivityResult(Activity.java:3908)
08-19 22:13:03.467: E/AndroidRuntime(4265):     at android.app.ActivityThread.deliverResults(ActivityThread.java:2549)
08-19 22:13:03.467: E/AndroidRuntime(4265):     ... 11 more

Could anyone please guide me how to fix this issue ? 谁能指导我解决此问题?

I'm assuming that the code fragment you posted is from the onActivityResult method of class Voice . 我假设您发布的代码片段来自Voice类的onActivityResult方法。 The problem is probably due to a contact not having a defined DISPLAY_NAME , in which case name will be set to null . 问题可能是由于联系人没有定义的DISPLAY_NAME ,在这种情况下, name将设置为null You are not checking for a null value for name . 您不检查name的空值。 Change your code from this: 从此更改代码:

if (name!="")

to: 至:

if (name != null && name.length() > 0)

You should also be protecting against exceptions when you call getString . 调用getString时,还应防止出现异常。 From the docs for Cursor.getString : 文档的Cursor.getString

The result and whether this method throws an exception when the column value is null or the column type is not a string type is implementation-defined. 结果以及此方法是否在列值为null或列类型不是字符串类型时引发异常是实现定义的。

I would suggest you to write the result of people.getString(1) to logcat, because I guess it's null. 我建议您将people.getString(1)的结果写入logcat,因为我猜它为空。 Look at ResultInfo{who=null, ...} 看一下ResultInfo{who=null, ...}

Cheers 干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM