检查游标时java.lang通过getCount方法引发的空指针异常

时间:2018-11-30 08:02:10

标签: java android xml nullpointerexception

这是我代码的示例,只是为了从电话簿中提取联系人,但要拨行

if(phone.getCount() > 0)

它会产生一些错误,提示为nullpointerException,下面是来自Java文件的我的逻辑

StringBuilder builder = new StringBuilder();
    ContentResolver contentResolver = getContentResolver();
    Cursor phones = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,null,null,null);

    if (phones.getCount() > 0)
    {
        String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
        String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

        while (phones.moveToNext())
        {

            if(name!=null)
            {
                builder.append(namecsv).append(name);
                builder.append(phonecsv).append(phoneNumber);
            }
        }

    }

    phones.close();

    nameArray = nameCsv.split(",");
    phoneArray = phoneCsv.split(",");

    ArrayAdapter <String> adapter = new ArrayAdapter<String>(this,android.R.layout.activity_list_item,android.R.id.text1,nameArray);
    contactViewer.setAdapter(adapter);

0 个答案:

没有答案