我正在尝试根据this教程使用Firebase构建实时聊天应用。根据教程,这行代码
mDatabaseReference.child(MESSAGES_CHILD).child(key!!).setValue(message)
应该将消息对象中存在的数据写到数据库中。
一切正常,除了当用户按下发送按钮时,文本没有上载到数据库(但是正在创建密钥,并且当我手动在数据库中手动搜索该密钥时,那里只有null存储)。
这是 ChatActivity.kt
的最小代码private const val TAG = "ChatActivity"
const val MESSAGES_CHILD = "messages"
class ChatActivity : AppCompatActivity() {
private lateinit var mChatAdapter: ChatAdapter
private lateinit var mLinearLayoutManager: LinearLayoutManager
private lateinit var mDatabaseReference: DatabaseReference
private lateinit var mFirebaseAuth: FirebaseAuth
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_chat)
mFirebaseAuth = FirebaseAuth.getInstance()
Log.d(TAG, "onCreate: \n Auth: $mFirebaseAuth")
mDatabaseReference = FirebaseDatabase.getInstance().reference
mLinearLayoutManager = LinearLayoutManager(this)
mChatAdapter = ChatAdapter(options)
//ChatAdapter extends FirebaseRecyclerAdapter
chat.apply { //R.id.chat
setHasFixedSize(true)
layoutManager = mLinearLayoutManager
adapter = mChatAdapter
}
send_button.setOnClickListener {
val message = Message(message_entry.text.trim().toString())
Log.d(TAG, "onCreate: \n $message")
val key = mDatabaseReference.child(MESSAGES_CHILD).push().key
Log.d(TAG, "onCreate: \n Key: $key")
mDatabaseReference.child(MESSAGES_CHILD). child(key!!).setValue(message)
}
}
override fun onPause() {
mChatAdapter.stopListening()
Log.d(TAG, "onPause: ")
super.onPause()
}
override fun onResume() {
super.onResume()
Log.d(TAG, "onResume: ")
mChatAdapter.startListening()
}
}
@IgnoreExtraProperties
data class Message(var text: String?= "", var name: String?= "", var id: String?= "") {
override fun toString(): String {
return "Text: $text \n Name: $name \n Id: $id"
}
}
这是应用程序调试详细信息
2020-10-17 11:18:34.294 5168-5168/com.skb.skara D/ChatActivity: onCreate:
Auth: com.google.firebase.auth.internal.zzn@5a3e651
2020-10-17 11:18:34.338 5168-5168/com.skb.skara D/ChatActivity: onResume:
2020-10-17 11:18:34.389 5168-5168/com.skb.skara D/ChatAdapter: getItemCount: 0
2020-10-17 11:18:34.389 5168-5168/com.skb.skara D/ChatAdapter: getItemCount: 0
2020-10-17 11:18:37.435 5168-5168/com.skb.skara D/ChatAdapter: getItemCount: 0
2020-10-17 11:18:37.435 5168-5168/com.skb.skara D/ChatAdapter: getItemCount: 0
2020-10-17 11:18:42.033 5168-5168/com.skb.skara D/ChatActivity: onCreate:
Text: Hi
Name:
Id:
2020-10-17 11:18:42.033 5168-5168/com.skb.skara D/ChatActivity: onCreate:
Key: -MJp5_ulT4U6cXU9Eu41
2020-10-17 11:18:42.056 5168-5168/com.skb.skara D/ChatAdapter: getItemCount: 1
2020-10-17 11:18:42.071 5168-5168/com.skb.skara D/ChatAdapter: getItemCount: 1
2020-10-17 11:18:42.072 5168-5168/com.skb.skara D/ChatAdapter: getItemCount: 1_
这是我的数据库规则
{
"rules": {
".read": "auth != null",
".write": "auth != null"
}
}
答案 0 :(得分:0)
尝试一下:
def checkOffWindowX(x) :
return x % windowsize[0]
def checkOffWindowY(y):
return y % windowsize[1]
String key = mDatabaseReference.child(MESSAGES_CHILD).push().getKey();
mDatabaseReference.child(MESSAGES_CHILD).child(key).setValue(message);
表示消息的新ID。
更新: 将您的消息对象转换为地图:
key
有关read and write进入实时数据库的更多信息
答案 1 :(得分:0)
今天,我解决了问题中所述的问题。实际的问题不在代码中,而是在我用来运行应用程序的仿真器中。 This链接提到了将Firebase添加到android studio项目的前提条件。在那我忘了验证我的模拟器上是否有Google Play图像。今天,当我再次进行交叉验证的先决条件时,我发现了问题,并在解决了问题之后,现在将数据上传到了Firebase。