You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Kotlin开发Firestore引用报错:enteredText未初始化如何修复

报错根因

你遇到的报错有两层问题:

  1. 语法层面不符合Kotlin规则:Kotlin要求非空类型的局部变量必须在声明时就完成初始化,你只写了var enteredText: String没有赋初始值,所以编译器直接标红报错。
  2. 业务逻辑顺序错误:你当前的Firestore查询代码是写在onCreate生命周期里,Activity一启动就会执行,这时候用户还没有点击按钮选择输入内容,enteredText根本还没被赋值,就算语法不报错,运行逻辑也完全不对。

修复方案

直接把Firestore查询逻辑移动到按钮的点击回调里即可,无需提前声明enteredText变量,还可以加一层空值判断避免用户未输入就点击按钮的异常:

package com.example.servus

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.TextView
import com.google.firebase.firestore.ktx.firestore
import com.google.firebase.ktx.Firebase

import android.view.View
import android.widget.ArrayAdapter
import android.widget.AutoCompleteTextView
import android.widget.Button
import android.widget.Toast


class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val autotextView
                = findViewById<AutoCompleteTextView>(R.id.autoTextViewStart)
        val autotextViewZiel
                = findViewById<AutoCompleteTextView>(R.id.autoTextViewZiel)


        // Get the array of languages
        val languages
                = resources.getStringArray(R.array.Languages)
        // Create adapter and add in AutoCompleteTextView
        val adapter
                = ArrayAdapter(this,
            android.R.layout.simple_list_item_1, languages)
        autotextView.setAdapter(adapter)
        autotextViewZiel.setAdapter(adapter)

        val value1= findViewById(R.id.value1) as TextView
        val value2= findViewById(R.id.value2) as TextView
        val value3= findViewById(R.id.value3) as TextView
        val db = Firebase.firestore

        val button
                = findViewById<Button>(R.id.btn); if (button != null)
        {
            button.setOnClickListener(View.OnClickListener {
                val enteredText = autotextView.text.toString()
                // 增加空值判断避免异常
                if (enteredText.isBlank()) {
                    Toast.makeText(this@MainActivity, "请先选择内容", Toast.LENGTH_SHORT).show()
                    return@OnClickListener
                }
                Toast.makeText(this@MainActivity, enteredText, Toast.LENGTH_SHORT).show()
                
                // 将Firestore查询逻辑移到这里,拿到用户输入后再执行
                val docRef = db.collection("storage").document(enteredText)
                docRef.get()
                    .addOnSuccessListener { document ->
                        if (document !=null) {
                            Log.d("exist", "DocumentSnapshot data: ${document.data}")
                            value1.text = document.getString("value1")
                            value2.text = document.getString("value2")
                            value3.text = document.getString("value3")
                        } else {
                            Log.d("noexist", "No such docoument")
                            Toast.makeText(this@MainActivity, "对应文档不存在", Toast.LENGTH_SHORT).show()
                        }
                    }
                    .addOnFailureListener { exception ->
                        Log.w("notexisting", "Error getting documents.", exception)
                        Toast.makeText(this@MainActivity, "查询失败:${exception.message}", Toast.LENGTH_SHORT).show()
                    }
            })
        }
    }
}

内容的提问来源于stack exchange,提问作者CSBossmann

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 13:48:04