帮助,SomeBody帮助我..
这是数据库结构
这是我的MainActivity onCreate方法
@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) {
}
});
}
这是Daftarchat课程:
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;
}
}
问题是它一旦开始就会一直崩溃:
当我删除此语句时(在DaftarChat类中):
txttext.setText(sampel.getMsgText());
txtuser.setText(sampel.getMsgUser());
txttime.setText(sampel.getMsgTime());
它什么都没有显示,但程序可以运行,但我仍然无法检索数据。
下面的logcat:
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
请帮助,我无法弄清楚..
由于
Helwa
答案 0 :(得分:0)
空对象引用上的ChatMsg.getMsgText()'
sampel.getMsgText()为null。添加一些空检查以防止应用程序崩溃,如:
txttext.setText(sampel.getMsgText()!=null?sampel.getMsgText():INSERT_DEFAULT_VALUE_HERE);
答案 1 :(得分:0)
好吧因此可能存在一些问题:
在onCreate中仅设置适配器一次:
DaftarChat adapter = new DaftarChat(MainActivity.this, new ArrayList<>());
listofMsg.setAdapter(adapter);
在onChildAdded方法中:
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
// Toast.makeText(MainActivity.this, "onChildAdded:" + dataSnapshot.getKey(), Toast.LENGTH_SHORT).show();
String id = dataSnapshot.getKey();
for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
ChatMsg msg = dataSnapshot.child(id).getValue(ChatMsg.class);
if(msg != null){
listOfMsg.getAdapter().addMsg(chatmsg);
}
}
}
然后在Adapter类中添加此方法:
public void addMsg(ChatMsg chatmsg){
this.daftarchat.add(chatmsg);
notifyDataSetChanged();
}
并修改你的getView方法:
@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);
if(sampel != null){
txttext.setText(sampel.getMsgText());
txtuser.setText(sampel.getMsgUser());
txttime.setText(sampel.getMsgTime());
}
return listViewItem;
}
答案 2 :(得分:0)
假设所有这些消息都是Firebase数据库根目录的直接子节点,为了使其正常工作,请使用以下更简单的代码:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
List<ChatMsg> chatlist = new ArrayList<>();
for(DataSnapshot ds : dataSnapshot.getChildren()) {
ChatMsg chatmsg = dataSnapshot.getValue(ChatMsg.class);
chatlist.add(chatmsg);
}
ListView listofMsg = (ListView) findViewById(R.id.list_of_messange);
DaftarChat adapter = new DaftarChat(MainActivity.this, chatlist);
listofMsg.setAdapter(adapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
rootRef.addListenerForSingleValueEvent(eventListener);
答案 3 :(得分:0)