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

时间:2018-03-29 19:47:29

标签: android cursor crash

我已经编写了一些代码来获取我存储在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);

1 个答案:

答案 0 :(得分:0)

请使用以下代码。

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

                    resultSet.moveToNext();
                }
            }