我是android开发的新手。我正在使用eclipse和java进行开发。我正在尝试读取联系人并进行一些字符串操作,例如 substring() 或 contains()。我做了以下步骤,
- 已添加权限 ( )
读取联系人的代码:
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)); } }
由于子字符串,我在这里遇到错误。如果我使用任何字符串类方法,我会收到错误消息。但是,如果在名称中分配一些硬编码值,那么我不会收到任何错误。IE。
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
谁能指导我如何解决这个问题?