【发布时间】:2015-07-01 23:09:11
【问题描述】:
我有一个基本适配器类,并想使用光标从它访问一个 SQLite 表,但我在上下文中遇到了问题。我是否以正确的方式获取上下文?
public class GetAllEntrysListViewAdapter extends BaseAdapter {
private JSONArray dataArray;
private Activity activity;
private static LayoutInflater inflater = null;
public Cursor cursor;
private SQLiteDatabase dbase;
DbHelper dbh;
private Context context;
String pos;
public GetAllEntrysListViewAdapter(JSONArray jsonArray, Activity a) {
this.dataArray = jsonArray;
this.activity = a;
inflater = (LayoutInflater)this.activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public GetAllEntrysListViewAdapter(Context context) {
this.context = context;
}
//...
dbh = new DbHelper(context);
cursor = getLikes(dbh);
cursor.moveToFirst();
if (cursor.moveToFirst()) {
do {
if (Integer.parseInt(cursor.getString(2)) == 1) {
cell.likeImage.setImageResource(R.drawable.heart_filled);
}
}
while(cursor.moveToNext());
}
else {
cursor.close();
}
cursor.close();
}
public Cursor getLikes(DbHelper dbh) {
dbase = dbh.getReadableDatabase();
String columns[] = {dbh.LIKES_MARKERID, dbh.LIKES_POSITION, dbh.LIKES_LIKE};
String selection = dbh.LIKES_MARKERID + " LIKE ? AND " + dbh.LIKES_POSITION + " LIKE ? ";
String args[] = {pinboard.markerID.toString(), pos};
Cursor cursor = dbase.query(dbh.TABLE_LIKES, columns, selection, args , null, null, null, null);
return cursor;
}
这是我收到的错误消息:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase android.content.Context.openOrCreateDatabase(java.lang.String, int, android.database.sqlite.SQLiteDatabase$CursorFactory, android.database.DatabaseErrorHandler)' on a null object reference
【问题讨论】:
-
您是否在其他要使用
inflater = (LayoutInflater)this.activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);的目标中使用Activity 参数?看我的回答
标签: android sqlite android-context baseadapter