我无法理解我是如何错误地初始化数据库的:
java.lang.NullPointerException
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188)
at com.example.stopcall.app.dal.PhoneDal.getItem(PhoneDal.java:105)
at com.example.stopcall.app.ItemDetailFragment$1.onClick(ItemDetailFragment.java:87)
at android.view.View.performClick(View.java:4654)
at android.view.View$PerformClick.run(View.java:19438)
at android.os.Handler.handleCallback(Handler.java:733)
代码落在这一行:
SQLiteDatabase db = this.getWritableDatabase();
从这个代码:
public class PhoneDal extends SQLiteOpenHelper {
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = Constants.DB_NAME;
private static final String BLOCKED_PHONES_TABLE = "BLOCKED_PHONES_TABLE";
private static final String COMMENTS_TABLE = "COMMENTS_TABLE";
public PhoneDal(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// SQL statement to create book table
String CREATE_BLOCKED_PHONES_TABLE = "CREATE TABLE "
+ BLOCKED_PHONES_TABLE + " ( "
+ "id INTEGER PRIMARY KEY AUTOINCREMENT, " + "phone TEXT, "
+ "isBlocked BIT )";
db.execSQL(CREATE_BLOCKED_PHONES_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (newVersion > oldVersion) {
Log.w("MyAppTag", "Updating database from version " + oldVersion
+ " to " + newVersion + " .Existing data will be lost.");
// Drop older books table if existed
db.execSQL("DROP TABLE IF EXISTS " + BLOCKED_PHONES_TABLE);
// create fresh books table
this.onCreate(db);
}
}
private static final String KEY_ID = "id";
private static final String KEY_PHONE = "KEY_PHONE";
private static final String KEY_IS_BLOCKED = "KEY_IS_BLOCKED";
public void addItem(Phone phone) {
// 1. get reference to writable DB
SQLiteDatabase db = this.getWritableDatabase();
初始化代码:
public class ItemDetailFragment extends Fragment {
/**
* The fragment argument representing the item ID that this fragment
* represents.
*/
public static final String ARG_ITEM_ID = "item_id";
/**
* The dummy content this fragment is presenting.
*/
private DummyContent.DummyItem mItem;
private PhoneDal phoneDal;
private CommentDal commentDal;
/**
* Mandatory empty constructor for the fragment manager to instantiate the
* fragment (e.g. upon screen orientation changes).
*/
public ItemDetailFragment() {
this.mItem = mItem;
this.phoneDal = new PhoneDal(getActivity());
this.commentDal = new CommentDal(getActivity());
}
@SuppressLint("ValidFragment")
public ItemDetailFragment(DummyContent.DummyItem mItem, PhoneDal phoneDal, CommentDal commentDal) {
this.mItem = mItem;
this.phoneDal = phoneDal;
this.commentDal = commentDal;
}
最佳答案
public ItemDetailFragment() {
this.phoneDal = new PhoneDal(getActivity());
片段构造函数调用
getActivity()
太早了-它将返回null。当调用Context
时,在SQLiteOpenHelper
构造函数中将空作为get...Database()
传递会导致异常。在片段生命周期中将
PhoneDal
初始化推迟到onAttach()
或更晚。关于java - android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)上的java.lang.NullPointerException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27585374/