gpt4 book ai didi

java - Dao Room数据库无法访问

转载 作者:行者123 更新时间:2023-12-01 17:44:15 25 4
gpt4 key购买 nike

所以我创建了这个数据库,由于某种原因我收到一条错误消息:Cannot access database on the main thread since it may potentially lock the UI for a long period of time.日志显示错误位于:wordList = (ArrayList<Note>) wDb.noteDao().getAllNotes(); ,但我看不出我做错了什么?

ListActivity.java

    private NoteDatabase wDb;
ArrayList<Note> wordList;


protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

s1 = getResources().getStringArray(R.array.PL);//Kald denne først
startService(new Intent(ListActivity.this,WordLeanerService.class).putExtra("inputExtra", s1));
setupConnectionToWLservice();

LoadData(); //Load SharedPreferences
setContentView(R.layout.activity_main);
s2 = getResources().getStringArray(R.array.DC);
s3 = getResources().getStringArray(R.array.DC2);

recyclerView = findViewById(R.id.recyclerView);
Exit = findViewById(R.id.CloseApp);
editSearch = findViewById(R.id.EditText);
searchBtn = findViewById(R.id.search_button);
getData(); //get set data from EditActivity and set Arrays

wDb = NoteDatabase.getInstance(getApplicationContext());
wordList = (ArrayList<Note>) wDb.noteDao().getAllNotes();
if(wordList.isEmpty()){
for (int i = 0; i < images.length; i++) {
Note nWord = new Note(s1[i], s3[i], s2[i], images[i], items[i], Notes[i]);
wDb.noteDao().insert(nWord);
}
}
}

Note.java

package com.example.word_learner_app;

import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.PrimaryKey;

@Entity(tableName = "note_table")
public class Note {
@PrimaryKey(autoGenerate = true)
private int id;
@ColumnInfo(name = "wordTitle")
private String title;
@ColumnInfo(name = "description")
private String description;
@ColumnInfo(name = "prounciation")
private String prounciation;
@ColumnInfo(name = "image")
private int Image;
@ColumnInfo(name = "rating")
private String rating;
@ColumnInfo(name = "notes")
private String notes;

public Note(String title, String description, String prounciation, int Image, String rating, String notes) {
this.title = title;
this.description = description;
this.prounciation = prounciation;
this.Image = Image;
this.rating = rating;
this.notes = notes;
}

public void setId(int id) {
this.id = id;
}

public int getId() {
return id;
}

public String getTitle() {
return title;
}

public String getDescription() {
return description;
}

public String getProunciation() {
return prounciation;
}

public int getImage() {
return Image;
}

public String getRating() {
return rating;
}

public String getNotes() {
return notes;
}
}

NoteDao.java

package com.example.word_learner_app;

import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.Query;
import androidx.room.Update;

import java.util.List;

@Dao
public interface NoteDao {
@Insert
void insert(Note note);

@Update
void update(Note note);

@Delete
void delete(Note note);

@Query("SELECT * FROM note_table")
List<Note> getAllNotes();
}

NoteDatabase.java

package com.example.word_learner_app;

import android.content.Context;

import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;

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

private static NoteDatabase instance;

public abstract NoteDao noteDao();

public static synchronized NoteDatabase getInstance(Context contexte){
if(instance == null){
instance = Room.databaseBuilder(contexte.getApplicationContext(),
NoteDatabase.class, "note_database")
.fallbackToDestructiveMigration()
.build();
}
return instance;
}
}

Logcat

2020-03-27 16:40:06.697 18007-18007/? E/ord_learner_ap: Unknown bits set in runtime_flags: 0x8000
2020-03-27 16:40:08.226 18007-18007/com.example.word_learner_app E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.word_learner_app, PID: 18007
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.word_learner_app/com.example.word_learner_app.ListActivity}: 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:3270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
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.java:267)
at androidx.room.RoomDatabase.query(RoomDatabase.java:323)
at androidx.room.util.DBUtil.query(DBUtil.java:83)
at com.example.word_learner_app.NoteDao_Impl.getAllNotes(NoteDao_Impl.java:158)
at com.example.word_learner_app.ListActivity.onCreate(ListActivity.java:67)
at android.app.Activity.performCreate(Activity.java:7802)
at android.app.Activity.performCreate(Activity.java:7791)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) 
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016) 
at android.os.Handler.dispatchMessage(Handler.java:107) 
at android.os.Looper.loop(Looper.java:214) 
at android.app.ActivityThread.main(ActivityThread.java:7356) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 

最佳答案

您尝试执行的操作在 Android 中未经授权,您尝试从 MainThread 获取数据库中的数据,这可能会导致应用程序卡住。这就是为什么建议使用协程来执行此类功能。

编辑:在java中你可以使用callback就像这个codelabs

关于java - Dao Room数据库无法访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60889398/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com