[英]SQLiteDatabase.OpenDatabase is NullPointerException in Fragment
This works outside of fragment onCreateView
but not in that static method.这适用于片段
onCreateView
之外,但不适用于该静态方法。
SQLiteDatabase checkDB = SQLiteDatabase.openDatabase(path_to + DATABASE_NAME, null, SQLiteDatabase.OPEN_READWRITE);
I have a DataBaseHelper
which is initialized outside the static method as我有一个
DataBaseHelper
,它在静态方法之外初始化为
FeedReaderHelper mHelper = new FeedReaderHelper(this);
whereas for fragment, I use而对于片段,我使用
if (getActivity() != null)
FeedReaderHelper xHelper = new FeedReaderHelper(getActivity());
else
Log.d(TAG, "Null");
I know that getActivity
is not null and the error persists even if I only try to open and close the database.我知道
getActivity
不为空,即使我只尝试打开和关闭数据库,错误仍然存在。 My logcat goes like this:我的 logcat 是这样的:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
I don't get what Resources
means here and how to avoid the error altogether.我不明白
Resources
在这里意味着什么以及如何完全避免错误。
Also, I've tried opening the database before onContentView
is called;另外,我尝试在
onContentView
之前打开数据库; it still resolves to NullPointer upon checkDB.close
它仍然在
checkDB.close
解析为 NullPointer
I was using FeedReederHelper
only to call db.exec()
statements whereupon the same Helper should have been used to open database without the hard-coded path in checkDB
call.我只使用
FeedReederHelper
来调用db.exec()
语句,因此应该使用相同的 Helper 打开数据库,而无需在checkDB
调用中使用硬编码路径。 mHelper.getWriteableDatabase();
solved the issue.解决了这个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.