0

I've migrated my old app to use the new Room database implementation (wow it took forever even though it's got 5 small, simple tables) but now for some reason when I launch the app it crashes immediately. It's especially nice because Logcat doesn't show a thing, because it appears it needs the app actually running to show anything (you have to select the process in that dropdown).

It's just this line in my Fragment class:

private ShotTrackerDao dao = ShotTrackerRoomDatabase.getDatabase(getContext()).shotTrackerDao();

If I comment it out, it runs fine. The Room DB class and the DAO class are the same as in the Udemy course I'm following and in YouTube videos I see on the topic.

@Dao
public interface ShotTrackerDao {
    @Insert(onConflict = OnConflictStrategy.ABORT)
    void insertFirearm(Firearm firearm);

    @Query("UPDATE firearm SET deleted = '1'")
    void deleteAllFirearms();

    ... so on, so forth
}

And the Room DB:

@Database(entities = { Ammunition.class, Caliber.class, Firearm.class, ShotTrack.class, UnitOfMeasure.class },
        version = 2,
        exportSchema = true)
@TypeConverters({CustomTypeConverters.class})
public abstract class ShotTrackerRoomDatabase extends RoomDatabase {

    private static volatile ShotTrackerRoomDatabase INSTANCE;
    private static final String DATABASE_NAME = "shot_tracker.db";
    private static final int NUMBER_OF_THREADS = 4;

    public abstract ShotTrackerDao shotTrackerDao();
    public static final ExecutorService databaseWriteExecutor = Executors.newFixedThreadPool(NUMBER_OF_THREADS);

public static ShotTrackerRoomDatabase getDatabase(final Context context)
    {
        if (INSTANCE == null) {
            synchronized (ShotTrackerRoomDatabase.class) {
                if (INSTANCE == null) {
                    INSTANCE = Room.databaseBuilder(context.getApplicationContext(), ShotTrackerRoomDatabase.class, DATABASE_NAME)
                        .addMigrations(MIGRATION_1_2)
                        .build();
                }
            }
        }

        return INSTANCE;
    }

    ... also has a Migration in it, although that's already worked
}

Does anyone know how I can figure out what's going wrong? I have no idea with Logcat not doing anything. Thanks much for any help.

clamum
  • 1,237
  • 10
  • 18
  • can we get the traceback pls? – Fred Dec 20 '21 at 08:46
  • open logcat and write "FATAL" it will show something, if not try use next lib https://github.com/mlegy/red-screen-of-death – Mahmoud Mabrok Dec 20 '21 at 08:49
  • I don't know how to get "traceback" but I managed to get Logcat to display the exception. LOL now for some reason I'm getting an error when a Fragment is added via the SectionsPagerAdapter (code I got right from the Udemy course I'm following). This has always worked until now, for some ungodly reason. I haven't touched any of the code related to this. The error: "java.lang.IllegalStateException: Fragment already added: FirearmsFragment{blah blah} at androidx.fragment.app.FragmentManager.addFragment(FragmentManager.java:1628)" So weird. Thanks for your help anyway guys. – clamum Dec 21 '21 at 04:32

1 Answers1

1

(As we have no Exception/crash message) may be this due to running queries at main thread.

So you should add allowMainThreadQueries() to Room's Builder.

Mahmoud Mabrok
  • 1,362
  • 16
  • 24
  • It wasn't due to that but thanks very much for the reply. I'll keep that in mind when using Room. – clamum Dec 21 '21 at 04:28