数据库助手,ORMLite和片段中为null

时间:2015-10-06 13:11:03

标签: android database ormlite

ORMLite和数据库帮助器出了问题。我读过因为使用片段我需要创建新类 - OrmLiteFragment扩展了Fragment。我做到了,但我在行上得到了空白:

databaseHelper =
                    OpenHelperManager.getHelper(getActivity(), DatabaseHelper.class);

这是我的班级:

public class OrmLiteFragment extends Fragment {

private DatabaseHelper databaseHelper = null;


protected DatabaseHelper getHelper() {
    if (databaseHelper == null) {
        databaseHelper =
                OpenHelperManager.getHelper(getActivity(), DatabaseHelper.class);
    }
    return databaseHelper;
}

@Override
public void onDestroy() {
    super.onDestroy();
    if (databaseHelper != null) {
        OpenHelperManager.releaseHelper();
        databaseHelper = null;
    }

}

而且,这是我的DatabasebaseHelper。类

public class DatabaseHelper extends OrmLiteSqliteOpenHelper {

private static final String DATABASE_NAME = "link.db";
private static final int DATABASE_VERSION = 1;
private RuntimeExceptionDao<Link, Integer> linkRuntimeDao = null;
private Dao<Link, Integer> linkDao;


public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION, R.raw.ormlite_config);

}
@Override
public void onCreate(SQLiteDatabase sqliteDatabase, ConnectionSource connectionSource) {
    Log.d("onCreate", "Creating Database");
    try {
        TableUtils.createTable(connectionSource, Link.class);

    } catch (SQLException e) {
        Log.e(DatabaseHelper.class.getName(), "Unable to create database", e);
        throw new RuntimeException(e);
    }
}

@Override
public void close() {
    super.close();
    linkRuntimeDao = null;
}
@Override
public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource, int oldVer, int newVer) {

}
public Dao<Link, Integer> getLinkDao() throws SQLException {
    if (linkDao == null) {
        linkDao = getDao(Link.class);
    }
    return linkDao;
}

任何想法有什么不对?

0 个答案:

没有答案