1

我有一个班级类别

  @DatabaseTable(tableName = "categories")
public class Category {
    public final static String CATEGORY_TITLE_FIELD_NAME  = "title";
    @SerializedName("id")
    @DatabaseField(id = true)
    int id;
    @SerializedName("title")
    @DatabaseField(dataType = DataType.STRING, columnName = CATEGORY_TITLE_FIELD_NAME)
    String title;
    // need for ORMlite
    public Category()
    {}
    public Category (int id, String title)
    {
        this.id = id;
        this.title = title;
    }
    public int getCategoryId() 
    {

         return this.id;
    }

    public String getCategoryTitle() 
    {
         return this.title;
    }

    public void setCategoryId(int id)
    {
         this.id = id;
    }

    public void setCategoryTitle(String title)
    {
         this.title = title;
    }

    @Override
    public String toString()
    {       
    return "{title="+title +" "+"id="+id+"}";   
    }

}

我从 JSON 中将数据放入其中,并希望将其保存在 ORMLite 中。我就是这样做的:

DatabaseHandler db = new DatabaseHandler(getApplicationContext());
JSONObject data = json.getJSONObject(KEY_DATA);
// getting categories
JSONArray categories = new JSONArray();
categories = data.getJSONArray(KEY_CATEGORIES);
Gson gson = new Gson();
JsonParser parser = new JsonParser();
JsonArray array = parser.parse(categories.toString()).getAsJsonArray();
Type listType = new TypeToken<List<Category>>() {}.getType();
List<Category> tasks = new ArrayList<Category>();
tasks = gson.fromJson(array.toString(), listType);
RuntimeExceptionDao<Category, Integer> simpleDao = getHelper1().getSimpleCategoryDao();
Dao<Category, Integer> categoryDao = getHelper1().getCategoryDao();
saveContacts(tasks);
List<Category> list = categoryDao.queryForAll();
Log.i("categoryDAO",list.toString());

这是用于保存数据的 saveContacts() 方法:

 public void saveContacts(List<Category> contacts) throws SQLException
    {
        OrmLiteSqliteOpenHelper dbHelper= DatabaseHandler.getInstance(_context);
        Dao<Category, Integer> daoContact=dbHelper.getDao(Category.class);
        for (Category contact : contacts) {
            daoContact.create(contact);
        }
        dbHelper.close();
    }

但我有这样的例外:

 FATAL EXCEPTION: main
java.lang.NullPointerException
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:118)
at com.j256.ormlite.android.AndroidConnectionSource.getReadWriteConnection(AndroidConnectionSource.java:63)
    at com.j256.ormlite.dao.BaseDaoImpl.create(BaseDaoImpl.java:304)
    at com.assignmentexpert.LoginActivity.saveContacts(LoginActivity.java:192)
    at com.assignmentexpert.LoginActivity$1.onClick(LoginActivity.java:108)
    at android.view.View.performClick(View.java:2485)
    at android.view.View$PerformClick.run(View.java:9080)
    at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:123)
    at android.app.ActivityThread.main(ActivityThread.java:3687)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:507)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
at dalvik.system.NativeStart.main(Native Method)

请告诉我我做错了什么?

4

1 回答 1

0

问题解决了将上下文传递给我的从 OrmLiteSqliteOpenHelper 扩展的 DatabaseHandler。

于 2012-08-14T08:59:29.690 回答