【发布时间】:2021-01-04 02:54:44
【问题描述】:
我尝试制作我的第一个应用程序,但我收到了错误消息。错误信息是:
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/com.ibrahimkiceci.simplynoteapp E/AndroidRuntime: 致命异常: main 进程:com.ibrahimkiceci.simplynoteapp,PID:30034 java.lang.NullPointerException: null 不能转换为非 null 类型 kotlin.String 在 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(未知来源:6) 在 com.google.firebase.firestore.core.AsyncEventListener.lambda$onEvent$0(AsyncEventListener.java:42) 在 com.google.firebase.firestore.core.AsyncEventListener$$Lambda$1.run(未知来源:6) 在 android.os.Handler.handleCallback(Handler.java:883) 在 android.os.Handler.dispatchMessage(Handler.java:100) 在 android.os.Looper.loop(Looper.java:214) 在 android.app.ActivityThread.main(ActivityThread.java:7356) 在 java.lang.reflect.Method.invoke(本机方法) 在 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
当我点击我的登录按钮时,应用程序关闭,我共享代码屏幕,
我该如何解决?谢谢!
类 ListViewActivity : AppCompatActivity() { 私有lateinit var auth:FirebaseAuth 私有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()
}
}
}
}
}
}
}
【问题讨论】:
-
你能把堆栈跟踪或行号粘贴到问题出现的地方吗?您也可以检查自己,如果此代码 sn-p 中的任何字符串值是否为 null,如果为 null,则在变量声明中添加
?运算符。 -
我粘贴了堆栈跟踪,我还检查了所有的 null 和?操作员,但看起来没问题,否则我错过了一些东西
-
查看行号
114,那边写的是什么? -
当我点击时,它会转到 Query 并向我显示以下代码: QuerySnapshot querySnapshot = new QuerySnapshot(this, snapshot, firestore); userListener.onEvent(querySnapshot, null); };
-
如果没有,您是否使用某些IDE进行开发,然后在记事本中打开或其他显示行号的IDE,您在第114行有错误,您能告诉那行是什么吗?从代码中并不清楚确切的行是什么。