0
此代碼發生NullPointException
。我認爲onCreate方法不會在null中調用'db'。 但我不能找到錯誤spot.ease看看下面的代碼,並給我任何建議。任何幫助將不勝感激。Android SQLiteOpenHelper:不調用onCreate()方法
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分貝= 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)
你可以發佈logcat異常嗎? – Szymon
@Szymon在後添加logcat。 – user2968002