firebase Android房间问题:创建时受保护的空值(支持SQLiteDatabase_db)

fumotvh3  于 2023-03-19  发布在  Android
关注(0)|答案(3)|浏览(131)

这是错误代码

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);
      }
    }
  }

This is the list I followed

文件夹布局

📦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);
    }
}

这个错误是我造成的还是文档造成的?代码是旧的吗?我做错了什么吗?

stszievb

stszievb1#

我通过降低版本来解决问题:

implementation 'androidx.room:room-runtime:2.2.2'
annotationProcessor "android.arch.persistence.room:compiler:1.1.1"
kapt "android.arch.persistence.room:compiler:1.1.1"
kapt 'androidx.room:room-compiler:2.2.2'
// optional - RxJava2 support for Room
implementation 'androidx.room:room-rxjava2:2.2.2'
// optional - RxJava3 support for Room
implementation 'androidx.room:room-rxjava3:2.4.2'
// optional - Guava support for Room, including Optional and ListenableFuture
implementation 'androidx.room:room-guava:2.2.2'
// optional - Test helpers
testImplementation 'androidx.room:room-testing:2.2.2'
// optional - Paging 3 Integration
implementation 'androidx.room:room-paging:2.4.2'
pes8fvy9

pes8fvy92#

对于那些有同样问题的人来说,这是由于同时使用annotationProcessor "android.arch.persistence.room:compiler:1.1.1"kapt "android.arch.persistence.room:compiler:1.1.1",只需删除一个。

ldfqzlk8

ldfqzlk83#

在我的例子中,我没有依赖问题,但是有同样的错误。我just deleted all debug app files and rebuild the code again,它对我有效。

相关问题