片段和recyclerview实现会导致错误

时间:2016-11-20 12:35:17

标签: android sqlite android-fragments

不知道为什么,但我得到一些奇怪的错误。控制台说: "引起:java.lang.NullPointerException:尝试调用虚拟方法&android; android.database.sqlite.SQLiteDatabase android.content.Context.openOrCreateDatabase(java.lang.String,int,android。 database.sqlite.SQLiteDatabase $ CursorFactory,android.database.DatabaseErrorHandler)'在空对象引用"

DBHandler

public class MyDatabaseHelper extends SQLiteOpenHelper {

private static final String DATABASE_NAME="CategoryDatabase";

public MyDatabaseHelper(Context context) {

    super(context, DATABASE_NAME, null, 1);

}

@Override

public void onCreate(SQLiteDatabase database) {

    database.execSQL("CREATE TABLE Category (_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT);");

}

@Override

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    db.execSQL("DROP TABLE IF EXISTS Category");

    onCreate(db);

}

public void addCategory(String name)

{

    ContentValues values=new ContentValues(1);

    values.put("name", name);

    getWritableDatabase().insert("Category", "name", values);

}

public Cursor getCategories()

{

    Cursor cursor = getReadableDatabase().rawQuery("select * from Category", null);

    return cursor;

}

public ArrayList<Category> GetAllValues()
{
    ArrayList<Category> list = new ArrayList<Category>();
    Cursor cursor =  getCategories();
    if (cursor.moveToFirst())
    {
        do
        {
            Category cat = new Category(cursor.getString(0));
            list.add(cat);
        }
        while (cursor.moveToNext());
    }
    if (cursor != null && !cursor.isClosed())
    {
        cursor.close();
    }

    return list;
   }

0 个答案:

没有答案