gpt4 book ai didi

java.lang.NullPointerException 在 android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java :224)

转载 作者:搜寻专家 更新时间:2023-10-30 22:11:22 25 4
gpt4 key购买 nike

我无法理解我是如何错误地初始化数据库的:

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());

fragment 构造函数调用 getActivity() 为时过早 - 它将返回 null。在 SQLiteOpenHelper 构造函数中将 null 作为 Context 传递会在调用 get...Database() 时导致异常。

PhoneDal 初始化推迟到 onAttach() 或 fragment 生命周期中的更晚时间。

关于java.lang.NullPointerException 在 android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java :224),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27585374/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com