Android 联系人选取器应用程序在手机上崩溃,但在模拟器上未崩溃
所以我有一个来自Android的关于微调器的示例代码,我对它进行了一些修改,以显示其中一个微调器中的联系人,在模拟器上它工作正常,它显示了我在联系人中添加的5个条目,但在手机上它在启动时就崩溃了,我无法理解LogCat中的任何内容,有什么问题吗? 这是密码Android 联系人选取器应用程序在手机上崩溃,但在模拟器上未崩溃,android,spinner,logcat,Android,Spinner,Logcat,所以我有一个来自Android的关于微调器的示例代码,我对它进行了一些修改,以显示其中一个微调器中的联系人,在模拟器上它工作正常,它显示了我在联系人中添加的5个条目,但在手机上它在启动时就崩溃了,我无法理解LogCat中的任何内容,有什么问题吗? 这是密码 // add items into spinner dynamically public void addItemsOnSpinner2() { spinner2 = (Spinner) findViewById(R.
// add items into spinner dynamically
public void addItemsOnSpinner2() {
spinner2 = (Spinner) findViewById(R.id.spinner2);
list = new ArrayList<String>();
getContacts(null);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(dataAdapter);
}
private String getContacts(String contact) {
people = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while (people.moveToNext()) {
int nameFieldColumnIndex = people.getColumnIndex(PhoneLookup.DISPLAY_NAME);
contact = people.getString(nameFieldColumnIndex);
list.add(contact);
}
people.close();
return contact;
}
原来我手机上的第一个条目是空的(null),谁知道为什么。。。我就这样把它修好了
public void getContacts() {
people = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while (people.moveToNext()) {
int nameFieldColumnIndex = people.getColumnIndex(PhoneLookup.DISPLAY_NAME);
String s = people.getString(nameFieldColumnIndex);
if (s != null)
list.add(s);
}
people.close();
}
为什么在
getContacts(null)中传递null
代码>方法?如果是它的字符串,则应改为传递”
(双引号)。活动的名称是什么?公共类MainActivity扩展活动{
public void getContacts() {
people = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while (people.moveToNext()) {
int nameFieldColumnIndex = people.getColumnIndex(PhoneLookup.DISPLAY_NAME);
String s = people.getString(nameFieldColumnIndex);
if (s != null)
list.add(s);
}
people.close();
}