gpt4 book ai didi

android - 到达光标时,Android应用程序崩溃

转载 作者:行者123 更新时间:2023-12-03 17:06:00 25 4
gpt4 key购买 nike

我写了一些代码来获取存储在SQLite数据库中的联系人。但是,当代码到达游标语句时,应用程序突然崩溃。

这是我到目前为止编写的代码:

public class CustomContactsActivity extends AppCompatActivity {
ListView listCustomContactsList;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_contacts);
listCustomContactsList = (ListView) findViewById(R.id.custom_contacts);
PopulateList();
}

//Insert selected contacts in to the SQLite database
public void InsertContact(String contactId, String contactName, String[] contactNo) {
int index = 0;
String concatContactNo = "";
BuzzDBHandler dbHandler = new BuzzDBHandler(this);
SQLiteDatabase database = dbHandler.getWritableDatabase();

if (contactNo.length > 1) {
for(int i = 0; i<contactNo.length; i++) {
concatContactNo += contactNo[i].toString() + "#";
}
}
String sql =
"INSERT OR REPLACE INTO " + BuzzDBSchema.CustomContacts.TABLE_NAME +
"( " + BuzzDBSchema.CustomContacts.COL_CONTACT_ID + ", " + BuzzDBSchema.CustomContacts.COL_CONTACT_NAME + ", " + BuzzDBSchema.CustomContacts.COL_CONTACT_NO+ " )" +
"VALUES(" + contactId+ ", " +contactName+ ", " + concatContactNo+ ")";
database.execSQL(sql);
}

private void PopulateList() {
BuzzDBHandler buzzDBHandler = new BuzzDBHandler(this);
SQLiteDatabase buzzDB = buzzDBHandler.getReadableDatabase();
List<String> listCustomContacts = new ArrayList<String>();
String[] selectedColums = {
BuzzDBSchema.CustomContacts.COL_CONTACT_ID,
BuzzDBSchema.CustomContacts.COL_CONTACT_NAME,
BuzzDBSchema.CustomContacts.COL_CONTACT_NO
};

Cursor getCustomContacts = buzzDB.query(true, BuzzDBSchema.CustomContacts.TABLE_NAME, selectedColums, null, null, null, null, null, null);
if (getCustomContacts.getCount() == 0) {
dlgWarning();
} else {
while (getCustomContacts.moveToNext()) {
String contactId = getCustomContacts.getString(getCustomContacts.getColumnIndex(BuzzDBSchema.CustomContacts.COL_CONTACT_ID));
String contactName = getCustomContacts.getString(getCustomContacts.getColumnIndex(BuzzDBSchema.CustomContacts.COL_CONTACT_NAME));
String contactNo = getCustomContacts.getString(getCustomContacts.getColumnIndex(BuzzDBSchema.CustomContacts.COL_CONTACT_NO));
}
getCustomContacts.close();
ArrayAdapter<String> simpleAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listCustomContacts);
listCustomContactsList.setAdapter(simpleAdapter);
}
}

private void dlgWarning() {
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
break;
case DialogInterface.BUTTON_NEGATIVE:
break;
}
}
};
}
}

该错误发生在我执行查询的语句的 PopulateList方法中,期望接收到游标:
Cursor getCustomContacts = buzzDB.query(true, BuzzDBSchema.CustomContacts.TABLE_NAME, selectedColums, null, null, null, null, null, null);

最佳答案

请使用下面的代码。

if(resultSet != null)
{
resultSet.moveToFirst();
while (!resultSet.isAfterLast()){

resultSet.moveToNext();
}
}

关于android - 到达光标时,Android应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49563948/

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