Android SQLiteOpenHelper:未调用onCreate()方法

时间:2014-04-17 04:00:59

标签: android sqlite

此代码发生NullPointException。我认为onCreate方法不会在null中调用'db'。 但我找不到错误的点。看看下面的代码并给我任何建议。任何帮助将不胜感激。

public class DBManager {
    private static DBManager instance;

    public static DBManager getInstance() {
        if (instance == null) {
            instance = new DBManager();
        }
        return instance;
    }

    DBHelper openHelper;

    private DBManager() {
        openHelper = new DBHelper(MyApplication.getContext());
    }

    public void insertPerson(PersonData person) {
        SQLiteDatabase db = openHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(PersonTable.NAME, person.name);
        values.put(PersonTable.COMMENT, person.comment);
        values.put(PersonTable.PHONE_NUM,person.phoneNum);
        values.put(PersonTable.TYPE,person.type);
        values.put(PersonTable.PROFILE_IMG,person.resId);

        db.insert(PersonTable.TABLE_NAME, null, values);
        db.close();
    }

public class DBHelper extends SQLiteOpenHelper {

        private final static String DB_NAME = "kakao.db";
        private final static int DB_VERSION = 1;

        public DBHelper(Context context) {
            super(context, DB_NAME, null, DB_VERSION);

        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            String sql = "CREATE TABLE "+ PersonTable.TABLE_NAME + "("
                    + PersonTable._ID
                    + " integer PRIMARY KEY autoincrement , "
                    + PersonTable.NAME + " text, "
                    + PersonTable.PHONE_NUM + " text, "
                    + PersonTable.PROFILE_IMG + " text, "
                    + PersonTable.COMMENT + " text, "
                    + PersonTable.TYPE + " text);";
            db.execSQL(sql);
        }

第29行是'SQLiteDatabase db = openHelper.getWritableDatabase();'

FATAL EXCEPTION: main
java.lang.NullPointerException
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
at taca.yongseong.kakaomessanger.DBManager.insertPerson(DBManager.java:29)
at taca.yongseong.kakaomessanger.Friend.getContact(Friend.java:137)
at taca.yongseong.kakaomessanger.Friend.setData(Friend.java:106)
at taca.yongseong.kakaomessanger.Friend.onCreateView(Friend.java:51)

1 个答案:

答案 0 :(得分:1)

您传递给Context构造函数的SQLiteOpenHelpernull