Kotlin中Firebase Realtime Database未解析引用等错误求助
在开发Kotlin Android项目时,使用Firebase Realtime Database遇到以下报错:在RegisterActivity中声明DatabaseReference的代码行提示“Unresolved reference: DatabaseReference”和“Expecting member declaration”错误。已尝试Stack Overflow相关解决方案,也重新连接了Firebase项目,但问题仍未解决。
报错代码
package com.example.movietime import android.content.Intent import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import com.example.movietime.databinding.ActivityRegisterBinding import com.google.firebase.ktx.Firebase class RegisterActivity : AppCompatActivity() { DatabaseReference database = FirebaseDatabase.getInstance().getReference(); // also here with all sentence // binding private lateinit var binding: ActivityRegisterBinding override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = ActivityRegisterBinding.inflate(layoutInflater) setContentView(binding.root) // move to login after click binding.submitButton.setOnClickListener { startActivity(Intent(this, LoginActiivty::class.java)) } } }
App模块Gradle配置
plugins { id 'com.android.application' id 'org.jetbrains.kotlin.android' id 'com.google.gms.google-services' } android { namespace 'com.example.movietime' compileSdk 33 defaultConfig { applicationId "com.example.movietime" minSdk 21 targetSdk 33 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildFeatures { viewBinding true } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } kotlinOptions { jvmTarget = '1.8' } } dependencies { implementation 'com.google.firebase:firebase-database-ktx' implementation 'androidx.core:core-ktx:1.9.0' implementation 'androidx.appcompat:appcompat:1.5.1' implementation 'com.google.android.material:material:1.7.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' implementation platform('com.google.firebase:firebase-bom:30.0.1') implementation 'com.google.firebase:firebase-analytics-ktx' implementation 'com.google.firebase:firebase-auth-ktx' implementation 'com.google.firebase:firebase-firestore-ktx' implementation 'com.google.firebase:firebase-firestore' implementation platform('com.google.firebase:firebase-bom:31.0.2') testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.3' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' }
项目级Gradle配置
buildscript { repositories { // Check that you have the following line (if not, add it): google() // Google's Maven repository } dependencies { classpath 'com.android.tools.build:gradle:7.3.1' classpath 'com.google.gms:google-services:4.3.14' } } // Top-level build file where you can add configuration options common to all sub-projects/modules. plugins { id 'com.android.application' version '7.3.1' apply false id 'com.android.library' version '7.3.1' apply false id 'org.jetbrains.kotlin.android' version '1.7.20' apply false }
解决建议
补全必要导入:代码中未导入
DatabaseReference和Firebase数据库相关类,需在文件顶部添加:import com.google.firebase.database.DatabaseReference import com.google.firebase.database.ktx.database修正Kotlin变量声明语法:原代码使用Java风格的变量声明,不符合Kotlin语法。可改为:
private lateinit var database: DatabaseReference或直接初始化:
private val database = Firebase.database.reference清理Gradle依赖冲突:App模块中重复引入了两个不同版本的Firebase BOM(30.0.1和31.0.2),需删除其中一个,保留最新版本的BOM确保依赖统一。建议删除
implementation platform('com.google.firebase:firebase-bom:30.0.1')这一行。同步并重建项目:修改依赖后点击Android Studio右上角「Sync Now」同步Gradle,再执行「Build -> Clean Project」和「Build -> Rebuild Project」,确保依赖正确加载。
修正类名拼写错误:代码中
LoginActiivty多了一个字母i,应改为LoginActivity,避免后续跳转功能异常。
内容的提问来源于stack exchange,提问作者Adir

