NullPointerException on getWritableDatabase after adding Fragments to my project
I had a working project, now I wanted to upgrade the layout and I'm trying to add Fragments (pre honeycomb), but now seems like I'm having some troubles connecting to my DB, and I'm getting NullPointerException
:
ERROR/AndroidRuntime(272开发者_Python百科94):
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:118)
ERROR/AndroidRuntime(27294):
at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:187)
I believe it has something to do with the Context i'm sending to the database constructor.
here is my class:
public class ShowFragment extends ListFragment {
ArrayList<String> results = new ArrayList<String>();
private SQLiteDatabase db;
WorkTrackdb workdb = new WorkTrackdb(getActivity());
private ViewGroup mRootView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Cursor c = getYears();
//some code here
}
Cursor getYears() {
db = workdb.getReadableDatabase();
String years[] = {"year"};
Cursor cursor = db.query(WorkTrackdb.TABLE_NAME, years, null,
null, null, null,"year DESC");
return cursor;
}
Try using SQLiteDatabase.openDatabase(String path, SQLiteDatabase.CursorFactory factory, int flags)
instead.
I'm not sure maybe you need to instantiate WorkTrackdb workdb = new WorkTrackdb(getActivity());
inside the onCreate()
精彩评论