Answer the question
In order to leave comments, you need to log in
How to work with sqlite in fragments?
Good afternoon, since I have little experience in Android development, I can not figure out the error.
In the main activity (in onCreate), depending on the presence of any data in the database: I get a Cursor and check the number of records in the table, if getCount() == 0, one fragment is loaded (FragmentEmpty), and if getCount()! =0, respectively, another (MainActivityFragment()).
Code from main activity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainActivityFragment = new MainActivityFragment();//at ru.kolpashikov.simplegpstracker.MainActivity.onCreate(MainActivity.java:28)
fragmentEmpty = new FragmentEmpty();
}
@Override
public void onResume(){
super.onResume();
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
try{
try {
dbHelper = new DBTracksHelper(this);
dbase = dbHelper.getReadableDatabase();
}catch(IOException e){}
c = dbase.query(true, Constants.TABLE_NAME, new String[]{"dt"},
null, null, null, null, null, null);
int nCount = c.getCount();
c.close();
try {
dbHelper.close();
dbase.close();
}catch(Exception e){}
if(nCount != 0){
fragmentTransaction.add(R.id.frameLayout, mainActivityFragment);
}else {
fragmentTransaction.add(R.id.frameLayout, fragmentEmpty);
}
}catch(SQLiteException e){}
fragmentTransaction.commit();
}
public MainActivityFragment() {
try{
dbHelper = new DBTracksHelper(getActivity());
dbase = dbHelper.getReadableDatabase(); //at ru.kolpashikov.simplegpstracker.MainActivityFragment.<init>(MainActivityFragment.java:37)
}catch(IOException e){}
getLoaderManager().initLoader(0, null, this);
}
09-05 11:01:10.653 11765-11765/ru.kolpashikov.simplegpstracker E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{ru.kolpashikov.simplegpstracker/ru.kolpashikov.simplegpstracker.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2062)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2087)
at android.app.ActivityThread.access$600(ActivityThread.java:133)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1198)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4793)
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:808)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:575)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188)
at ru.kolpashikov.simplegpstracker.MainActivityFragment.<init>(MainActivityFragment.java:37)
at ru.kolpashikov.simplegpstracker.MainActivity.onCreate(MainActivity.java:28)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2026)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2087)
at android.app.ActivityThread.access$600(ActivityThread.java:133)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1198)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4793)
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:808)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:575)
at dalvik.system.NativeStart.main(Native Method)
Answer the question
In order to leave comments, you need to log in
firstly, the number of records in the table can be obtained using sql, pulling all the data for this is, to put it mildly, inefficient and bad
form, why do you immediately create two fragments in onCreate, I also don’t understand, except perhaps as in a joke ─ one glass of water by the bed if you want drink, the second one is empty if you don’t want to))
you at least display errors if something happened inside, but you don’t know about it and the
first thing that catches your eye in the log is NullPointerException
for good, you need to describe the provider and catch changes in the number of records in the database using CursorLoader , with its help to work in a fragment
Fragment does not need a constructor !
everything you need can be passed to him using the Bundle
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question