1

this is my first time asking question on stackoverflow.

I'll just go straight to the point, I'm currently developing an application which involve creating a chat room using firebase, I found this

https://www.youtube.com/watch?v=uX6_w6yhj4E

and decided to follow him. Everything was going smoothly until i finish the code and tries to run it. Everything functioned as expected except the send button that was suppose to display my message in the chat room. My send button didn't respond even though i tap on it. I can't seem to find the problem.

Here is my code for the chat_room:

public class Chat_Room extends AppCompatActivity {

    private Button btn_send_msg;
    private EditText input_msg;
    private TextView chat_conversation;

    private String user_name, room_name;
    private DatabaseReference root;
    private String temp_key;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.chat_room);

        btn_send_msg = (Button) findViewById(R.id.btn_send);
        input_msg = (EditText) findViewById(R.id.msg_input);
        chat_conversation = (TextView) findViewById(R.id.textview);

        user_name = getIntent().getExtras().get("user_name").toString();
        room_name = getIntent().getExtras().get("room_name").toString();
        setTitle(" Room - "+room_name);

        root = FirebaseDatabase.getInstance().getReference().child(room_name);

        btn_send_msg.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {

                Map<String,Object> map = new HashMap<String, Object>();
                temp_key = root.push().getKey();
                root.updateChildren(map);

                DatabaseReference message_root = root.child(temp_key);
                Map<String, Object> map2 = new  HashMap<String, Object>();
                map2.put("name",user_name);
                map2.put("msg",input_msg.getText().toString());

                //message_root.updateChildren(map2);
                message_root.setValue(map2);

            }
        });

        root.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {

            append_chat_conversation(dataSnapshot);
            }

            @Override
            public void onChildChanged(DataSnapshot dataSnapshot, String s) {

                append_chat_conversation(dataSnapshot);
            }

            @Override
            public void onChildRemoved(DataSnapshot dataSnapshot) {

            }

            @Override
            public void onChildMoved(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

    }

    private String chat_msg, chat_user_name;

    private void append_chat_conversation(DataSnapshot dataSnapshot){

        Iterator i = dataSnapshot.getChildren().iterator();

        while (i.hasNext()){
            chat_msg = (String) ((DataSnapshot)i.next()).getValue();
            chat_user_name = (String) ((DataSnapshot)i.next()).getValue();

            chat_conversation.append(chat_user_name +" : "+chat_msg+"\n");
        }
    }
}

Let me know if I did something wrong or if the information provided was not enough, really appreciate for the help as I'm still a rookie in both android studio and firebase. Thanks in advance.

Zephyros
  • 77
  • 7

1 Answers1

0

Welcome to stack!! :)

Update only updates data, if there is not data to update in the database then nothing will happen.
Your first update will do nothing.

Map<String,Object> map = new HashMap<String, Object>();
            temp_key = root.push().getKey();
            root.updateChildren(map);

The map is empty and nothing will be updated because, well, the map is empty.

The second update, will also do nothing and this is why

 DatabaseReference message_root = root.child(temp_key);
            Map<String, Object> map2 = new  HashMap<String, Object>();
            map2.put("name",user_name);
            map2.put("msg",input_msg.getText().toString());

            message_root.updateChildren(map2);

Here you made a reference to the child with the push key. Push keys are unique and no two push keys will ever be the same. So when you make a databaseReference to the 'message_root'. That Database reference does not exist at that point. So the attributes, "name" and "msg" do not exist in the database reference and therefore cannot be updated.

That is why you see nothing. To fix it, try this

    //message_root.updateChildren(map2);

    message_root.setValue(map2);

Instead of updating the message_root. Set the value instead. You have to set the value because the message_root does not exist and by calling setting value you set its existence if that makes sense.

[EDIT]

Try confirming that at least something can be posted to your database. In your on create method create a map and push that map to your database.

If nothing is reflected, I can only assume that one of two things has happened. You have set up Firebase incorrectly(have you added your google-services json) or your security rulesare set to only alound writes from authenticated users.Make sure your security rules are open. I think the security rules are the most likely cause.

Go to the Firebase console. Go to the database and click on Rules. Make sure your rules look like this.


    {
      "rules": {

        ".read": true,
        ".write": true

      } 
    }

Copy and pase those rules and post them in directly.

Umar Karimabadi
  • 960
  • 8
  • 20
  • Still the same problem :( – Zephyros Mar 05 '17 at 01:17
  • Updated, i only changed the updateChildren to setValue like you suggested. – Zephyros Mar 05 '17 at 12:04
  • One second, I will be with you in a bit : ) – Umar Karimabadi Mar 05 '17 at 13:11
  • Does your database update at all when you press the button. Or is nothing reflected on your database. I am sure something is being posted to your database. If nothing is being posted on your database then please post a comment. – Umar Karimabadi Mar 05 '17 at 13:37
  • Sorry for the late reply, I've confirmed that I've added the json file and the rules has been changed to 'true' before. And for some reason, I couldn't run my app anymore even though I didn't touch a single thing. It gives me this error: Error:Execution failed for task ':app:transformClassesWithDexForDebug'. > com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.dex.DexException: Multiple dex files define Lcom/google/android/gms/internal/zzafl; – Zephyros Mar 06 '17 at 15:38
  • I will redo the app again tomorrow to see if I really missed something although I was just following every steps from the video. Sorry for the inconvenience caused. >. – Zephyros Mar 06 '17 at 15:43
  • Hi, I've decided to not include the chat feature, so there's no need to answer this question. Again sorry for the inconvenience and thank you for the awesome response. :) – Zephyros Mar 07 '17 at 10:10