我已经浏览过现有的NPE /数据库线程,但找不到任何直接相关的内容......
我有一个OnClickListener类(与关联的Activity类分开),它在下面的代码中调用createFromPreset方法。下面的方法代码中的最后一行(this.getWritableDatabase)抛出一个NPE。当我调试代码并在该行中断时,“this”显示为“TableControllerMealItem”(不像我期望的那样为null)。
此代码以前工作正常。这可能是巧合,但我刚刚创建了一个Application类(并在Android Manifest中注册了它)。这可能会搞乱吗?
public class TableControllerMealItem extends MySQLiteHelper {
public TableControllerMealItem(Context context) {
super(context);
}
public long createFromPreset(meal_item meal_item, long meal_id, long preset_item_id) {
String presetDesc = "";
int presetMinutes = 0;
//Get details from the preset_item (description and minutes)
String sql = "SELECT * FROM preset_item WHERE _id = " + preset_item_id;
SQLiteDatabase db = this.getWritableDatabase();
这是LogCat:
05-27 11:58:39.613: E/AndroidRuntime(3021): java.lang.NullPointerException
05-27 11:58:39.613: E/AndroidRuntime(3021): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
05-27 11:58:39.613: E/AndroidRuntime(3021): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
05-27 11:58:39.613: E/AndroidRuntime(3021): at com.ian.mealtimer.TableControllerMealItem.createFromPreset(TableControllerMealItem.java:56)
05-27 11:58:39.613: E/AndroidRuntime(3021): at com.ian.mealtimer.OnClickListenerSelectPresetItem.onClick(OnClickListenerSelectPresetItem.java:40)
05-27 11:58:39.613: E/AndroidRuntime(3021): at android.view.View.performClick(View.java:4438)
05-27 11:58:39.613: E/AndroidRuntime(3021): at android.view.View$PerformClick.run(View.java:18422)
05-27 11:58:39.613: E/AndroidRuntime(3021): at android.os.Handler.handleCallback(Handler.java:733)
05-27 11:58:39.613: E/AndroidRuntime(3021): at android.os.Handler.dispatchMessage(Handler.java:95)
05-27 11:58:39.613: E/AndroidRuntime(3021): at android.os.Looper.loop(Looper.java:136)
05-27 11:58:39.613: E/AndroidRuntime(3021): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-27 11:58:39.613: E/AndroidRuntime(3021): at java.lang.reflect.Method.invokeNative(Native Method)
05-27 11:58:39.613: E/AndroidRuntime(3021): at java.lang.reflect.Method.invoke(Method.java:515)
05-27 11:58:39.613: E/AndroidRuntime(3021): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-27 11:58:39.613: E/AndroidRuntime(3021): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-27 11:58:39.613: E/AndroidRuntime(3021): at dalvik.system.NativeStart.main(Native Method)
MySQLite帮助程序类在这里:
public class MySQLiteHelper extends SQLiteOpenHelper {
private static final String TAG = "MySQLiteHelper";
public static final String TABLE_PRESET_ITEM = "preset_item";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_PRESET_DESC = "preset_desc";
public static final String COLUMN_PRESET_MINUTES = "preset_minutes";
protected static final String DATABASE_NAME = "mealtimersqlite.db";
private static final int DATABASE_VERSION = 1;
public MySQLiteHelper(Context context) {
super(context, "mealtimersqlite.db", null, 5);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
String SQLCommand;
SQLCommand = "CREATE TABLE preset_item( _id INTEGER PRIMARY KEY, "
+ "preset_desc TEXT NULL, preset_minutes INTEGER NULL );";
db.execSQL(SQLCommand);
SQLCommand = "CREATE TABLE meal(_id INTEGER PRIMARY KEY,"
+ "meal_desc TEXT NULL, meal_ready_time INTEGER NULL, meal_reminders_flag INTEGER NULL);";
db.execSQL(SQLCommand);
SQLCommand = "CREATE TABLE meal_item(_id INTEGER PRIMARY KEY, "
+ "meal_item_desc TEXT NULL, meal_item_minutes INTEGER NULL, meal_item_start_time INTEGER NULL, "
+ "meal_item_ready_time INTEGER NULL, fk_meal_id INTEGER NOT NULL, fk_preset_item_id INTEGER NULL );";
db.execSQL(SQLCommand);
Log.d(TAG, "Mealtimer Database Created");
}
@Override
public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
// TODO Auto-generated method stub
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS preset_item;");
db.execSQL("DROP TABLE IF EXISTS meal;");
db.execSQL("DROP TABLE IF EXISTS meal_item;");
Log.d(TAG, "Mealtimer Database Tables Dropped;");
// Create tables again
onCreate(db);
}
}
这是调用createFromPreset方法的类:
public class OnClickListenerSelectPresetItem implements OnClickListener {
public final static String EXTRA_MEAL_ID = "com.ian.mealtimer.MEAL_ID";
private long glbMealId = 1;
Context context;
private Activity activity;
public OnClickListenerSelectPresetItem(Activity activity){
this.activity = activity;
this.glbMealId = ((MealTimerApplication) activity.getApplication()).getMealId();
}
public void onCreate(long meal_id) {
}
public void onClick(View view) {
//INSERT a meal_item using then preset_item_id of clicked item
//then return to the MealItemDetail Activity
//First get the id of the selected item
Long selectedPresetItem = Long.valueOf(view.getTag().toString());
//Now insert the meal_item record
// Save new meal_item record
meal_item meal_item = new meal_item();
long newRecord = new TableControllerMealItem(
context).createFromPreset(meal_item, glbMealId, selectedPresetItem);
答案 0 :(得分:1)
您传递给Context
构造函数的SQLiteOpenHelper
为null
。
答案 1 :(得分:0)
context
中的OnClickListenerSelectPresetItem
成员变量从未初始化,并且默认值为null
。
将null
传递给Context
到SQLiteOpenHelper
构造函数会导致getDatabaseLocked()
中的NPE一次,例如在助手上调用getWritableDatabase()
。
答案 2 :(得分:0)
您需要从传递给Context
的{{1}}参数初始化本地成员Activity
:
OnClickListenerSelectPresetItem()