Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLite database in combination with fragments

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)

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;
}

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

Thanks for helping in advance.

like image 853
Marc Avatar asked May 17 '13 13:05

Marc


1 Answers

The Fix

Replace this line

private final DBAdapter db = new DBAdapter(getActivity());

with this

private DBAdapter db;

In the onActivityCreated() method, add the following line

db = new DBAdapter(getActivity());

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.

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.

like image 123
kdehairy Avatar answered Nov 14 '22 23:11

kdehairy