Android Room 库。无法在主线程访问数据库

问题描述 投票:0回答:1

我有一个 SQLite 本地数据库,其中实现了 Room 库。 当我编译项目时创建本地数据库。

尝试构建项目并得到这个:

 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dblearning.dbroom_test/com.dblearning.dbroom_test.RecyclerActivity}: java.lang.IllegalStateException: Cannot access database on the main thread since it may potentially lock the UI for a long period of time.
                                                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3698)
                                                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3855)
                                                                                                        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:105)
                                                                                                        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:136)
                                                                                                        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:96)
                                                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2267)
                                                                                                        at android.os.Handler.dispatchMessage(Handler.java:106)
                                                                                                        at android.os.Looper.loopOnce(Looper.java:201)
                                                                                                        at android.os.Looper.loop(Looper.java:288)
                                                                                                        at android.app.ActivityThread.main(ActivityThread.java:7964)
                                                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                                                        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:553)
                                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
                                                                                                    Caused by: java.lang.IllegalStateException: Cannot access database on the main thread since it may potentially lock the UI for a long period of time.
                                                                                                        at androidx.room.RoomDatabase.assertNotMainThread(RoomDatabase.kt:439)
                                                                                                        at androidx.room.RoomDatabase.query(RoomDatabase.kt:479)
                                                                                                        at androidx.room.util.DBUtil.query(DBUtil.kt:75)
                                                                                                        at com.dblearning.dbroom_test.DAO_Impl.getAll(DAO_Impl.java:165)
                                                                                                        at com.dblearning.dbroom_test.RecyclerActivity.onCreate(RecyclerActivity.java:29)
                                                                                                        at android.app.Activity.performCreate(Activity.java:8069)
                                                                                                        at android.app.Activity.performCreate(Activity.java:8049)
                                                                                                        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1329)
                                                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3668)

这是我的活动课程:

public class RecyclerActivity extends AppCompatActivity  {
    private Button add,del,update;
    private EditText dataField;
    List<DataEntity>  dataEntityList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler);
        RoomDB db = Room.databaseBuilder(this,
                RoomDB.class, "tab").build();
        DAO dao = db.Dao();
        dataEntityList =dao.getAll();
        RecyclerView recyclerView = findViewById(R.id.recyclerView);
        Adapter entityAdapter = new Adapter(dataEntityList);
        recyclerView.setLayoutManager(new GridLayoutManager(this, 3));
        recyclerView.setAdapter(entityAdapter);
           ... 
}
}

这是我的 DAO 课程:

@Dao
public interface DAO {

    @Query("SELECT * FROM tab")
    List<DataEntity> getAll();

    @Query("SELECT * FROM tab WHERE id IN (:userIds)")
    List<DataEntity> loadAllByIds(int[] userIds);
    @Query("DELETE FROM tab")
    void deleteAll();

    @Query("SELECT * FROM tab WHERE first_item LIKE :first AND " + "second_item LIKE :last LIMIT 1")
    DataEntity findByName(String first, String last);

    @Update
    void update(DataEntity entity);

    @Insert
    void insertAll(DataEntity... Entities);
    @Delete
    void delete(DataEntity Entity);
}

这是我的实体类:

@Entity(  tableName ="tab" )
public class DataEntity {

    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "id")
    public int uid;

    public int getUid() {
        return uid;
    }

    public void setUid(int uid) {
        this.uid = uid;
    }

    @ColumnInfo(name = "first_item")
    public String firstItem;

    public String getFirstItem() {
        return firstItem;
    }

    public void setFirstItem(String firstItem) {
        this.firstItem = firstItem;
    }

    @ColumnInfo(name = "second_item")
    public String secondItem;

    public String getSecondItem() {
        return secondItem;
    }

    public void setSecondItem(String secondItem) {
        this.secondItem = secondItem;
    }
    public DataEntity(){

    }

}

预计通过 GUI 为将来的人口创建一个空数据库。 尝试编译我的项目时出现错误。 尝试使用 google 解决方案并通过错误消息在此处搜索它,但仅找到 kotlin。

java android android-sqlite android-room crud
1个回答
0
投票

您可以在Codelab中找到解决方案:developer.android.com/codelabs/android-room-with-a-view – dev.bmax

您需要在 ViewModel 中定义逻辑来处理 所有与 DB 相关的操作也在 IO 线程上执行。 – 奇拉格·图玛尔

我添加了 ViewModel 和存储库并在我的活动中使用它。我的应用程序已构建。 CRUD 操作现在可以正常工作了。

© www.soinside.com 2019 - 2024. All rights reserved.