问题描述
我的Firebase出现问题.调试应用程序时出现错误
Hi I've got a problem with my Firebase. When I debug my app i've got error
下面是我的代码:
private void displayChatMessage() {
FirebaseListOptions<ChatMessage> options = new FirebaseListOptions.Builder<ChatMessage>()
.setQuery(FirebaseDatabase.getInstance().getReference(""), ChatMessage.class).setLayout(R.layout.messages).build();
ListView listOfMessages = (ListView)findViewById(R.id.list_of_messages);
adapter = new FirebaseListAdapter<ChatMessage>(options) {
@Override
protected void populateView(View v, ChatMessage model, int position) {
// zdefiniuj elementy modelu
TextView messageText = v.findViewById(R.id.message_text);
TextView messageUser = v.findViewById(R.id.message_user);
TextView messageTime = v.findViewById(R.id.message_time);
// ustaw wartosci
messageText.setText(model.getMessageText());
messageUser.setText(model.getMessageUser());
// format daty pokazywanej w wiadomosciach
messageTime.setText(DateFormat.format("dd-MM-yyyy (HH:mm:ss)",
model.getMessageTime()));
}
};
listOfMessages.setAdapter(adapter);
}
@Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
@Override
protected void onStop() {
super.onStop();
adapter.stopListening();
}
推荐答案
您有以下内容:
@Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
现在onStart()
是活动生命周期,在这里您开始监听值.
Now onStart()
is an activity lifecycle, and here you are starting to listen for values.
活动生命周期如下:
onCreate()>onStart()>onResume()
这三种方法被调用,因此您的活动出现在您的面前.并且由于您在onStart()
中使用adapter
,所以适配器的初始化必须在onCreate()
之前的方法中.因此,该adapter = new FirebaseListAdapter<ChatMessage>(options) {...}
必须位于onCreate()
内部.
Those three methods are called so your activity appears infront of you. And since you are using adapter
inside onStart()
, then the initialization of the adapter needs to be in the method before it which is onCreate()
. So this adapter = new FirebaseListAdapter<ChatMessage>(options) {...}
has to be inside onCreate()
.
这篇关于FirebaseListAdapter.startListening()中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!