问题描述
我正在尝试在Firebase数据库中增加一个整数,但是该应用程序崩溃了.
I am trying to increment an integer in Firebase database but, the app crashes.
abstract class BrowserActivity : ThemableBrowserActivity(),OnClickListener {
private var mAuth: FirebaseAuth? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
BrowserApp.appComponent.inject(this)
setContentView(R.layout.activity_main)
incrementId() //calling the function increment id
}
protected fun incrementId() {
val cUser = mAuth?.currentUser
val uid: String = cUser!!.uid
var databaseReference = FirebaseDatabase.getInstance().getReference("users").child(uid).child("id")
databaseReference!!.runTransaction(object : Transaction.Handler {
override fun doTransaction(mutableData: MutableData): Transaction.Result {
var count = mutableData.getValue(Int::class.java)
if (count == null) {
mutableData.setValue(1)
} else {
mutableData.setValue(count+1)
}
return Transaction.success(mutableData)
}
override fun onComplete(databaseError: DatabaseError, b: Boolean, dataSnapshot: DataSnapshot) {}
})
}
}
Logcat:
数据库结构:
my-firebase-app-name-9c826
|__-users
|_-FZTuHfNiXoSWOLiNlSv9w1vVZj03 //uid: user id of current user
|__id: 1
我的Firebase规则:`我的Firebase规则:
My Firebase rules:`My Firebase rules:
{ "rules": { ".read": true, ".write": true }}
{ "rules": { ".read": true, ".write": true }}
推荐答案
参考帖子,我将databaseError
作为null
传递给了onComplete
.默认情况下,kotlin中的所有变量都不为null.因此,如果我想传递null
参数,我必须在参数中添加?
,在我的情况下应为DatabaseError?
.因此onComplete
函数应如下所示:
With reference to this post, I am passing databaseError
as null
to onComplete
.By default all variables in kotlin are non null. So, if I want to pass a null
parameter,I have to add a ?
to the parameter, which in my case should be DatabaseError?
. So the onComplete
function should look like:
override fun onComplete(databaseError: DatabaseError?, b: Boolean, dataSnapshot: DataSnapshot) {}
这篇关于错误:java.lang.IllegalArgumentException:指定为非null的参数为空Firebase事务Kotlin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!