尝试在firebase上添加新的子对象时,android应用程序冻结

尝试在firebase上添加新的子对象时,android应用程序冻结,android,firebase,firebase-realtime-database,Android,Firebase,Firebase Realtime Database,我正在使用firebase制作聊天应用程序。我在Childadad侦听器上添加了子事件。当我第一次打开“活动”时,它会检索已正确显示的数据,但当我尝试在“活动”打开时添加子节点时,应用程序会冻结。请帮忙。 这是我的活动代码: public class ChatActivity extends AppCompatActivity { DatabaseReference ref,ref1; TextView t; EditText e; ImageButton b; String contact,u

我正在使用firebase制作聊天应用程序。我在Childadad侦听器上添加了子事件。当我第一次打开“活动”时,它会检索已正确显示的数据,但当我尝试在“活动”打开时添加子节点时,应用程序会冻结。请帮忙。 这是我的活动代码:

public class ChatActivity extends AppCompatActivity {
DatabaseReference ref,ref1;
TextView t;
EditText e;
ImageButton b;
String contact,user;
ArrayList<Chat> chatList;
ListView listView;
ChatAdapter chatAdapter;
String msg;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_chat);
    chatList = new ArrayList<>();
    Intent i = getIntent();
    contact = i.getStringExtra("contact");
    user = i.getStringExtra("user");
    t = findViewById(R.id.textView3);
    e = findViewById(R.id.editText8);
    b = findViewById(R.id.imageButton);
    listView = findViewById(R.id.listView2);
    t.setText(" " + contact);
    ref = FirebaseDatabase.getInstance().getReference().child("Messages").child(user).child(contact);
    ref.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            if (!dataSnapshot.exists()) {
                return;
            }
            Toast.makeText(getApplicationContext(), "onChildAdded of ref", Toast.LENGTH_LONG).show();
            while(!(dataSnapshot.hasChild("type") && dataSnapshot.hasChild("msg") && dataSnapshot.hasChild("time"))){}
            chatList.add(new Chat(dataSnapshot.child("type").getValue(Integer.class), dataSnapshot.child("msg").getValue(String.class), dataSnapshot.child("time").getValue(Long.class)));
            if(chatAdapter!=null)
                chatAdapter.notifyDataSetChanged();
        }

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

        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

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

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

    b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            msg = e.getText().toString();
            ref = FirebaseDatabase.getInstance().getReference().child("Messages").child(user).child(contact);
            ref1 = FirebaseDatabase.getInstance().getReference().child("Messages").child(contact).child(user);
            ref = ref.push();
            ref1 = ref1.push();
            ref.child("type").setValue(1);
            ref1.child("type").setValue(2);
            ref.child("msg").setValue(msg);
            ref1.child("msg").setValue(msg);
            ref.child("time").setValue(ServerValue.TIMESTAMP);
            ref1.child("time").setValue(ServerValue.TIMESTAMP);
        }
    });
    chatAdapter = new ChatAdapter(this, chatList);
    listView.setAdapter(chatAdapter);
}
}

删除此行:
而(!(dataSnapshot.hasChild(“type”)和&dataSnapshot.hasChild(“msg”)和&dataSnapshot.hasChild(“time”){}
不工作。给出此错误:
java.lang.NullPointerException:尝试在com.example.user.firebasetest1.ChatActivity$1.onChildaded(ChatActivity.java:61)上对空对象引用调用虚拟方法“long java.lang.long.longValue()”
当你修复一个错误并得到一个新的错误时总是很可爱:)while循环是你的应用程序冻结的原因,因为它永远不会退出。也许你可以把它改成某种if语句。我添加了while循环来尝试删除它时的错误。我认为,如果我放置while循环,它只会在子节点完全添加时继续,因此不会给出空指针错误。我不知道是什么原因导致空指针错误。
public class ContactAdapter extends ArrayAdapter<Contact> {
private Context context;
private List<Contact> contactList = new ArrayList<>();
public ContactAdapter(@NonNull Context context, ArrayList<Contact> list) {
    super(context, 0 , list);
    this.context = context;
    contactList = list;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    View listItem = convertView;
    if(listItem == null)
        listItem = LayoutInflater.from(context).inflate(R.layout.contact_item,parent,false);

    final Contact currentContact = contactList.get(position);

    TextView name = (TextView) listItem.findViewById(R.id.textView);
    name.setText("   " + currentContact.getUser());

    TextView release = (TextView) listItem.findViewById(R.id.textView2);
    release.setText(" " + currentContact.getLastMsg());

    return listItem;
}
}
public class Chat {
private int type;
private String msg;
private long time;
public Chat(int type,String msg,long time){this.type = type; this.msg = msg; this.time = time;}
public int getType(){return type;}
public void setType(int type){this.type = type;}
public String getMsg(){return msg;}
public void setMsg(String msg){this.msg = msg;}
public long getTime(){return time;}
public void setTime(long time){this.time = time;}
}
  {
 "Messages" : {
   "pranavk28" : {
     "sunilk26" : {
       "-LFRGvG86BpKNYBONRKX" : {
         "msg" : "hii",
         "time" : 1529482819019,
         "type" : 1
       },
       "-LFRH3nTMYv-QcqXkFFb" : {
         "msg" : "how are you?",
         "time" : 1529482858064,
         "type" : 1
       },
       "-LFRIWJvmACC61niuN07" : {
         "msg" : "How was your day?",
         "time" : 1529483237050,
         "type" : 1
       }
     }
   },
   "sunilk26" : {
     "pranavk28" : {
       "-LFRGvG9JJcpprSKz8YB" : {
         "msg" : "hii",
         "time" : 1529482819020,
         "type" : 2
       },
       "-LFRH3nTMYv-QcqXkFFc" : {
         "msg" : "how are you?",
         "time" : 1529482858064,
         "type" : 2
       },
       "-LFRIWJwEJ_ypA1zeBXt" : {
         "msg" : "How was your day?",
         "time" : 1529483237051,
         "type" : 2
       }
     }
   }
 },
 "Users" : {
   "pranavk28" : {
     "email" : "pranavk28@gmail.com",
     "name" : "Pranav Bhardwaj",
     "password" : "priyamkr",
     "username" : "pranavk28"
   },
   "rockykbc" : {
      "email" : "rocky6@hotmail.com",
      "name" : "Ravi Kumar",
      "password" : "neetukbc",
      "username" : "rockykbc"
    },
    "sunilk26" : {
      "email" : "thakur@gmail.com",
      "name" : "D K Thakur",
      "password" : "pass1234",
      "username" : "sunilk26"
    }
  }
}