我有一个数据库类,我用来将一个sql lite数据库连接到一个listview,该类存储了我所有的上下文内容,例如retreieving行,但即使这个简单的getPaitnets也令人心烦这是我的全班
public class DatabaseHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "CautioManager",
TABLE_CONTACTS = "patient",
KEY_ID = "id",
KEY_NAME = "name",
KEY_PHONE = "phone",
KEY_CONTACTPHONE = "contactphone",
KEY_EMAIL = "email",
KEY_ADDRESS = "address",
KEY_IMAGEURI = "imageUri";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_CONTACTS + "(" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_NAME + " TEXT," + KEY_PHONE + " TEXT," + KEY_EMAIL + " TEXT," + KEY_ADDRESS + " TEXT," + KEY_IMAGEURI + " TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);
onCreate(db);
}
public void createContact(Patients contact) {
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, contact.getName());
values.put(KEY_PHONE, contact.getPhone());
values.put(KEY_CONTACTPHONE, contact.get_contactPhone());
values.put(KEY_EMAIL, contact.getEmail());
values.put(KEY_ADDRESS, contact.getAddress());
values.put(KEY_IMAGEURI, contact.getImageURI().toString());
db.insert(TABLE_CONTACTS, null, values);
db.close();
}
public Patients getPatientsById(int id) {
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.query(TABLE_CONTACTS, new String[] { KEY_ID, KEY_NAME, KEY_PHONE, KEY_EMAIL, KEY_ADDRESS, KEY_IMAGEURI }, KEY_ID + "=?", new String[] { String.valueOf(id) }, null, null, null, null );
if (cursor != null)
cursor.moveToFirst();
Patients _patients = new Patients(Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), cursor.getString(5), Uri.parse(cursor.getString(6)));
db.close();
cursor.close();
return _patients;
}
public void deleteContact(Patients contact) {
SQLiteDatabase db = getWritableDatabase();
db.delete(TABLE_CONTACTS, KEY_ID + "=?", new String[] { String.valueOf(contact.getId()) });
db.close();
}
public int getContactsCount() {
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_CONTACTS, null);
int count = cursor.getCount();
db.close();
cursor.close();
return count;
}
public int updatePatients(Patients _Patients) {
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, _Patients.getName());
values.put(KEY_PHONE, _Patients.getPhone());
values.put(KEY_CONTACTPHONE, _Patients.get_contactPhone());
values.put(KEY_EMAIL, _Patients.getEmail());
values.put(KEY_ADDRESS, _Patients.getAddress());
values.put(KEY_IMAGEURI, _Patients.getImageURI().toString());
int rowsAffected = db.update(TABLE_CONTACTS, values, KEY_ID + "=?", new String[] { String.valueOf(_Patients.getId()) });
db.close();
return rowsAffected;
}
public List<Patients> getALlPatients() {
List<Patients> _patients = new ArrayList<Patients>();
SQLiteDatabase db = getWritableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_CONTACTS, null);
if (cursor.moveToFirst()) {
do {
_patients.add(new Patients(Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4),cursor.getString(5), Uri.parse(cursor.getString(6))));
}
while (cursor.moveToNext());
}
if (cursor.getCount()==0)
{
_patients.add(new Patients(1,"David Buckley","","","","",null));
_patients.add(new Patients(2,"James Buckley","","","","",null));
}
cursor.close();
db.close();
return _patients;
}
}
然后在我的活动中,我使用它来声明db
的实例public DatabaseHandler db = new DatabaseHandler(this);
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNavigationDrawerFragment = (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
List<Patients> patientsList= new ArrayList<Patients>();
ArrayAdapter<Patients> adapter = new ArrayAdapter<Patients>(this,android.R.layout.simple_list_item_1,patientsList);
ListView lv= (ListView) findViewById(R.id.listView);
lv.setAdapter(adapter);
}
但是当我编译它并在模拟器上运行它时会返回跟随null对象错误。
对不起,这里的人是错误。
Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
显示fragment_patient_list的布局文件
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.davidbuckley.Cautio.Fragments.PatientsListFragment">
<!-- TODO: Update blank fragment layout -->
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:layout_gravity="center" />
</FrameLayout>