gpt4 book ai didi

android - CursorIndexOutOfBoundsException 错误

转载 作者:行者123 更新时间:2023-11-30 03:33:24 26 4
gpt4 key购买 nike

我正在尝试加载联系人信息并使用 ListView 显示它。但是我得到了一个错误。

我的代码:

public class Sync extends ListActivity implements OnClickListener {
// Progress Dialog
private ProgressDialog pDialog;

//cursor loader
SimpleCursorAdapter mAdapter;

List<HashMap<String, String>> contactList;

private static final String DisplayName="";
private static final String Given="";
private static final String Family="";
private static final String NickName="";
private static final String HomePhone="";
private static final String MobilePhone="";
private static final String WorkPhone="";
private static final String HomeEmail="";
private static final String WorkEmail="";
private static final String CompanyName="";
private static final String Title="";
ArrayList<String> list;

@Override
protected void onCreate(Bundle savedInstanceState) {

//ContactHoldObject co;
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
//ListView contactView = (ListView) findViewById(R.id.list);
contactList = new ArrayList<HashMap<String, String>>();

registerForContextMenu(getListView());
// Loading products in Background Thread
new LoadAllContacts().execute();

}

class LoadAllContacts extends AsyncTask<String, String, String> {

/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Sync.this);
pDialog.setMessage("Loading list of training centers. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting All contacts
* */
protected String doInBackground(String... args) {
Uri contactsUri = ContactsContract.Contacts.CONTENT_URI;
// Querying the table ContactsContract.Contacts to retrieve all the contacts
Cursor contactsCursor = getContentResolver().query(contactsUri, null, null, null,
ContactsContract.Contacts.DISPLAY_NAME + " ASC ");
// ContactHoldObject.contactinfo =new ContactInfo[contactsCursor.getCount()];
//co=new ContactHoldObject();


if(contactsCursor.moveToFirst()){
do{
long contactId = contactsCursor.getLong(contactsCursor.getColumnIndex("_ID"));

Uri dataUri = ContactsContract.Data.CONTENT_URI;

// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
Cursor dataCursor = getContentResolver().query(dataUri, null,
ContactsContract.Data.CONTACT_ID + "=" + contactId,
null, null);


String displayName="";
String given="";
String family="";
String nickName="";
String homePhone="";
String mobilePhone="";
String workPhone="";
String homeEmail="";
String workEmail="";
String companyName="";
String title="";

displayName=dataCursor.getString(dataCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
//contactView.
map.put(DisplayName, displayName);

if(dataCursor.moveToFirst()){

String whereName = ContactsContract.Data.MIMETYPE + " = ? AND " + ContactsContract.CommonDataKinds.StructuredName.CONTACT_ID + " = " + contactId;
String[] whereNameParams = new String[] { ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE };
Cursor nameCur = getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, whereName, whereNameParams, ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME);
while (nameCur.moveToNext()) {
if(nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME)) != null
&& !nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME)).equals("")){
given = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
}else{
given="No firstname";
}
map.put(Given, given);
/*given = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
family = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
displayName = dataCursor.getString(dataCursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME ));
String string = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.DATA13));*/

/*System.out.println("Name: " + given + " Family: " + family + " Displayname: " + display + "MIDDLE_NAME"+MIDDLE_NAME+"PHONETIC_FAMILY_NAME"+PHONETIC_FAMILY_NAME+" PHONETIC_MIDDLE_NAME: "+PHONETIC_MIDDLE_NAME+"PREFIX :"+PREFIX+ "SUFFIX :" +SUFFIX+"DATA1:: "+DATA1+ "DATA1 :"+DATA1);*/
}nameCur.close();
do{

// Getting NickName
if(dataCursor.getString(dataCursor.getColumnIndex("mimetype")).equals(ContactsContract.CommonDataKinds.Nickname.CONTENT_ITEM_TYPE))
nickName = dataCursor.getString(dataCursor.getColumnIndex("data1"));
//System.out.println("nickName "+nickName);
// Getting Phone numbers
if(dataCursor.getString(dataCursor.getColumnIndex("mimetype")).equals(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)){
switch(dataCursor.getInt(dataCursor.getColumnIndex("data2"))){
case ContactsContract.CommonDataKinds.Phone.TYPE_HOME :
homePhone = dataCursor.getString(dataCursor.getColumnIndex("data1"));
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE :
mobilePhone = dataCursor.getString(dataCursor.getColumnIndex("data1"));
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_WORK :
workPhone = dataCursor.getString(dataCursor.getColumnIndex("data1"));
break;
}
}


if(dataCursor.getString(dataCursor.getColumnIndex("mimetype")).equals(ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE ) ) {
switch(dataCursor.getInt(dataCursor.getColumnIndex("data2"))){
case ContactsContract.CommonDataKinds.Email.TYPE_HOME :
homeEmail = dataCursor.getString(dataCursor.getColumnIndex("data1"));
break;
case ContactsContract.CommonDataKinds.Email.TYPE_WORK :
workEmail = dataCursor.getString(dataCursor.getColumnIndex("data1"));
break;
}
}

// Getting Organization details
if(dataCursor.getString(dataCursor.getColumnIndex("mimetype")).equals(ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE)){
companyName = dataCursor.getString(dataCursor.getColumnIndex("data1"));
title = dataCursor.getString(dataCursor.getColumnIndex("data4"));
}



}while(dataCursor.moveToNext());
String details = "";
String email=" ";
// Concatenating various information to single s
if(homePhone != null && !homePhone.equals("") )
details = "HomePhone : " + homePhone + "\n";
if(mobilePhone != null && !mobilePhone.equals("") )
details += "MobilePhone : " + mobilePhone + "\n";
if(workPhone != null && !workPhone.equals("") )
details += "WorkPhone : " + workPhone + "\n";
if(nickName != null && !nickName.equals("") )
details += "NickName : " + nickName + "\n";
if(homeEmail != null && !homeEmail.equals("") ){
details += homeEmail + "\n";
email=homeEmail;
}
if(workEmail != null && !workEmail.equals("") ){
details += workEmail + "\n";
email=workEmail;
}
if(companyName != null && !companyName.equals("") )
details += "CompanyName : " + companyName + "\n";
if(title != null && !title.equals("") )
details += "Title : " + title + "\n";

if(details!= null && !details.equals("") ){
// System.out.println(given+"\n"+details);


// adding each child node to HashMap key => value
map.put(Family, family);
map.put(NickName, nickName);
map.put(HomePhone, homePhone);
map.put(WorkPhone, workPhone);
map.put(HomeEmail, homeEmail);
map.put(MobilePhone, mobilePhone);
map.put(WorkEmail, workEmail);
map.put(CompanyName, companyName);
map.put(Title, title);

//addContacts(given,email);
}
}dataCursor.close();
contactList.add(map);
list = new ArrayList<String>(map.values()); }while(contactsCursor.moveToNext());




// adding HashList to ArrayList
}contactsCursor.close();

return null;


}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
Sync.this, contactList,
R.layout.trackrow, new String[] {
DisplayName},
new int[] { R.id.textName });
// updating listview
setListAdapter(adapter);

}
});

}
//return "success";
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Map<String,String> item = (Map<String, String>) l.getItemAtPosition(position);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override // Creation du menu contextuel
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Action");
menu.add(0,100,0,"Call");
menu.add(0,200,1,"More details");
menu.add(0, 300, 3, "Localisation");
menu.add(0,400,2,"send mail");
menu.add(0,500,4,"add comment");
menu.add(0,600,5,"view comments");
menu.add(0,700,5,"Link WebSite");
}

@Override // Selection d'un item du menu contextuel
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
return true;
}

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub


}

}

这是在 logcat 中显示的错误:

android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 7

这可能是个愚蠢的错误,但我一直没弄明白!

最佳答案

你打电话

displayName=dataCursor.getString(dataCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));

之前

dataCursor.moveToFirst();

因此光标位于 -1,这会导致异常。

关于android - CursorIndexOutOfBoundsException 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17019113/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com