Android聊天应用Send Button失效:消息未入库且未显示求助
Hey there! Let's figure out why your messages aren't saving to Firebase or showing up in the chat interface. I've looked over your code and here are the most likely culprits to check:
1. Case-Sensitive Firebase Path Mismatch
This is a super common gotcha with Firebase! Look at your reference paths:
reference1usesMessages/(uppercase M)reference2usesmessages/(lowercase m)
Firebase Realtime Database treats these as two completely separate nodes. When you send a message, you're pushing data to two different locations, but your ChildEventListener is only listening to the uppercase Messages/ node. If the actual data is going to the lowercase messages/ node (or vice versa), your listener will never pick up new messages.
Fix: Standardize the path to use the same case everywhere. For example:
reference1 = new Firebase("https://messageplus-cd647.firebaseio.com/messages/" + UserDetails.phonenumber + "_" + UserDetails.chatWith); reference2 = new Firebase("https://messageplus-cd647.firebaseio.com/messages/" + UserDetails.chatWith + "_" + UserDetails.phonenumber);
2. Firebase Database Security Rules
If your database rules don't allow authenticated users to write data, your messages will fail to save silently.
- Head to your Firebase Console → Realtime Database → Rules
- Make sure your rules allow write access for authenticated users. A basic setup would be:
{ "rules": { "messages": { ".read": "auth != null", ".write": "auth != null" } } }
(Note: For production, you'll want to tighten these rules to only allow access to specific user message pairs, but this is a good starting point for testing.)
3. Add Error Logging to Catch Issues
Your ChildEventListener is missing the onCancelled method, which means you won't see any database errors that might be happening. Add this to debug what's going wrong:
@Override public void onCancelled(DatabaseError databaseError) { Log.e("ChatAppError", "Database operation failed: " + databaseError.getMessage()); }
Also, add debug logs in your send button click listener to confirm your variables are valid:
sendButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String messageText = messageArea.getText().toString(); Log.d("ChatDebug", "Sending message: " + messageText); Log.d("ChatDebug", "From: " + UserDetails.phonenumber + ", To: " + UserDetails.chatWith); if(!messageText.trim().isEmpty()){ // Use trim() to catch empty strings with spaces Map<String, String> map = new HashMap<String, String>(); map.put("message", messageText); map.put("user", UserDetails.phonenumber); reference1.push().setValue(map); reference2.push().setValue(map); messageArea.setText(""); // Clear input after sending } } });
4. Fix ScrollView Scrolling Behavior
Your scrollView.fullScroll(View.FOCUS_DOWN) might run before the new message view is fully added to the layout, so it doesn't scroll to the bottom. Wrap it in a post() call to ensure it runs after the view updates:
public void addMessageBox(String message, int type){ TextView textView = new TextView(Chat.this); textView.setText(message); LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); lp.setMargins(0, 0, 0, 10); textView.setLayoutParams(lp); if(type == 1) { textView.setBackgroundResource(R.drawable.rounded_corner1); } else{ textView.setBackgroundResource(R.drawable.rounded_corner2); } layout.addView(textView); // Fix scrolling scrollView.post(new Runnable() { @Override public void run() { scrollView.fullScroll(View.FOCUS_DOWN); } }); }
5. Verify UserDetails Variables
Double-check that UserDetails.phonenumber and UserDetails.chatWith are not null or empty. If either value is missing, your Firebase paths will be invalid, and messages won't be saved. The debug logs we added earlier will help confirm this.
Start with fixing the path case mismatch first—this is almost certainly the main issue here. Then check the other points if the problem persists!
内容的提问来源于stack exchange,提问作者user9027554

