我的目标只是从Firebase检索数据,然后在Android中将其作为ListView输出(无需将任何内容推送回数据库)。我试图从AndroidChat示例中学习并创建了我自己的类和我自己的自定义列表适配器类,而不是Chat.java和ChatListAdapter.java(如在orignal示例中)。我还更改了对Firebase的引用,并将我的数据结构与https://android-chat.firebaseio-demo.com/的数据结构相似。下面是我的数据结构:(以下所有内容与AndroidChat示例相同,只是不同的变量名称)
我自己的班级:
package com.firebase.androidchat;
public class MenuItem {
private String food;
private String weekDay;
// Required default constructor for Firebase object mapping
@SuppressWarnings("unused")
private MenuItem() { }
MenuItem(String food, String weekDay) {
this.food = food;
this.weekDay = weekDay;
}
public String getFood() {
return food;
}
public String getWeekDay() {
return weekDay;
}
}
我自己的自定义列表适配器类:
package com.firebase.androidchat;
import android.app.Activity;
import android.view.View;
import android.widget.TextView;
import com.firebase.client.Query;
public class MenuListAdapter extends FirebaseListAdapter<MenuItem> {
public MenuListAdapter(Query ref, Activity activity, int layout) {
super(ref, MenuItem.class, layout, activity);
}
@Override
protected void populateView(View view, MenuItem item) {
// Map a MenuItem object to an entry in our listview
String weekDay = item.getWeekDay();
TextView authorText = (TextView)view.findViewById(R.id.author);
authorText.setText(weekDay + ": ");
((TextView)view.findViewById(R.id.message)).setText(item.getFood());
}
}
我的主要活动简要说明:
public class MainActivity extends ListActivity {
// TODO: change this to your own Firebase URL
private static final String FIREBASE_URL = "https://barlaurea.firebaseio.com/";
private Firebase ref;
private ValueEventListener connectedListener;
private MenuListAdapter menuListAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Setup our Firebase ref
ref = new Firebase(FIREBASE_URL).child("menu");
}
@Override
public void onStart() {
super.onStart();
// Setup our view and list adapter. Ensure it scrolls to the bottom as data changes
final ListView listView = getListView();
// Tell our list adapter that we only want 50 messages at a time
menuListAdapter = new MenuListAdapter(ref.limit(50), this, R.layout.chat_message);
listView.setAdapter(menuListAdapter);
menuListAdapter.registerDataSetObserver(new DataSetObserver() {
@Override
public void onChanged() {
super.onChanged();
listView.setSelection(menuListAdapter.getCount() - 1);
}
});
当我启动“运行”按钮时,我的设备显示一个Toast消息“已连接到Firebase”,但屏幕保持空白,然后在几秒钟后崩溃,显示一个祝酒词“不幸的是,Firebase Chat已停止”。我在这里想念的是什么?谢谢!
编辑:这是我的LogCat视图:“FAIL EXCEPTION:主线”处的所有内容都变为红色 最初的AndroidChat在我的设备上运行正常..
02-07 16:17:35.105: D/dalvikvm(31766): GC_CONCURRENT freed 313K, 16% free 7795K/9223K, paused 14ms+16ms, total 65ms
02-07 16:17:36.000: D/dalvikvm(31766): GC_CONCURRENT freed 494K, 17% free 7874K/9479K, paused 2ms+3ms, total 34ms
02-07 16:17:36.365: D/dalvikvm(31766): GC_CONCURRENT freed 482K, 17% free 7923K/9543K, paused 19ms+21ms, total 142ms
02-07 16:17:36.495: D/AndroidRuntime(31766): Shutting down VM
02-07 16:17:36.495: W/dalvikvm(31766): threadid=1: thread exiting with uncaught exception (group=0x41fb02a0)
02-07 16:17:36.520: E/AndroidRuntime(31766): FATAL EXCEPTION: main
02-07 16:17:36.520: E/AndroidRuntime(31766): com.firebase.client.FirebaseException: Failed to bounce to type
02-07 16:17:36.520: E/AndroidRuntime(31766): at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:185)
02-07 16:17:36.520: E/AndroidRuntime(31766): at com.firebase.androidchat.FirebaseListAdapter$1.onChildAdded(FirebaseListAdapter.java:63)
02-07 16:17:36.520: E/AndroidRuntime(31766): at com.firebase.client.core.ChildListenerContainer$1.run(ChildListenerContainer.java:52)
02-07 16:17:36.520: E/AndroidRuntime(31766): at android.os.Handler.handleCallback(Handler.java:615)
02-07 16:17:36.520: E/AndroidRuntime(31766): at android.os.Handler.dispatchMessage(Handler.java:92)
02-07 16:17:36.520: E/AndroidRuntime(31766): at android.os.Looper.loop(Looper.java:137)
02-07 16:17:36.520: E/AndroidRuntime(31766): at android.app.ActivityThread.main(ActivityThread.java:4947)
02-07 16:17:36.520: E/AndroidRuntime(31766): at java.lang.reflect.Method.invokeNative(Native Method)
02-07 16:17:36.520: E/AndroidRuntime(31766): at java.lang.reflect.Method.invoke(Method.java:511)
02-07 16:17:36.520: E/AndroidRuntime(31766): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
02-07 16:17:36.520: E/AndroidRuntime(31766): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
02-07 16:17:36.520: E/AndroidRuntime(31766): at dalvik.system.NativeStart.main(Native Method)
02-07 16:17:36.520: E/AndroidRuntime(31766): Caused by: com.shaded.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "weekday" (class com.firebase.androidchat.MenuItem), not marked as ignorable (2 known properties: , "weekDay", "food"])
02-07 16:17:36.520: E/AndroidRuntime(31766): at [Source: java.io.StringReader@42826fc8; line: 1, column: 13] (through reference chain: com.firebase.androidchat.MenuItem["weekday"])
02-07 16:17:36.520: E/AndroidRuntime(31766): at com.shaded.fasterxml.jackson.databind.DeserializationContext.reportUnknownProperty(DeserializationContext.java:555)
02-07 16:17:36.520: E/AndroidRuntime(31766): at com.shaded.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:708)
02-07 16:17:36.520: E/AndroidRuntime(31766): at com.shaded.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1160)
02-07 16:17:36.520: E/AndroidRuntime(31766): at com.shaded.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:315)
02-07 16:17:36.520: E/AndroidRuntime(31766): at com.shaded.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:121)
02-07 16:17:36.520: E/AndroidRuntime(31766): at com.shaded.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2888)
02-07 16:17:36.520: E/AndroidRuntime(31766): at com.shaded.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2034)
02-07 16:17:36.520: E/AndroidRuntime(31766): at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:181)
02-07 16:17:36.520: E/AndroidRuntime(31766): ... 11 more
答案 0 :(得分:5)
答案在这里找到:
Firebase's AndroidChat example: Failed to bounce to type
在这种情况下,我应该在firebase实例中键入weekDay而不是工作日。