喜retriving數據的所有我試圖從一個表稱爲接觸retrive的數據,但它給我的空指針異常而從數據庫中的Android
Cursor cursor = database.query(ContactsDBOpenHelper.TABLE_CONTACTS,allcolumns,null,null,null,null,null);
一個空指針異常有誰能夠提出什麼錯? 我有適當的數據在表中。
公共類ViewAllActivity擴展ListActivity {
SQLiteOpenHelper dbhelper;
SQLiteDatabase database;
ContactsDataSource contactsDataSource;
public static final String[] allcolumns = {
ContactsDBOpenHelper.COLUMN_SEQUENCE,
ContactsDBOpenHelper.COLUMN_NAME,
ContactsDBOpenHelper.COLUMN_DEPT,
ContactsDBOpenHelper.COLUMN_EMAIL,
ContactsDBOpenHelper.COLUMN_PHONE,
ContactsDBOpenHelper.COLUMN_ADDRESS,
ContactsDBOpenHelper.COLUMN_ZIPCODE
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
contactsDataSource = new ContactsDataSource(this);
contactsDataSource.open();
dbhelper = new ContactsDBOpenHelper(this);
dbhelper.getWritableDatabase();
List<Contact> contacts = viewAllC();
ArrayAdapter<Contact> adapter = new ArrayAdapter<Contact>(ViewAllActivity.this,R.layout.list,contacts);
setListAdapter(adapter);
}
public List<Contact> viewAllC() {
List<Contact> contacts = new ArrayList<Contact>();
Cursor cursor = database.query(ContactsDBOpenHelper.TABLE_CONTACTS,allcolumns,null,null,null,null,null);
Log.i("LOGCAT", "rows returned : " + cursor.getCount());
Contact contact;
if(cursor.getCount()>0) {
while(cursor.moveToNext()) {
contact = new Contact();
contact.setSequence(cursor.getLong(cursor.getColumnIndex(ContactsDBOpenHelper.COLUMN_SEQUENCE)));
contact.setName(cursor.getString(cursor.getColumnIndex(ContactsDBOpenHelper.COLUMN_NAME)));
contact.setDepartment(cursor.getString(cursor.getColumnIndex(ContactsDBOpenHelper.COLUMN_DEPT)));
contact.setEmail(cursor.getString(cursor.getColumnIndex(ContactsDBOpenHelper.COLUMN_EMAIL)));
contact.setPhone(cursor.getString(cursor.getColumnIndex(ContactsDBOpenHelper.COLUMN_PHONE)));
contact.setAddress(cursor.getString(cursor.getColumnIndex(ContactsDBOpenHelper.COLUMN_ADDRESS)));
contact.setZipCode(cursor.getLong(cursor.getColumnIndex(ContactsDBOpenHelper.COLUMN_ZIPCODE)));
contacts.add(contact);
}
}
return contacts;
}
}
This is My open helper
public class ContactsDBOpenHelper extends SQLiteOpenHelper {
public ContactsDBOpenHelper(Context context) {
\t \t super(context, DATABASE_NAME, null, DATABASE_VERSION);
\t }
\t @Override
\t public void onCreate(SQLiteDatabase db) {
\t \t db.execSQL(TABLE_CREATE);
\t \t Log.i(LOGTAG, "Table has been created");
\t }
\t @Override
\t public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
\t \t db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);
\t \t onCreate(db);
\t }
}
您可以發佈您的logcat – ashutiwari4 2014-11-04 11:40:34
Log.i(「logcat的」,「返回的行:」 + cursor.getCount());什麼來r在這裏 – 2014-11-04 11:41:23
它不去那裏,因爲它有一個例外,在它上面的線 – Makwana 2014-11-04 11:42:31