未解析的引用:我的Kotlin程序中Firebase实时数据库的数据库



我正在尝试在Kotlin中使用Firebase实时数据库。我遵循本教程,但我认为有些东西是错误的,因为我得到一个错误编译之前,当我想检索数据库的实例(未解析的引用DataBasereference(或数据库,如果我取消注释的第一行函数tryToRead)。

这是代码:

MainActivity.kt


package com.example.spesapp
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.google.firebase.ktx.Firebase
class MainActivity : AppCompatActivity() {
private lateinit var database: DataBasereference //Here I see "Unresolved reference"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

}
fun tryToRead(){
//val database = Firebase.database
val myref = database.getReference
}
}

Gradle(模块级)

plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'com.google.gms.google-services'
}
android {
compileSdk 32
defaultConfig {
applicationId "com.example.frangelapp"
minSdk 21
targetSdk 32
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
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 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
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'

testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

Gradle(项目级)

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
// Check that you have the following line (if not, add it):
google()  // Google's Maven repository
}
dependencies {
// Add this line
classpath 'com.google.gms:google-services:4.3.10'
}
}
plugins {
id 'com.android.application' version '7.1.1' apply false
id 'com.android.library' version '7.1.1' apply false
id 'org.jetbrains.kotlin.android' version '1.6.10' apply false
}
task clean(type: Delete) {
delete rootProject.buildDir
}

我不知道是什么错误。教程看起来很简单,但我正面临着这个问题。提前感谢

你会得到以下错误:

未解析的引用databaserefence

因为你有而不是在项目中添加了实时数据库依赖项。要解决这个问题,只需添加:
implementation 'com.google.firebase:firebase-database-ktx'

在构建中。gradle文件。

相关内容

最新更新