[英]Android:Kotlin TypeCastException: null cannot be cast to non-null type kotlin.String
[英]FATAL EXCEPTION java.lang.NullPointerException: null cannot be cast to non-null type kotlin.String
我尝试制作我的第一个应用程序,但我收到了错误消息。 错误信息是:
2020-09-17 11:18:17.846 30034-30132/com.ibrahimkiceci.simplynoteapp W/i.simplynoteap: Accessing hidden method Lsun/misc/Unsafe;->getInt(Ljava/lang/Object;J)I (greylist, linking, allowed)
2020-09-17 11:18:18.017 30034-30034/com.ibrahimkiceci.simplynoteapp D/AndroidRuntime:关闭 VM 2020-09-17 11:18:18.020 30034-30034-30034-30034-30034-30034-30034-com.例外:主进程:com.ibrahimkiceci.simplynoteapp,PID:30034 java.lang.NullPointerException:在 com.ibrahimkiceci.simplynoteapp.ListViewActivity$getDataFromFirestore$1.onEvent(ListViewActivity.kt 114) 在 com.ibrahimkiceci.simplynoteapp.ListViewActivity$getDataFromFirestore$1.onEvent(ListViewActivity.kt:19) 在 com.google.firebase.firestore.Query.lambda$addSnapshotListenerInternal$2(Query.java:1142) 在 com.google.firebase .firestore.Query$$Lambda$3.onEvent(Unknown Source:6) at com.google.firebase.firestore.core.AsyncEventListener.lambda$onEvent$0(AsyncEventListener.java:42) at com.google.firebase.firestore.core .AsyncEventListener$$Lambda$1.run(Unknown Source:6) at android.os.Handler.handleCallback(Handler.java:883) at android.os.Handler.di spatchMessage(Handler.java:100) at android.os.Looper.loop(Looper.java:214) at android.app.ActivityThread.main(ActivityThread.java:7356) at java.lang.reflect.Method.invoke(Native)方法)在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 2020-09-17 11:18 :18.086 30034-30034/com.ibrahimkiceci.simplynoteapp I/Process: 发送信号。 PID:30034 SIG:9
当我点击我的登录按钮时,应用程序关闭,我分享了代码屏幕,
我该如何解决? 谢谢!
class ListViewActivity : AppCompatActivity() { private lateinit var auth: FirebaseAuth private lateinit var db : FirebaseFirestore
var titleTextFromFB : ArrayList<String> = ArrayList()
var imageFromFB : ArrayList<String> = ArrayList()
var adapter: NoteAdapter? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_list_view)
auth = FirebaseAuth.getInstance()
db = FirebaseFirestore.getInstance()
getDataFromFirestore()
// recyclerview
var layoutManager = LinearLayoutManager(this)
recyclerView.layoutManager = layoutManager
adapter = NoteAdapter(titleTextFromFB, imageFromFB)
recyclerView.adapter = adapter
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
val menuInflater = menuInflater
menuInflater.inflate(R.menu.add_note, menu)
return super.onCreateOptionsMenu(menu)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
if (item.itemId == R.id.add_note) {
// Take Notes Activity
val intent = Intent(applicationContext, TakeNotesActivity::class.java)
startActivity(intent)
} else if (item.itemId == R.id.log_out) {
val alert = AlertDialog.Builder(this)
alert.setTitle("Log out")
alert.setMessage("Are you sure to logout from the app ?")
alert.setPositiveButton("Yes") {dialog, which ->
auth.signOut()
val intent = Intent(applicationContext, MainActivity::class.java)
startActivity(intent)
finish()
}
alert.setNegativeButton("No") {dialog, which ->
}
alert.show()
}
return super.onOptionsItemSelected(item)
}
// get data from firestore
fun getDataFromFirestore() {
db.collection("Notes").orderBy("date", Query.Direction.DESCENDING).addSnapshotListener { snapshot, exception ->
if (exception != null) {
// If there is a error ,
Toast.makeText(applicationContext, exception.localizedMessage.toString(), Toast.LENGTH_LONG).show()
} else {
if (snapshot != null) {
if (!snapshot.isEmpty) {
val documents = snapshot.documents
for (document in documents) {
val userEmail = document.get("userEmail") as String
val noteTitle = document.get("noteTitle") as String
val yourNote = document.get("yourNote") as String
val downloadUrl = document.get("downloadUrl") as String
val timestamp = document.get("date") as Timestamp
val date = timestamp.toDate()
titleTextFromFB.add(noteTitle)
imageFromFB.add(downloadUrl)
adapter!!.notifyDataSetChanged()
}
}
}
}
}
}
}
据我了解,您的错误是由此代码块生成的:
val userEmail = document.get("userEmail") as String
val noteTitle = document.get("noteTitle") as String
val yourNote = document.get("yourNote") as String
val downloadUrl = document.get("downloadUrl") as String
这里document
一个变量是 null,它不能在 kotlin 中转换为不可为 null 的String
类型。
如果您不确定哪些字段可以为空,请编写如下代码:
val userEmail : String? = document.get("userEmail") as? String
val noteTitle : String? = document.get("noteTitle") as? String
val yourNote : String? = document.get("yourNote") as? String
val downloadUrl : String? = document.get("downloadUrl") as? String
为了更清楚,请检查 kotlin 文档中的Safe(nullable)-Cast
运算符
在你的 for 循环中
for (document in documents) {
val userEmail = document.get("userEmail") as String
val noteTitle = document.get("noteTitle") as String
val yourNote = document.get("yourNote") as String
val downloadUrl = document.get("downloadUrl") as String
val timestamp = document.get("date") as Timestamp
val date = timestamp.toDate()
titleTextFromFB.add(noteTitle)
imageFromFB.add(downloadUrl)
adapter!!.notifyDataSetChanged()
}
一个(或多个)字段为空,而您正试图将它们转换为非空 String 对象! 所以在这种情况下,kotlin 会抛出一个运行时异常。 你应该把它转换成字符串吗? 而不是字符串:像这样:
val field = document.get("field") as String?
或检查其可空性:
val field = if (document.get("field") != null) document.get("field") as String
else ""
当我删除并重新安装我的数据库时,问题解决了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.