2
**I am trying to populate the firebase data via FirebaseRecyclerAdapter but i get  errors in log,
01-01 22:42:32.610  25547-25547/exam.vsrk.cleanvit I/System.out﹕ Description: add
01-01 22:42:32.610  25547-25547/exam.vsrk.cleanvit I/System.out﹕ Status: clean
01-01 22:42:32.610  25547-25547/exam.vsrk.cleanvit I/System.out﹕ Description: ghffjggu
01-01 22:42:32.610  25547-25547/exam.vsrk.cleanvit I/System.out﹕ Status: dirty
01-01 22:42:32.610  25547-25547/exam.vsrk.cleanvit I/System.out﹕ Description: fhfhjhg
01-01 22:42:32.610  25547-25547/exam.vsrk.cleanvit I/System.out﹕ Status: dirty
01-01 22:42:32.610  25547-25547/exam.vsrk.cleanvit I/System.out﹕ Description: fg
01-01 22:42:32.610  25547-25547/exam.vsrk.cleanvit I/System.out﹕ Status: dirty
01-01 22:42:32.610  25547-25547/exam.vsrk.cleanvit I/System.out﹕ Description: ggg
01-01 22:42:32.610  25547-25547/exam.vsrk.cleanvit I/System.out﹕ Status: dirty
01-01 22:42:32.610  25547-25547/exam.vsrk.cleanvit I/System.out﹕ Description: add
01-01 22:42:32.611  25547-25547/exam.vsrk.cleanvit I/System.out﹕ Status: dirty
01-01 22:42:32.611  25547-25547/exam.vsrk.cleanvit I/System.out﹕ Description: .ark this
01-01 22:42:32.611  25547-25547/exam.vsrk.cleanvit I/System.out﹕ Status: dirty
01-01 22:42:32.611  25547-25547/exam.vsrk.cleanvit I/System.out﹕ Description: mark
01-01 22:42:32.611  25547-25547/exam.vsrk.cleanvit I/System.out﹕ Status: dirty
01-01 22:42:32.648  25547-25547/exam.vsrk.cleanvit D/AndroidRuntime﹕ Shutting down VM
01-01 22:42:32.650  25547-25547/exam.vsrk.cleanvit E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: exam.vsrk.cleanvit, PID: 25547
    com.firebase.client.FirebaseException: Failed to bounce to type
            at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:185)
            at com.firebase.ui.FirebaseRecyclerAdapter.getItem(FirebaseRecyclerAdapter.java:150)
            at com.firebase.ui.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:179)
            at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5217)
            at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5250)
            at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4487)
            at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4363)
            at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1961)
            at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1370)
            at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1333)
            at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:562)
            at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2900)
            at android.support.v7.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1445)
            at android.support.v7.widget.RecyclerView.access$400(RecyclerView.java:144)
            at android.support.v7.widget.RecyclerView$1.run(RecyclerView.java:282)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
            at android.view.Choreographer.doCallbacks(Choreographer.java:580)
            at android.view.Choreographer.doFrame(Choreographer.java:549)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5343)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
     Caused by: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "-880497298" (class exam.vsrk.cleanvit.MapComponents.Maps.FireBaseUI.RemovedSpotItems), not marked as ignorable (4 known properties: , "status", "owner", "description", "cleanedBy"])
            at [Source: java.io.StringReader@1026bfb7; line: 1, column: 16] (through reference chain: exam.vsrk.cleanvit.MapComponents.Maps.FireBaseUI.RemovedSpotItems["-880497298"])
            at com.fasterxml.jackson.databind.DeserializationContext.reportUnknownProperty(DeserializationContext.java:555)
            at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:708)
            at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1160)
            at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:315)
            at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:121)
            at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2888)
            at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2034)
            at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:183)
            at com.firebase.ui.FirebaseRecyclerAdapter.getItem(FirebaseRecyclerAdapter.java:150)
            at com.firebase.ui.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:179)
            at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5217)
            at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5250)
            at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4487)
            at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4363)
            at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1961)
            at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1370)
            at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1333)
            at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:562)
            at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2900)
            at android.support.v7.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1445)
            at android.support.v7.widget.RecyclerView.access$400(RecyclerView.java:144)
            at android.support.v7.widget.RecyclerView$1.run(RecyclerView.java:282)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
            at android.view.Choreographer.doCallbacks(Choreographer.java:580)
            at android.view.Choreographer.doFrame(Choreographer.java:549)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5343)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)

这是我的完整代码。

public class RemovedSpotsActivity extends AppCompatActivity {

  private FirebaseRecyclerAdapter mAdapter;
  List<RemovedSpotItems> removedSpots;


