Android棒棒糖错误:尝试读取手机通讯录时出现空指针异常
我在尝试读取Nexus5(更新为棒棒糖)中的电话联系人时出现以下错误 我正在使用以下代码从手机中检索联系人Android棒棒糖错误:尝试读取手机通讯录时出现空指针异常,android,sqlite,cursor,contacts,Android,Sqlite,Cursor,Contacts,我在尝试读取Nexus5(更新为棒棒糖)中的电话联系人时出现以下错误 我正在使用以下代码从手机中检索联系人 private void readContacts() { ContentResolver cr = getActivity().getContentResolver(); Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null
private void readContacts() {
ContentResolver cr = getActivity().getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
null, null, null);
Cursor c = null ;
Cursor pCur = null;
try {
String phone = null;
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
try {
DirectoryRecordEntry entry = null;
String id = cur.getString(cur
.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur
.getString(cur
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
int idIndex = cur.getColumnIndexOrThrow(Contacts._ID);
long ids = cur.getLong(idIndex);
Uri rawContactUri = ContentUris.withAppendedId(
RawContacts.CONTENT_URI, ids);
Uri entityUri = Uri.withAppendedPath(rawContactUri,
Entity.CONTENT_DIRECTORY);
c= getActivity().getContentResolver().query(entityUri,
new String[]{
RawContacts.SOURCE_ID}, null, null, null);
String source_id = "";
//此处获取空指针异常,c为空:(
while(c.moveToNext()){
source_id=c.getString(0);
}
if(整数
.parseInt(cur.getString(cur
.getColumnIndex(Contacts contract.Contacts.HAS_PHONE_NUMBER))>0){
if(source\u id!=null&&source\u id.length()>0){
条目=新的OutlookContactEntry(名称“”);
}否则{
条目=新电话号码项(名称“”);
}
pCur=cr.query(ContactsContract.CommonDataTypes.Phone.CONTENT\u URI,
无效的
Contacts contract.CommonDataTypes.Phone.CONTACT\u ID
+“=?”,新字符串[]{id},
无效);
while(pCur.moveToNext()){
试一试{
电话=pCur
.getString(pCur)
.getColumnIndex(ContactsContract.CommonDataTypes.Phone.NUMBER));
int phoneType=pCur
.getInt(pCur)
.getColumnIndex(ContactsContract.CommonDataTypes.Phone.TYPE));
if(OutlookContactEntry的条目实例){
OutlookContactEntry oEntry=(OutlookContactEntry)条目;
oEntry.addContact(getType(phoneType),phone);
}否则{
entry.getPhNumberList().add(电话);
}
}捕获(例外e){
Log.e(类别名称,“从电话中检索联系人时出错”,e);
}
}
}
if(条目!=null){
if(OutlookContactEntry的条目实例){
OutlookContactEntry oEntry=(OutlookContactEntry)条目;
List List=newarraylist(oEntry.getContacts().values());
如果(!list.isEmpty()){
entry.setNumber(list.get(0));
}
}否则{
如果(!entry.getPhNumberList().isEmpty()){
entry.setNumber(entry.getPhNumberList().get(0));
}
}
objListRowRecord.put(名称、条目);
}
}捕获(例外e){
Log.e(类别名称,“从电话中检索联系人时出错”,e);
}
}
}
}捕获(例外e){
Log.e(类别名称,“从电话中检索联系人时出错”,e);
}最后
{
试一试{
如果(pCur!=null){
pCur.close();
}
如果(c!=null){
c、 close();
}
如果(cur!=null){
cur.close();
}
cr=null;
}捕获(例外e){
Log.e(类名称,“错误关闭光标”,e);
}
}
}
我只是在将kitkat更新为棒棒糖后才遇到这个问题
它可以通过空检查来解决,但我真的想知道它背后的原因
任何帮助都将不胜感激
谢谢
阿米特说:
返回位于第一个条目之前的游标对象,或null 它返回
null
,因为它被允许这样做
查询内容解析程序时,始终需要检查null
。显示:
/**
* This method is to read contacts.
*/
public void readContacts() {
localPhTempList.clear();
ContentResolver cr = activity.getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
String phone = null;
Cursor c = null;
Cursor pCur = null;
try {
if (cur != null && cur.getCount() > 0) {
while (cur.moveToNext()) {
try {
DirectoryRecordEntry entry = null;
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
int idIndex = cur.getColumnIndexOrThrow(Contacts._ID);
long ids = cur.getLong(idIndex);
Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI, ids);
Uri entityUri = Uri.withAppendedPath(rawContactUri, Entity.CONTENT_DIRECTORY);
c = activity.getContentResolver().query(entityUri, new String[] {
RawContacts.SOURCE_ID }, null, null, null);
String source_id = "";
if (c != null) {
while (c.moveToNext()) {
source_id = c.getString(0);
}
}
if (c != null) {
c.close();
}
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
if (source_id != null && source_id.length() > 0) {
entry = new OutlookContactEntry(name, "-1");
} else {
entry = new PhoneNumberEntry(name, "-2");
}
pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " ='"
+ id + "'", null, null);
if (pCur != null) {
while (pCur.moveToNext()) {
try {
phone = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
int phoneType = pCur.getInt(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
if (entry instanceof OutlookContactEntry) {
OutlookContactEntry oEntry = (OutlookContactEntry) entry;
String names = getType(phoneType);
String number = phone;
if (names != null && names.length() > 0 && number != null && number.length() > 0
/*
* && !number.equalsIgnoreCase("-1")
* && !number.equalsIgnoreCase("-1")
*/) {
oEntry.addContact(names, number);
}
} else {
entry.getPhNumberList().add(phone);
}
} catch (Exception e) {
Log.e(CLASS_NAME, "Error in retrieving contacts from phone", e);
}
}
if (pCur != null) {
pCur.close();
}
}
}
if (entry != null) {
if (entry instanceof OutlookContactEntry) {
OutlookContactEntry oEntry = (OutlookContactEntry) entry;
List<String> list = new ArrayList<String>(oEntry.getContacts().values());
if (!list.isEmpty()) {
entry.setNumber(list.get(0));
}
} else {
if (!entry.getPhNumberList().isEmpty()) {
entry.setNumber(entry.getPhNumberList().get(0));
}
}
// objListRowRecord.put(name, entry);
if (!(entry.getNumber().equalsIgnoreCase("-1") || entry.getNumber().equalsIgnoreCase("-2"))) {
localPhTempList.add(entry);
}
}
} catch (Exception e) {
Log.e(CLASS_NAME, "Error in retrieving contacts from phone", e);
}
}
}
} catch (Exception e) {
Log.e(CLASS_NAME, "Error in retrieving contacts from phone", e);
} finally {
try {
if (pCur != null) {
pCur.close();
pCur = null;
}
if (c != null) {
c.close();
c = null;
}
if (cur != null) {
cur.close();
cur = null;
}
cr = null;
} catch (Exception e) {
Log.e(CLASS_NAME, "Error closing cursor", e);
}
}
}
返回位于第一个条目之前的游标对象,或null 它返回
null
,因为它被允许这样做
查询内容解析程序时,始终需要检查null
。显示:
/**
* This method is to read contacts.
*/
public void readContacts() {
localPhTempList.clear();
ContentResolver cr = activity.getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
String phone = null;
Cursor c = null;
Cursor pCur = null;
try {
if (cur != null && cur.getCount() > 0) {
while (cur.moveToNext()) {
try {
DirectoryRecordEntry entry = null;
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
int idIndex = cur.getColumnIndexOrThrow(Contacts._ID);
long ids = cur.getLong(idIndex);
Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI, ids);
Uri entityUri = Uri.withAppendedPath(rawContactUri, Entity.CONTENT_DIRECTORY);
c = activity.getContentResolver().query(entityUri, new String[] {
RawContacts.SOURCE_ID }, null, null, null);
String source_id = "";
if (c != null) {
while (c.moveToNext()) {
source_id = c.getString(0);
}
}
if (c != null) {
c.close();
}
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
if (source_id != null && source_id.length() > 0) {
entry = new OutlookContactEntry(name, "-1");
} else {
entry = new PhoneNumberEntry(name, "-2");
}
pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " ='"
+ id + "'", null, null);
if (pCur != null) {
while (pCur.moveToNext()) {
try {
phone = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
int phoneType = pCur.getInt(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
if (entry instanceof OutlookContactEntry) {
OutlookContactEntry oEntry = (OutlookContactEntry) entry;
String names = getType(phoneType);
String number = phone;
if (names != null && names.length() > 0 && number != null && number.length() > 0
/*
* && !number.equalsIgnoreCase("-1")
* && !number.equalsIgnoreCase("-1")
*/) {
oEntry.addContact(names, number);
}
} else {
entry.getPhNumberList().add(phone);
}
} catch (Exception e) {
Log.e(CLASS_NAME, "Error in retrieving contacts from phone", e);
}
}
if (pCur != null) {
pCur.close();
}
}
}
if (entry != null) {
if (entry instanceof OutlookContactEntry) {
OutlookContactEntry oEntry = (OutlookContactEntry) entry;
List<String> list = new ArrayList<String>(oEntry.getContacts().values());
if (!list.isEmpty()) {
entry.setNumber(list.get(0));
}
} else {
if (!entry.getPhNumberList().isEmpty()) {
entry.setNumber(entry.getPhNumberList().get(0));
}
}
// objListRowRecord.put(name, entry);
if (!(entry.getNumber().equalsIgnoreCase("-1") || entry.getNumber().equalsIgnoreCase("-2"))) {
localPhTempList.add(entry);
}
}
} catch (Exception e) {
Log.e(CLASS_NAME, "Error in retrieving contacts from phone", e);
}
}
}
} catch (Exception e) {
Log.e(CLASS_NAME, "Error in retrieving contacts from phone", e);
} finally {
try {
if (pCur != null) {
pCur.close();
pCur = null;
}
if (c != null) {
c.close();
c = null;
}
if (cur != null) {
cur.close();
cur = null;
}
cr = null;
} catch (Exception e) {
Log.e(CLASS_NAME, "Error closing cursor", e);
}
}
}
返回位于第一个条目之前的游标对象,或null 它返回
null
,因为它被允许这样做
查询内容解析程序时,始终需要检查null
。显示:
/**
* This method is to read contacts.
*/
public void readContacts() {
localPhTempList.clear();
ContentResolver cr = activity.getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
String phone = null;
Cursor c = null;
Cursor pCur = null;
try {
if (cur != null && cur.getCount() > 0) {
while (cur.moveToNext()) {
try {
DirectoryRecordEntry entry = null;
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
int idIndex = cur.getColumnIndexOrThrow(Contacts._ID);
long ids = cur.getLong(idIndex);
Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI, ids);
Uri entityUri = Uri.withAppendedPath(rawContactUri, Entity.CONTENT_DIRECTORY);
c = activity.getContentResolver().query(entityUri, new String[] {
RawContacts.SOURCE_ID }, null, null, null);
String source_id = "";
if (c != null) {
while (c.moveToNext()) {
source_id = c.getString(0);
}
}
if (c != null) {
c.close();
}
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
if (source_id != null && source_id.length() > 0) {
entry = new OutlookContactEntry(name, "-1");
} else {
entry = new PhoneNumberEntry(name, "-2");
}
pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " ='"
+ id + "'", null, null);
if (pCur != null) {
while (pCur.moveToNext()) {
try {
phone = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
int phoneType = pCur.getInt(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
if (entry instanceof OutlookContactEntry) {
OutlookContactEntry oEntry = (OutlookContactEntry) entry;
String names = getType(phoneType);
String number = phone;
if (names != null && names.length() > 0 && number != null && number.length() > 0
/*
* && !number.equalsIgnoreCase("-1")
* && !number.equalsIgnoreCase("-1")
*/) {
oEntry.addContact(names, number);
}
} else {
entry.getPhNumberList().add(phone);
}
} catch (Exception e) {
Log.e(CLASS_NAME, "Error in retrieving contacts from phone", e);
}
}
if (pCur != null) {
pCur.close();
}
}
}
if (entry != null) {
if (entry instanceof OutlookContactEntry) {
OutlookContactEntry oEntry = (OutlookContactEntry) entry;
List<String> list = new ArrayList<String>(oEntry.getContacts().values());
if (!list.isEmpty()) {
entry.setNumber(list.get(0));
}
} else {
if (!entry.getPhNumberList().isEmpty()) {
entry.setNumber(entry.getPhNumberList().get(0));
}
}
// objListRowRecord.put(name, entry);
if (!(entry.getNumber().equalsIgnoreCase("-1") || entry.getNumber().equalsIgnoreCase("-2"))) {
localPhTempList.add(entry);
}
}
} catch (Exception e) {
Log.e(CLASS_NAME, "Error in retrieving contacts from phone", e);
}
}
}
} catch (Exception e) {
Log.e(CLASS_NAME, "Error in retrieving contacts from phone", e);
} finally {
try {
if (pCur != null) {
pCur.close();
pCur = null;
}
if (c != null) {
c.close();
c = null;
}
if (cur != null) {
cur.close();
cur = null;
}
cr = null;
} catch (Exception e) {
Log.e(CLASS_NAME, "Error closing cursor", e);
}
}
}
返回位于第一个条目之前的游标对象,或null 它返回
null
,因为它被允许这样做
查询内容解析器时,始终需要检查null
。/**
/**
* This method is to read contacts.
*/
public void readContacts() {
localPhTempList.clear();
ContentResolver cr = activity.getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
String phone = null;
Cursor c = null;
Cursor pCur = null;
try {
if (cur != null && cur.getCount() > 0) {
while (cur.moveToNext()) {
try {
DirectoryRecordEntry entry = null;
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
int idIndex = cur.getColumnIndexOrThrow(Contacts._ID);
long ids = cur.getLong(idIndex);
Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI, ids);
Uri entityUri = Uri.withAppendedPath(rawContactUri, Entity.CONTENT_DIRECTORY);
c = activity.getContentResolver().query(entityUri, new String[] {
RawContacts.SOURCE_ID }, null, null, null);
String source_id = "";
if (c != null) {
while (c.moveToNext()) {
source_id = c.getString(0);
}
}
if (c != null) {
c.close();
}
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
if (source_id != null && source_id.length() > 0) {
entry = new OutlookContactEntry(name, "-1");
} else {
entry = new PhoneNumberEntry(name, "-2");
}
pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " ='"
+ id + "'", null, null);
if (pCur != null) {
while (pCur.moveToNext()) {
try {
phone = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
int phoneType = pCur.getInt(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
if (entry instanceof OutlookContactEntry) {
OutlookContactEntry oEntry = (OutlookContactEntry) entry;
String names = getType(phoneType);
String number = phone;
if (names != null && names.length() > 0 && number != null && number.length() > 0
/*
* && !number.equalsIgnoreCase("-1")
* && !number.equalsIgnoreCase("-1")
*/) {
oEntry.addContact(names, number);
}
} else {
entry.getPhNumberList().add(phone);
}
} catch (Exception e) {
Log.e(CLASS_NAME, "Error in retrieving contacts from phone", e);
}
}
if (pCur != null) {
pCur.close();
}
}
}
if (entry != null) {
if (entry instanceof OutlookContactEntry) {
OutlookContactEntry oEntry = (OutlookContactEntry) entry;
List<String> list = new ArrayList<String>(oEntry.getContacts().values());
if (!list.isEmpty()) {
entry.setNumber(list.get(0));
}
} else {
if (!entry.getPhNumberList().isEmpty()) {
entry.setNumber(entry.getPhNumberList().get(0));
}
}
// objListRowRecord.put(name, entry);
if (!(entry.getNumber().equalsIgnoreCase("-1") || entry.getNumber().equalsIgnoreCase("-2"))) {
localPhTempList.add(entry);
}
}
} catch (Exception e) {
Log.e(CLASS_NAME, "Error in retrieving contacts from phone", e);
}
}
}
} catch (Exception e) {
Log.e(CLASS_NAME, "Error in retrieving contacts from phone", e);
} finally {
try {
if (pCur != null) {
pCur.close();
pCur = null;
}
if (c != null) {
c.close();
c = null;
}
if (cur != null) {
cur.close();
cur = null;
}
cr = null;
} catch (Exception e) {
Log.e(CLASS_NAME, "Error closing cursor", e);
}
}
}
*此方法用于读取联系人。
*/
公共联系人(){
localphtemplast.clear();
ContentResolver cr=activity.getContentResolver();
Cursor cur=cr.query(ContactsContract.Contacts.CONTENT_URI,null,null,null);
字符串phone=null;
光标c=null;
光标pCur=null;
试一试{
如果(cur!=null&&cur.getCount()>0){
while(cur.moveToNext()){
试一试{
DirectoryRecordEntry=null;
字符串id=cur.getString(cur.getColumnIndex(ContactsContract.Contacts._id));
字符串名称=cur.getString(cur.getColumnIndex(C