在Android开发中阅读联系人时获取错误

时间:2013-08-19 16:58:12

标签: android

我是Android开发的新手。我正在开发eclipse和java。我正在尝试读取联系人并执行一些字符串操作,如substring()或contains()。我做了以下步骤,

  1. 许可添加()
  2. 阅读联系人的代码:

    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));
    }
    }
    
  3. 这里我因为子字符串而收到错误。如果我使用任何字符串类方法,我会收到错误。但是如果在名称中分配一些硬编码值,那么我没有收到任何错误。即

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

    错误讯息:

    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
    

    有人可以指导我如何解决这个问题吗?

2 个答案:

答案 0 :(得分:0)

我假设您发布的代码片段来自类onActivityResult的{​​{1}}方法。问题可能是由于联系人未定义Voice,因此DISPLAY_NAME将设置为name。您没有检查null的空值。从这个改变您的代码:

name

为:

if (name!="")

当您致电if (name != null && name.length() > 0) 时,您还应该防范异常。来自the docs for Cursor.getString

  

结果以及当列值为null或列类型不是字符串类型时此方法是否抛出异常是实现定义的。

答案 1 :(得分:0)

我建议你把people.getString(1)的结果写入logcat,因为我猜它是空的。 看看ResultInfo{who=null, ...}

干杯