Only safe call allowed error when EditText is used to send string to firebase in android studio

huangapple go评论68阅读模式
英文:

Only safe call allowed error when EditText is used to send string to firebase in android studio

问题

我是新手使用 Firebase。我创建了一个简单的项目,该项目将用户输入的字符串发送到 Firebase 中的实时数据库。我使用了 EditText 视图来接收输入。以下是我的主要活动。username 是 EditText 视图的 ID。

package com.example.firebase
import...

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding
    private lateinit var database: DatabaseReference
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        binding.Remote.setOnClickListener {

            val name = binding.username.text.toString() //此行出现错误
            database = FirebaseDatabase.getInstance().getReference("Users")
            val User = User(name)
            database.child(name).setValue(User).addOnSuccessListener {
                binding.username.text.clear() //以及此行
                Toast.makeText(this, "成功", Toast.LENGTH_SHORT).show()
            }.addOnFailureListener {
                Toast.makeText(this, "失败", Toast.LENGTH_SHORT).show()
            }
        }
    }
}

构建后,在代码的注释行上显示以下错误:

只允许在 EditText 类型的可空接收器上进行安全 (?.) 或非空断言 (!!.) 调用。

如果我直接输入字符串而不使用 EditText 视图,则正常运行。有人能帮我解决这个问题吗?

英文:

I am new to Firebase. I have made a simple project which sends user strings to Realtime Database in Firebase. For input, I have used the EditText view. Here is my main activity. username is the id of EditText View

package com.example.firebase
import...

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding
    private lateinit var database: DatabaseReference
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        binding.Remote.setOnClickListener {


            val name = binding.username.text.toString() //Error on this line
            database = FirebaseDatabase.getInstance().getReference("Users")
            val User = User(name)
            database.child(name).setValue(User).addOnSuccessListener {
                binding.username.text.clear() //And This Line
                Toast.makeText(this,"Sucess", Toast.LENGTH_SHORT).show()
            }.addOnFailureListener {
                Toast.makeText(this,"Failed", Toast.LENGTH_SHORT).show()
            }
        }

After Building following error is shown on the commented line of code:

> Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type EditText.

It works fine if I direct enter a string instead of using EditText View. Can someone help me with this?

答案1

得分: 2

只翻译代码部分:

  1. Use safe call (?.)
val name = binding.username?.text.toString()
  1. Use a non-null asserted (!!.)
val name = binding.username!!.text.toString()
英文:

It looks like your username EditText object is null. So there are two ways in which you solve this problem:

  1. Use safe call (?.)

     val name = binding.username?.text.toString()
     //                         👆
    
  2. Use a non-null asserted (!!.)

     val name = binding.username!!.text.toString()
     //                         👆
    

See more info inside the Kotlin documentation regarding null safety.

答案2

得分: 0

你有不同版本的布局文件吗(例如,纵向/横向,小屏幕,不同的API级别等)?检查所有这些版本中是否都有一个ID为“username”的EditText - 如果在某些变体中缺少它,生成的绑定类将使该属性可为空,因为它可能不存在(取决于充气哪个版本的布局文件)。

英文:

Do you have different versions of that layout file (e.g. portrait/landscape, small screen, different API level etc)? Check you have an EditText with an ID of username in all of them - if it's missing from some variations, the generated binding class makes that property nullable, since it might not be present (depending on which version of the layout file was inflated)

huangapple
  • 本文由 发表于 2023年5月29日 19:27:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76356934.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定