Help, SomeBody Help Me..
Here is the database Structure
and Here is my MainActivity onCreate method
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
activity_main = (RelativeLayout) findViewById(R.id.activity_main);
input = (EditText)findViewById(R.id.inputmessage);
fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(this);
mAuth = FirebaseAuth.getInstance();
if(mAuth.getCurrentUser() != null)
{
Toast.makeText(MainActivity.this, "Welcome "+mAuth.getCurrentUser().getEmail(), Toast.LENGTH_SHORT).show();
}
else{
finish();
Intent intent = new Intent(MainActivity.this, Sign_in_form.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
chatlist = new ArrayList<>();
listofMsg = (ListView) findViewById(R.id.list_of_messange);
databaseChat = FirebaseDatabase.getInstance().getReference("chatyoutubemajta");
databaseChat.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
// Toast.makeText(MainActivity.this, "onChildAdded:" + dataSnapshot.getKey(), Toast.LENGTH_SHORT).show();
String id = dataSnapshot.getKey();
ChatMsg chatmsg = dataSnapshot.child(id).getValue(ChatMsg.class);
chatlist.add(chatmsg);
DaftarChat adapter = new DaftarChat(MainActivity.this,chatlist);
listofMsg.setAdapter(adapter);
}
@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) {
}
});
}
Here is Daftarchat class:
public class DaftarChat extends ArrayAdapter<ChatMsg> {
private Activity context;
private List<ChatMsg> daftarchat;
public DaftarChat(Activity context,List<ChatMsg> daftarchat){
super(context,R.layout.list_item,daftarchat);
this.context = context;
this.daftarchat = daftarchat;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View listViewItem = inflater.inflate(R.layout.list_item,null,true);
TextView txttext = (TextView)listViewItem.findViewById(R.id.message_text);
TextView txtuser = (TextView)listViewItem.findViewById(R.id.message_user);
TextView txttime = (TextView)listViewItem.findViewById(R.id.message_time);
ChatMsg sampel = daftarchat.get(position);
txttext.setText(sampel.getMsgText());
txtuser.setText(sampel.getMsgUser());
txttime.setText(sampel.getMsgTime());
return listViewItem;
}
}
The problem is it keeps crashing whenever it starts:
when i remove this statement (inside DaftarChat class) :
txttext.setText(sampel.getMsgText());
txtuser.setText(sampel.getMsgUser());
txttime.setText(sampel.getMsgTime());
it shows nothing, but the program can run, but i still cant retrieve the data.
logcat below:
FATAL EXCEPTION: main
Process: com.example.chatapplication, PID: 4384
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.example.chatapplication.ChatMsg.getMsgText()' on a null object reference
at com.example.chatapplication.DaftarChat.getView
Please Help, I cant figure it out..
Thanks
Helwa