Skip to content

Commit c27f763

Browse files
committed
Avoid crashing on start up if no active book is found
This should not happend, but it does. We've received some reports through Crashlytics. Nothing obviously wrong has been found. Maybe it got into an invalid state due to some bug in a previous version. As a consequence, this just workarounds the problem by setting the first book in the database as active so the application doesn't crash again. Fixes: http://crashes.to/s/63a0b0221ed http://crashes.to/s/740af90dd24 http://crashes.to/s/e5edc6bf27e
1 parent fc261f7 commit c27f763

2 files changed

Lines changed: 23 additions & 3 deletions

File tree

app/src/main/java/org/gnucash/android/app/GnuCashApplication.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import org.gnucash.android.db.adapter.ScheduledActionDbAdapter;
5151
import org.gnucash.android.db.adapter.SplitsDbAdapter;
5252
import org.gnucash.android.db.adapter.TransactionsDbAdapter;
53+
import org.gnucash.android.model.Book;
5354
import org.gnucash.android.model.Commodity;
5455
import org.gnucash.android.model.Money;
5556
import org.gnucash.android.service.ScheduledActionService;
@@ -145,8 +146,14 @@ public static void initializeDatabaseAdapters() {
145146
mDbHelper.getReadableDatabase().close();
146147
}
147148

148-
mDbHelper = new DatabaseHelper(getAppContext(),
149-
mBooksDbAdapter.getActiveBookUID());
149+
try {
150+
mDbHelper = new DatabaseHelper(getAppContext(),
151+
mBooksDbAdapter.getActiveBookUID());
152+
} catch (BooksDbAdapter.NoActiveBookFoundException e) {
153+
fixBooksDatabase();
154+
mDbHelper = new DatabaseHelper(getAppContext(),
155+
mBooksDbAdapter.getActiveBookUID());
156+
}
150157
SQLiteDatabase mainDb;
151158
try {
152159
mainDb = mDbHelper.getWritableDatabase();
@@ -167,6 +174,13 @@ public static void initializeDatabaseAdapters() {
167174
mBudgetsDbAdapter = new BudgetsDbAdapter(mainDb, mBudgetAmountsDbAdapter, mRecurrenceDbAdapter);
168175
}
169176

177+
/** Sets the first book in the database as active. */
178+
private static void fixBooksDatabase() {
179+
Book firstBook = BooksDbAdapter.getInstance().getAllRecords().get(0);
180+
firstBook.setActive(true);
181+
BooksDbAdapter.getInstance().addRecord(firstBook);
182+
}
183+
170184
public static AccountsDbAdapter getAccountsDbAdapter() {
171185
return mAccountsDbAdapter;
172186
}

app/src/main/java/org/gnucash/android/db/adapter/BooksDbAdapter.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,14 +156,20 @@ public boolean isActive(String bookUID){
156156
BookEntry.COLUMN_ACTIVE + "= 1", null, null, null, null, "1");
157157
try{
158158
if (cursor.getCount() == 0)
159-
throw new RuntimeException("There is no active book in the app. This should NEVER happen, fix your bugs!");
159+
throw new NoActiveBookFoundException("There is no active book in the app. This should NEVER happen, fix your bugs!");
160160
cursor.moveToFirst();
161161
return cursor.getString(cursor.getColumnIndexOrThrow(BookEntry.COLUMN_UID));
162162
} finally {
163163
cursor.close();
164164
}
165165
}
166166

167+
public class NoActiveBookFoundException extends RuntimeException {
168+
public NoActiveBookFoundException(String message) {
169+
super(message);
170+
}
171+
}
172+
167173
public @NonNull List<String> getAllBookUIDs(){
168174
List<String> bookUIDs = new ArrayList<>();
169175
try (Cursor cursor = mDb.query(true, mTableName, new String[]{BookEntry.COLUMN_UID},

0 commit comments

Comments
 (0)