Добрый день, так как мало опыта в разработке под Android, не могу разобраться с ошибкой.
В основной активности (в onCreate), в зависимости от наличия в базе каких-либо данных: получаю Cursor, и проверяю количество записей в таблице, если getCount() == 0, подгружается один фрагмент (FragmentEmpty), и если getCount()!=0, соответственно, другой (MainActivityFragment()).
Код из основной активности:
@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();
}
Конструктор MainActivityFragment:
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)
Что я делаю не так?