手机
当前位置:查字典教程网 >编程开发 >安卓软件开发 >while (cursor.moveToNext())时出现空指针异常
while (cursor.moveToNext())时出现空指针异常
摘要:求助,查询本地联系人时cursor出现空指针异常,新人,纠结一天了,谢谢各位大大,或者给个能用的其他连接也行,感谢,真找不出哪里出问题了Ur...

求助,查询本地联系人时cursor出现空指针异常,新人,纠结一天了,谢谢各位大大,或者给个能用的其他连接也行,感谢,真找不出哪里出问题了

Uri uri = Uri.parse("Content://com.android.providers.contacts/raw_contacts");

                Cursor cursor = resolver.query(uri, new String[] { "contact_id" },

null, null, null);

                              while (cursor.moveToNext()) ,在进去while()循环的时候

while (cursor.moveToNext())时出现空指针异常0

while (cursor.moveToNext())时出现空指针异常1

回复讨论(解决方案)

while (cursor.moveToNext())时出现空指针异常2

private ListView select_contact;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

select_contact = (ListView) findViewById(R.id.select_contact);

List< Map< String, String > > data = getContactInfo();

 select_contact.setAdapter(new SimpleAdapter(this, data, R.layout.contact_item_view,

 new String[]{"name","phone"}, new int[]{R.id.tv_name,R.id.tv_phone}));

}

/**

 * 读取联系人

 * 

 * @return

 */

private List< Map< String, String > > getContactInfo() {

//把所有联系人

List< Map< String, String > > list = new ArrayList< Map< String,String > >();//list是接口,所以new arraylist

// 得到一个内容解析器

ContentResolver resolver = getContentResolver();

// raw_contacts 表的uri

Uri uri = Uri.parse("Content://com.android.providers.contacts/raw_contacts");

Uri uriData = Uri.parse("Content://com.android.providers.contacts/data");

Cursor cursor = resolver.query(uri, new String[] { "contact_id" },

null, null, null);

// System.out.println(cursor);

while (cursor.moveToNext()) {

String contact_id = cursor.getString(0);

if (contact_id != null) {

//具体的某个联系人

Map< String, String > map = new HashMap< String, String >();//同样map是接口,所以new hashmap()

Cursor Datacursor = resolver.query(uriData, new String[] {

"data1", "mimetype_id" }, "contact_id=?",

new String[] { contact_id }, null);

while (Datacursor.moveToNext()) {

String data1 = Datacursor.getString(0);

String mimetype = Datacursor.getString(1);

System.out.println("data1=="+data1+"==mimetype=="+mimetype);

// vhd.android.cursor.item/name

if ("7".equals(mimetype)) {

//联系人的姓名

map.put("name", data1);

}else if ("5".equals(mimetype)) {

//联系人的电话号码

map.put("phone", data1);

}

}

list.add(map);

Datacursor.close();

}

}

cursor.close();

return list;

} 游标里面有值?确认游标不是空的吧?

先判断一下,可以参考:

http://greenboy1.iteye.com/blog/847190 各位大大帮帮忙啊,卡着了 这个好像没什么难度吧,很明显,你的cursor是null啊。

先确认你查询的对象是否存在。

		Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
		while (cursor.moveToNext()) {
		}
还是不行,ContentResolver resolver = getContentResolver(); 就是这句话出了问题,注释后,

Cursor cursor = resolver.query(uri, new String[] { "contact_id" },

null, null, null);就提示这句话空指针,求赐教!!!!!!!!!!!!!!!!!!!!!!!!! 游标不是空的,已在外层加了  if(cursor!=null&&cursor.getCount() >=1),依然报错

while (cursor.moveToNext())时出现空指针异常3 我也想知道你这个问题怎么解决的 这个问题很简单,我每次都是while (cursor.moveToNext())  都说这里找不到,网上看了很多说获取  getContentResolver() 的时候在前面加上getApplicationContext(),但我亲测还是不行,,

ContentResolver resolver =getApplicationContext().getContentResolver(),

最后我想应该就是读取内容的时候没有拿到,  在AndroidManifest.xml中  我就把权限提到最前面就可以拿到值了。

【while (cursor.moveToNext())时出现空指针异常】相关文章:

android如何短时间音效播放实现SoundPool

android开启图案解锁时,取消滑动解锁

Android:多线程之图文混排

Android用代码获取手机root之后的最高权限

android中的按两次返回键退出代码

Android开发笔记之:复写按钮方法

Android取消EditText自动获取焦点默认行为

Android工程:引用另一个Android工程的方法详解

android将搜索引擎设置为中国雅虎无法搜索问题解决方法

Android_Mvp+DataBinding最佳实战

精品推荐
分类导航