【发布时间】:2020-06-03 15:18:34
【问题描述】:
我在将意图值传递给下一个活动时遇到问题。
在创建配置文件时,我创建了两个数据库引用 1. 带有 pushid 2. 没有 push id
现在我正在尝试使用 pushid 从 databaseref 传递值
我的代码是这样的
mDatabaseReference = mDatabase!!.reference.child("Tokens").child(userId).push()
为了存储 push id 键值,我使用了下面的变量
var tokenID: String = mDatabaseReference!!.key!!
为了将这个值推送到意图,我使用了
intent.putExtra(TOKENID,tokenID)
当我使用日志时,我可以看到推送 id 值
我在初始活动中使用伴随对象创建的 TOKENID
将此推送 id 值传递给下一个活动
tokenreq = intent.getStringExtra(Profile.TOKENID)
检索我在数据库引用下使用的值
mDatabaseReference = mDatabase!!.reference.child("Tokens").child(userId).child(tokenreq!!)
当我运行程序时,应用程序崩溃,在日志数据中提到 KotlinNullPointerException for tokenreq
日志文件
kotlin.KotlinNullPointerException
at com.luvpi.luvproject.activities.Verification$onCreate$2.onClick(Verification.kt:96)
at android.view.View.performClick(View.java:5612)
at android.view.View$PerformClick.run(View.java:22288)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6123)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
完整的流程代码
checkcode.setOnClickListener {
val userId = FirebaseAuth.getInstance().currentUser!!.uid
mDatabase = FirebaseDatabase.getInstance()
Log.d(TAG,"Push id is $tokenrequest")
mDatabaseReference = mDatabase!!.reference.child("Tokens").child(userId).child(tokenrequest!!)
mDatabaseReference!!.addListenerForSingleValueEvent(object :ValueEventListener{
override fun onCancelled(databaseError: DatabaseError) {
}
override fun onDataChange(dataSnapshot: DataSnapshot) {
var tokenkey = dataSnapshot.child("tokenid").value.toString()
Log.d(TAG,"Verification Code is $tokenkey")
if (tokenkey == userverify.toString()) {
Toast.makeText(this@Verification, "Successfully Verified", Toast.LENGTH_LONG)
.show()
startActivity(Intent(this@Verification, LatestMessage::class.java))
} else {
Toast.makeText(this@Verification, "Verification code is incorrect", Toast.LENGTH_LONG).show()
return
}
}
})
}
【问题讨论】:
-
请发布你的日志
-
TOKENID可能有问题
-
我会在调用 intent.putExtra(TOKENID,tokenID) 指令之前检查 tokenID 值
-
请添加整个错误及其发生的行。
-
附加日志信息
标签: android kotlin firebase-realtime-database