SQLite数据库与片段结合 [英] SQLite database in combination with fragments
问题描述
我正在将我的应用程序从活动"更改为片段",它运行的很好,但是我有一个错误使我无法打开数据库.这是错误(来自logcat):
I'm changing my application from Activity's to Fragments, it's going great but I've one bug that won't let me open my database. Here's the error (from the logcat):
05-17 15:28:38.704 13025-13025/com.MJV.werktijden E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
at com.MJV.werktijden.DBAdapter.open(DBAdapter.java:71)
at com.MJV.werktijden.OverviewFragment.onActivityCreated(OverviewFragment.java:38)
at android.app.Fragment.performActivityCreated(Fragment.java:1703)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:903)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
at android.app.BackStackRecord.run(BackStackRecord.java:682)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
我尝试自己解决问题,但找不到问题.仅仅因为当我将数据库与Activity一起使用时,该数据库确实可以工作. 我用这种方法打开数据库:
I tried to solve it myself, but I couldn't find the problem. Just because this database did work when I used it with Activity's. I'm opening the database with this method:
//---opens the database---
public DBAdapter open() throws SQLException
{
db = DBHelper.getWritableDatabase();
return this;
}
然后从我的Fragment中调用该方法:
And that method is being called from my Fragment:
private final DBAdapter db = new DBAdapter(getActivity());
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.overview_layout, container, false);
return rootView;
}
public void onActivityCreated (Bundle savedInstanceState){
listView = (ListView) getView().findViewById(R.id.listView1);
db.open();
感谢您的提前帮助.
推荐答案
修复
替换此行
private final DBAdapter db = new DBAdapter(getActivity());
与此
private DBAdapter db;
在onActivityCreated()
方法中,添加以下行
db = new DBAdapter(getActivity());
发生了什么
在Fragment
的private final DBAdapter db = new DBAdapter(getActivity());
行中,您在创建活动之前正在调用getActivity()
.因此,当SQLiteOpenHelper
尝试使用Context
(您的活动)时,它会发现它为null.
What is happening
In the line private final DBAdapter db = new DBAdapter(getActivity());
in your Fragment
, you are calling getActivity()
before the activity is created. So, when the SQLiteOpenHelper
tries to use the Context
(your activity), it finds that it is null.
简单的解决方法是仅在创建Activity
之后调用getActivity()
(从而打开数据库).在创建Activity
之后将调用onActivityCreated()
方法,因此新代码将能够通过getActivity()
获取您的Activity
引用,而不会引发任何null异常.
The simple fix is to call getActivity()
(and thereby open your database) only after the Activity
has been created. onActivityCreated()
method is invoked after Activity
has been created and hence the new code will be able to get the reference of yourActivity
via getActivity()
without throwing any null exception.
这篇关于SQLite数据库与片段结合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!