Библиотека помещения - ошибка инициализации курсора? - PullRequest
0 голосов
/ 21 октября 2018

Я использую базу данных комнаты в своем приложении.Эмулятор Android и% 99 устройств моего пользователя не имеют с ним проблем, но некоторые из моих пользователей получают эту ошибку.

java.lang.IllegalStateException: Couldn't read row 0, col 1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.

Полная ошибка

android.database.CursorWindow.nativeGetString (CursorWindow.java)
android.database.AbstractWindowedCursor.getString (AbstractWindowedCursor.java:66)
me.ibrahimsn.applock.data.local.app.AppDao_Impl.getAllApps (AppDao_Impl.java:202)
me.ibrahimsn.applock.data.local.app.AppRepository.getAll (AppRepository.java:20)
me.ibrahimsn.applock.ui.start.StartViewModel.updateApps (StartViewModel.java:51)
me.ibrahimsn.applock.ui.start.StartActivity.onCreate (StartActivity.java:48)

Я использую его сDagger 2 и вот мои классы

@Database(entities = {App.class}, version = 1, exportSchema = false)
public abstract class AppDatabase extends RoomDatabase {

    private static AppDatabase INSTANCE;

    public abstract AppDao appDao();

    public static AppDatabase getAppDatabase(Context context) {
        if (INSTANCE == null) {
            INSTANCE = Room.databaseBuilder(context.getApplicationContext(), AppDatabase.class, "mydb")
                    .allowMainThreadQueries().build();
        }
        return INSTANCE;
    }

    public static void destroyInstance() {
        INSTANCE = null;
    }
}

Dagger Module

@Module
public class ApplicationModule {

    @Provides
    @Singleton
    AppDatabase provideRoomDatabase(Context context) {
        return AppDatabase.getAppDatabase(context);
    }

    @Provides
    @Singleton
    AppDao provideAppDao(AppDatabase appDatabase) {
        return appDatabase.appDao();
    }
}

Dao

@Dao
public interface AppDao {

    @Query("SELECT * FROM app")
    List<App> getAllApps();

    @Query("SELECT * FROM app WHERE label LIKE :query ORDER BY status DESC")
    List<App> findApps(String query);

    @Query("SELECT * FROM app WHERE status = 1")
    List<App> getLockedApps();

    @Insert
    void insertAll(App... apps);

    @Update
    void update(App app);

    @Query("UPDATE app SET status = :st")
    void updateAll(Integer st);

    @Delete
    void delete(App app);
}

Хранилище

@Singleton
public class AppRepository {

    private final AppDao appDao;

    @Inject
    public AppRepository(AppDao appDao) {
        this.appDao = appDao;
    }

    public List<App> getAll() {
        return appDao.getAllApps();
    }

    public List<App> find(String query) {
        return appDao.findApps(query);
    }

    public void insert(App... apps) {
        appDao.insertAll(apps);
    }

    public void update(App app) {
        appDao.update(app);
    }

    public void updateAll(Boolean status) {
        appDao.updateAll(status ? 1 : 0);
    }

    public void delete(App app) {
        appDao.delete(app);
    }
}

А вотфайл ошибки

public class StartViewModel extends ViewModel {

    private final AppRepository repository;
    private final PackageManager packageManager;
    private final IconHelper iconHelper;

    private final MutableLiveData<Boolean> status = new MutableLiveData<>();

    @Inject
    StartViewModel(AppRepository repository, PackageManager packageManager) {
        this.repository = repository;
        this.packageManager = packageManager;
        this.iconHelper = new IconHelper(packageManager);
    }

    void updateApps() {
        Intent launcherIntent = new Intent(Intent.ACTION_MAIN, null);
        launcherIntent.addCategory(Intent.CATEGORY_LAUNCHER);

        List<ResolveInfo> Resolves = this.packageManager.queryIntentActivities(launcherIntent, 0);

        List<String> AppPackages = new ArrayList<>();
        List<String> ResolvePackages = new ArrayList<>();

        List<App> Apps = repository.getAll(); //Error is here
        for(App a : Apps) AppPackages.add(a.getPackageName());
...