我项目中的手机类型和电子邮件类型存在一些问题。我使用2.1平台,在添加电话类型和电子邮件类型之前我的项目没有问题,在我尝试使用这些手机类型和电子邮件类型后要运行该项目,它会打开一个消息框并显示应用程序已停止并强制关闭。我的xml文件有一个Button和ListView。我正在使用此代码。
码
public class GetAllDatas extends Activity {
ListView lvItem;
private Button btnAdd;
String Ptype, Etype;
int contactPhoneType;
String displayName="", emailAddress="", phoneNumber="";
ArrayList<String> contactlist=new ArrayList<String>();
ArrayAdapter<String> itemAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lvItem = (ListView)this.findViewById(R.id.lvitems);
btnAdd = (Button)this.findViewById(R.id.btnAddItem);
itemAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,contactlist);
lvItem.setAdapter(itemAdapter);
btnAdd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
readContacts();
}
});
}
private void readContacts()
{
ContentResolver cr =getContentResolver();
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while (cursor.moveToNext())
{
displayName = "" ;
ArrayList<String> phoneNumber = new ArrayList<String>();
ArrayList<String> emailAddress = new ArrayList<String>();
displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
/* Email */
Cursor emails = cr.query(Email.CONTENT_URI,null,Email.CONTACT_ID + " = " + id, null, null);
while (emails.moveToNext())
{
emailAddress.add(emails.getString(emails.getColumnIndex(Email.DATA)));
}
contactPhoneType = emails.getInt(emails.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
switch(contactPhoneType){
case ContactsContract.CommonDataKinds.Phone.TYPE_HOME:
Etype = "Home";
break;
}
emails.close();
/* Phone Number and Type */
if(Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
{
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",new String[]{id}, null);
while (pCur.moveToNext())
{
phoneNumber.add(pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
}
contactPhoneType = pCur.getInt(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
switch(contactPhoneType){
case ContactsContract.CommonDataKinds.Phone.TYPE_HOME:
Ptype = "Home";
break;
}
pCur.close();
}
int phoneNumberCount = phoneNumber.size();
int emailCount = emailAddress.size();
// Add arraylist data to contactlist..
if(phoneNumberCount > emailCount) {
for (int i=0; i<phoneNumberCount; i++)
{
if(emailCount>i)
{
contactlist.add(displayName + " , " + Ptype + " , " + phoneNumber.get(i) + " , " + Etype + " , " + emailAddress.get(i) + "\n");
}
else
{
contactlist.add(displayName + " , " + Ptype + " , " + phoneNumber.get(i) + " , " + " , " + "\n");
}
}
}
else
{
for (int i=0; i<emailCount; i++)
{
if(phoneNumberCount>i)
{
contactlist.add(displayName + " , " + Ptype + " , " + phoneNumber.get(i) + " , " + Etype + " , " + emailAddress.get(i) + "\n");
}
else
{
contactlist.add(displayName + " , " + " , " + " , " + Etype + " , " + emailAddress.get(i) + "\n");
}
}
}
}
Collections.sort(contactlist);
itemAdapter.notifyDataSetChanged();
cursor.close();
}
}
logcat的
06-26 13:31:16.213: E/AndroidRuntime(449): Uncaught handler: thread main exiting due to uncaught exception
06-26 13:31:16.224: E/AndroidRuntime(449): android.database.CursorIndexOutOfBoundsException: Index 1 requested, with a size of 1
06-26 13:31:16.224: E/AndroidRuntime(449): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
06-26 13:31:16.224: E/AndroidRuntime(449): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214)
06-26 13:31:16.224: E/AndroidRuntime(449): at android.database.AbstractWindowedCursor.getInt(AbstractWindowedCursor.java:84)
06-26 13:31:16.224: E/AndroidRuntime(449): at android.database.CursorWrapper.getInt(CursorWrapper.java:123)
06-26 13:31:16.224: E/AndroidRuntime(449): at com.abhi.GetAllDatas.readContacts(GetAllDatas.java:71)
06-26 13:31:16.224: E/AndroidRuntime(449): at com.abhi.GetAllDatas.access$0(GetAllDatas.java:47)
06-26 13:31:16.224: E/AndroidRuntime(449): at com.abhi.GetAllDatas$1.onClick(GetAllDatas.java:41)
答案 0 :(得分:0)
可能是这一行:
while (cursor.moveToNext())
对我来说,看起来你在读取第一个光标位置之前移动了光标,并且你得到了一个CursorIndexOOB例子。
尝试使用do-while循环,看看是否有帮助。
另外,请检查您的应用程序是否有权阅读联系人数据。
uses-permission android:name="android.permission.READ_CONTACTS"
答案 1 :(得分:0)
我的代码中出现错误。将光标移动到最后一条记录(p {{{}}循环超出最后一条记录后的pCur.moveToNext())后,您正在检查phoneType和emailtype。您需要在while
循环内检查它,而不是在if。之后。更改以下内容:
while
也对emailType进行相同的更正。
答案 2 :(得分:0)
看起来问题是索引
android.database.CursorIndexOutOfBoundsException: Index 1 requested, with a size of 1
获取Phone.TYPE可以根据此http://www.vtgroup.com/#ContactsContract
尝试以下代码 // Get all phone numbers.
//
Cursor phones = cr.query(Phone.CONTENT_URI, null,
Phone.CONTACT_ID + " = " + contactId, null, null);
while (phones.moveToNext()) {
String number = phones.getString(phones.getColumnIndex(Phone.NUMBER));
int type = phones.getInt(phones.getColumnIndex(Phone.TYPE));
switch (type) {
case Phone.TYPE_HOME:
// do something with the Home number here...
break;
case Phone.TYPE_MOBILE:
// do something with the Mobile number here...
break;
case Phone.TYPE_WORK:
// do something with the Work number here...
break;
}
}
phones.close();
答案 3 :(得分:0)
正如Dheeresh所说,你必须改变这种状况