  @Override
  protected void onCreate(Bundle s) {

    super.onCreate(s);
    setContentView(R.layout.removed_spots_list);

    removedSpots=new ArrayList<>();

    Firebase.setAndroidContext(this);
    final Firebase ref = new Firebase(getResources().getString(R.string.firebase_url));
    RecyclerView recycler = (RecyclerView) findViewById(R.id.recycler_view);
    recycler.setHasFixedSize(true);
    recycler.setLayoutManager(new LinearLayoutManager(this));
    String key=ref.child("markers").getKey();
    Log.v("KEY_ENTERED",key);
    ref.child("markers").addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
          Map<String, Object> newPost = (Map<String, Object>) dataSnapshot.getValue();
          System.out.println("Description: " + newPost.get("description"));
            System.out.println("Status: " + newPost.get("status"));
            RemovedSpotItems items = new RemovedSpotItems();

            items.setDescription((String) newPost.get("description"));
            items.setStatus((String) newPost.get("status"));
            removedSpots.add(items);
        }
        public void onChildChanged(DataSnapshot dataSnapshot, String s) { }
        public void onChildRemoved(DataSnapshot dataSnapshot) { }
        public void onChildMoved(DataSnapshot dataSnapshot, String s) { }
        public void onCancelled(FirebaseError firebaseError) { }
    });

    mAdapter = new FirebaseRecyclerAdapter<RemovedSpotItems, ReomvedSpotViewHolder>(RemovedSpotItems.class, R.layout.removed_spots_row, ReomvedSpotViewHolder.class,ref) {

        @Override
        protected void populateViewHolder(ReomvedSpotViewHolder viewHolder, RemovedSpotItems model, int position) {
            super.populateViewHolder(viewHolder, model, position);
            final int i = 0;
            Log.v("TAG","populating");
            RemovedSpotItems model1=removedSpots.get(i);

            viewHolder.description.setText(model1.getDescription());
            viewHolder.email.setText(model1.getStatus());
        }
    };

    recycler.setAdapter(mAdapter);
}

我已经尽力了,但无法识别错误我认为检索数据没有问题,因为我们可以在日志中看到 FirebaseRecyclerAdapter 存在一些问题。

RemovedSpotItems.java

public class RemovedSpotItems {
    public String description;
    public String cleanedBy;
    public String owner;
    public String status;

    public String getDescription()
    {
        return  description;
    }
    public void setDescription(String description)
    {
        this.description=description;        
    }
    public String getCleanedBy()
    {
        return cleanedBy;
    }
    public void setCleanedBy(String cleanedBy)
    {
        this.cleanedBy=cleanedBy;
    }
    public String getStatus()
    {
        return status;
    }
    public void setStatus(String status)
    {
        this.status=status;
    }
    public String getOwner() { return owner;}
    public  void setOwner(String owner)
    {
        this.owner=owner;
    }

}

JSON结构

{
  "markers" : {
    "-341910319" : {
      "description" : "fhfhfhh",
      "latitude" : 26.207505661864655,
      "longitude" : 78.17407708615065,
      "owner" : "6b63df86-9d11-47f5-bac1-7150f9043016",
      "status" : "dirty"
    },
    "-101000882" : {
      "description" : "fifs",
      "latitude" : 26.207906640793592,
      "longitude" : 78.17450053989887,
      "owner" : "6b63df86-9d11-47f5-bac1-7150f9043016",
      "status" : "dirty"
    }
  },
  "users" : {
    "6b63df86-9d11-47f5-bac1-7150f9043016" : {
      "displayName" : "ramkishorevit@gmail.com",
      "provider" : "password"
    }
  }
}
4

2 回答 2

0

编辑

您的 RemovedSpotItems 类需要一个默认构造函数

Firebase 需要一个默认(无参数构造函数)才能创建此类的新实例

public RemovedSpotItems() {}

添加了编辑的原始

如果您想使用 RemovedSpotItems 类而不需要 JSON 对象中的所有属性,则可以使用:

import com.fasterxml.jackson.annotation.JsonIgnoreProperties; 
...


 @JsonIgnoreProperties(ignoreUnknown=true)
 public class RemovedSpotItems { 
    ...
    //edit
    public RemovedSpotItems() {}
    ...
 }

只要确保您正在处理正确使用的那些。

可能返回的类型getValue()BooleanStringLongDoubleMap<String, Object>List<Object>。如果该位置不存在数据,则快照将返回null

于 2016-02-19T03:23:48.210 回答
0

错误消息说:

无法识别的字段“-880497298”(类exam.vsrk.cleanvit.MapComponents.Maps.FireBaseUI.RemovedSpotItems),未标记为可忽略(4 个已知属性:、“状态”、“所有者”、“描述”、“cleanedBy”])

在将 JSON 从 Firebase 解析到您的 Java 类时,Jackson 在您的类中发现了四个属性:statusowner和. 但是在 JSON 中,它发现了一个名为 的字段,它不知道如何处理。descriptioncleanedBy-880497298

此问题的原因似乎是您传递了对适配器的错误引用。您的代码中的相关片段:

final Firebase ref = new Firebase(getResources().getString(R.string.firebase_url));
...
String key=ref.child("markers").getKey();
...
mAdapter = new FirebaseRecyclerAdapter<RemovedSpotItems, ReomvedSpotViewHolder>(
    RemovedSpotItems.class, 
    R.layout.removed_spots_row, 
    ReomvedSpotViewHolder.class,
    ref) {

因此,您很可能需要ref.child("markers")在构建适配器时作为最后一个参数传入。

修复此问题后,您仍然会收到 JSON-to-Java 错误,因为您的 JSON 与您的类不匹配。我建议研究这个问题和答案来解决这个问题:当我将 JSON 从 Firebase 转换为 Java 对象时,为什么会出现“无法弹回输入”?

于 2016-01-02T15:39:58.823 回答