接触游标返回 NullPointerException
Posted
技术标签:
【中文标题】接触游标返回 NullPointerException【英文标题】:Contact Cursor Returning NullPointerException 【发布时间】:2013-06-26 20:17:36 【问题描述】:我正在使用以下代码获取收件箱中短信的联系人姓名。当我尝试以下代码时,它运行良好,直到一些扩展,但在获取一半名称后给出 NullPointerException:
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_smscontacts);
Uri uri=Uri.parse("content://sms/inbox");
Cursor cursor=getContentResolver().query(uri, null, null, null, null);
Cursor cur=null;
int Count=0;
while(cursor.moveToNext())
String address=cursor.getString(cursor.getColumnIndexOrThrow("address")).toString();
if (address != null)
Uri person=Uri.withAppendedPath(ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI, Uri.encode(address));
String[] projection = new String[] ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME ;
cur=getContentResolver().query(person, projection, null, null, null);
if(cur.moveToNext())
Log.i("Name", cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)));
Log.i("count", Count++ + "");
else
Log.i("cursor", "Null" + address);
else
Log.i("address", "Null");
cur.close();
cursor.close();
Log.i("count", Count + "");
LogCat 如下:
06-29 15:04:41.509: E/androidRuntime(28700): FATAL EXCEPTION: main
06-29 15:04:41.509: E/AndroidRuntime(28700): java.lang.RuntimeException: Unable to start activity ComponentInfocom.lovish.smscontactsdummy/com.lovish.smscontactsdummy.SMSContactsActivity: java.lang.NullPointerException
06-29 15:04:41.509: E/AndroidRuntime(28700): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2081)
06-29 15:04:41.509: E/AndroidRuntime(28700): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2106)
06-29 15:04:41.509: E/AndroidRuntime(28700): at android.app.ActivityThread.access$700(ActivityThread.java:134)
06-29 15:04:41.509: E/AndroidRuntime(28700): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1217)
06-29 15:04:41.509: E/AndroidRuntime(28700): at android.os.Handler.dispatchMessage(Handler.java:99)
06-29 15:04:41.509: E/AndroidRuntime(28700): at android.os.Looper.loop(Looper.java:137)
06-29 15:04:41.509: E/AndroidRuntime(28700): at android.app.ActivityThread.main(ActivityThread.java:4856)
06-29 15:04:41.509: E/AndroidRuntime(28700): at java.lang.reflect.Method.invokeNative(Native Method)
06-29 15:04:41.509: E/AndroidRuntime(28700): at java.lang.reflect.Method.invoke(Method.java:511)
06-29 15:04:41.509: E/AndroidRuntime(28700): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
06-29 15:04:41.509: E/AndroidRuntime(28700): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
06-29 15:04:41.509: E/AndroidRuntime(28700): at dalvik.system.NativeStart.main(Native Method)
06-29 15:04:41.509: E/AndroidRuntime(28700): Caused by: java.lang.NullPointerException
06-29 15:04:41.509: E/AndroidRuntime(28700): at com.lovish.smscontactsdummy.SMSContactsActivity.onCreate(SMSContactsActivity.java:38)
06-29 15:04:41.509: E/AndroidRuntime(28700): at android.app.Activity.performCreate(Activity.java:5047)
06-29 15:04:41.509: E/AndroidRuntime(28700): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
06-29 15:04:41.509: E/AndroidRuntime(28700): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2045)
06-29 15:04:41.509: E/AndroidRuntime(28700): ... 11 more
请帮我解决需要它的解决方案..!!
【问题讨论】:
在查询之后,您可以尝试在调用 cur.moveToNext() 之前检查是否 cur=null。 Nopes 同样的问题...它在之前的消息中检查 cur=null 后进入但再次卡住并开始将 cur 返回为 null.. 也许你可以试试 while(!cursor.isAfterLast()) 然后把 cursor.moveToNext() 放在循环的底部 您在哪个光标中看到错误?也发布 logcat @Android Noob....还是同样的问题 【参考方案1】:在while
循环之后,作为一个快速修复将这个:
if (cur != null)
cur.close();
如有疑问,请使用您的调试器。我不到一分钟就在 Eclipse 中诊断出问题...
【讨论】:
非常感谢它的工作...:) 我确实使用调试器...但直到今天我确实遇到了此类错误..所以每天都在努力学习更多...: )以上是关于接触游标返回 NullPointerException的主要内容,如果未能解决你的问题,请参考以下文章