我想显示SMS收件箱和发件箱中的联系人名称列表(如在本机通讯应用程序中)。我想出了下面的代码:NullPointerException在手机上运行时(但不在模拟器上)
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
Uri messagesUri = Uri.parse("content://sms/");
Cursor cursor = getContentResolver().query(messagesUri,new String[] { "_id", "thread_id", "address", "person", "date", "body", "type" }, null, null, null);
startManagingCursor(cursor);
String[] columns = new String[] { "address", "person", "date", "body", "type" };
String sms = "";
if (cursor.getCount() > 0) {
while (cursor.moveToNext()){
String address = cursor.getString(cursor.getColumnIndex(columns[0]));
sms += address + " ";
String contact=address;
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(address));
Cursor cs= getContentResolver().query(uri, new String[]{PhoneLookup.DISPLAY_NAME},PhoneLookup.NUMBER+"='"+address+"'",null,null);
startManagingCursor(cs);
if(cs.getCount()>0)
{
cs.moveToFirst();
contact=cs.getString(cs.getColumnIndex(PhoneLookup.DISPLAY_NAME));
}
listItems.add(contact);
}
}
adapter=new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
listItems);
setListAdapter(adapter);
}
当我运行在模拟器应用这个工作,但是当我尝试在手机上运行它,我得到一个NullPointerException。如果我对在logcat的错误消息双击下面一行被突出显示:
if(cs.getCount()>0)
这里有什么问题吗?
检查是否'cs'本身是有效的? – Mahesh 2012-03-29 20:32:32
我该怎么做? – user1301428 2012-03-30 06:26:31