Gea*_*van 9 java android kotlin
正如标题所示,我在“var myNote = Note(id, title, note, ServerValue.TIMESTAMP)”错误“Required:String Found:String?”这一行的“id”下得到一个红色下划线。Kotlin 和 Android Studio
class MainActivity : AppCompatActivity() {
var mRef:DatabaseReference? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val database = FirebaseDatabase.getInstance()
mRef = database.getReference("Notes")
add_new_note.setOnClickListener {
showDialogAddNote()
}
}
fun showDialogAddNote() {
val alertBuilder = AlertDialog.Builder(this)
val view = layoutInflater.inflate(R.layout.add_note, null)
alertBuilder.setView(view)
val alertDialog = alertBuilder.create()
alertDialog.show()
view.btnSaveNote.setOnClickListener {
val title = view.etTitle.text.toString()
val note = view.etNote.text.toString()
if (title.isNotEmpty() && note.isNotEmpty()) {
var id = mRef!!.push().key
var myNote = Note(id, title, note, ServerValue.TIMESTAMP)
mRef!!.child(id).setValue(myNote)
alertDialog.dismiss()
} else {
Toast.makeText(this, "Empty", Toast.LENGTH_LONG).show()
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的 Notes.kt 课程
package com.example.gearoidodonovan.books
import java.util.*
class Note (var id:String, var title:String, var note:String, var timestamp: MutableMap<String, String>) {
}
Run Code Online (Sandbox Code Playgroud)
Ell*_*otM 10
Kotlin 迫使您对可空性保持高度警惕。
您的Note实体说它需要一个 non-nullable id:String,并且显然mRef!!.push().key返回一个String?意思,它是一个可为空的字符串。你可以通过双击来解决这个问题,即mReff!!.push().key!!
另一个提示是ALT+ENTER这些与 Kotlin 相关的错误,它会为您提供双重打击。
| 归档时间: |
|
| 查看次数: |
12781 次 |
| 最近记录: |