我试图在MainActivity中的onCreate期间自动将值插入到sqlite数据库中。
以下是一些代码:
public static final String KEY_RULEID = "_id";
public static final String KEY_RULE = "rule";
public static final String KEY_TYPE = "type";
public static final String KEY_ENABLED = "enabled";
private static final String rulesc = "??????";
private static final int typesc = 0;
private static final String enabledsc = "TRUE";
private SQLiteDatabase db;
/* insert default rules into the database */
private void insertDefaultRule(String rule, int type, String enabled) {
ContentValues defaultValues = new ContentValues();
defaultValues.put(KEY_RULE, rulesc);
defaultValues.put(KEY_TYPE, typesc);
defaultValues.put(KEY_ENABLED, enabledsc);
System.out.println("line 175");
db.insert(Constants.DATABASES_TABLE_RULES, null, defaultValues);
}
第175行打印到系统日志,因此我知道db.insert行是错误的来源。主要错误是NullPointerException,但这是另一半,可能会有所帮助。
java.lang.NullPointerException
E/AndroidRuntime(7178): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:118)
E/AndroidRuntime(7178): at com.prototype.sample.RulesDatabaseAdapter.open(RulesDatabaseAdapter.java:55)
E/AndroidRuntime(7178): at com.prototype.sample.RulesDatabaseAdapter.insertDefualtRules(RulesDatabaseAdapter.java:155)
E/AndroidRuntime(7178): at com.prototype.sample.MessagesList.DefaultRules(MessagesList.java:88)
E/AndroidRuntime(7178): at com.prototype.sample.MessagesList.onCreate(MessagesList.java:53)
E/AndroidRuntime(7178): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
E/AndroidRuntime(7178): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
E/AndroidRuntime(7178): ... 11 more
编辑:添加了相关信息。这个类确实扩展了SQLiteOpenHelper。
Constants.DATABASES_TABLE_RULES在Constants类中定义如下:
public final static String DATABASES_TABLE_RULES = "rules";
打开数据库的代码:
/* opens the database */
public RulesDatabaseAdapter open() throws SQLException {
db = DBHelper.getWritableDatabase();
return this;
}