这是错误代码
C:\Users\huh\AndroidStudioProjects\test\app\build\generated\ap_generated_sources\debug\out\com\name\test\database\AppDatabase_Impl.java:56: error: onCreate(SupportSQLiteDatabase) in <anonymous com.droidbane.test.database.AppDatabase_Impl$1> cannot override onCreate(SupportSQLiteDatabase) in Delegate
protected void onCreate(SupportSQLiteDatabase _db) {
^
attempting to assign weaker access privileges; was public
- 您能否至少分享一下我的错误或您使用的房间代码,或者给予一个示例?*
这是我的错误代码,它引导我到这里:(应用数据库_实现. java)
@Override
protected void onCreate(SupportSQLiteDatabase _db) {
if (mCallbacks != null) {
for (int _i = 0, _size = mCallbacks.size(); _i < _size; _i++) {
mCallbacks.get(_i).onCreate(_db);
}
}
}
文件夹布局
📦com.name.test
┣ 📂database
┃ ┗ 📜AppDatabase.java
┃ ┗ 📜Words.java
┃ ┗ 📜WordsDao.java
┣ 📜MainActivity.java
应用程序数据库.java
package com.name.test.database;
import androidx.room.Database;
import androidx.room.RoomDatabase;
@Database(entities = {Words.class}, version = 1)
public abstract class AppDatabase extends RoomDatabase {
public abstract WordsDao wordsDao();
}
歌词
package com.name.test.database;
import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.PrimaryKey;
@Entity
public class Words {
@PrimaryKey
public int uid;
@ColumnInfo(name = "words")
public String words;
}
言道
package com.name.test.database;
import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.Query;
import java.util.List;
@Dao
public interface WordsDao {
@Query("SELECT * FROM words")
List<Words> getAll();
@Insert
void insertAll(Words... words);
@Delete
void delete(Words words);
}
主要活动
package com.name.test;
import androidx.appcompat.app.AppCompatActivity;
import androidx.room.Room;
import android.nfc.Tag;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import com.droidbane.test.database.AppDatabase;
import com.droidbane.test.database.Words;
import com.droidbane.test.database.WordsDao;
import java.util.List;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void ChangeTextClick(View view) {
AppDatabase db = Room.databaseBuilder(getApplicationContext(),
AppDatabase.class, "database-name").build();
WordsDao wordsDao = db.wordsDao();
List<Words> words = wordsDao.getAll();
Log.w("asd", "ChangeTextClick: " + words);
}
}
这个错误是我造成的还是文档造成的?代码是旧的吗?我做错了什么吗?
3条答案
按热度按时间stszievb1#
我通过降低版本来解决问题:
pes8fvy92#
对于那些有同样问题的人来说,这是由于同时使用
annotationProcessor "android.arch.persistence.room:compiler:1.1.1"
和kapt "android.arch.persistence.room:compiler:1.1.1"
,只需删除一个。ldfqzlk83#
在我的例子中,我没有依赖问题,但是有同样的错误。我
just deleted all debug app files and rebuild the code again
,它对我有效。