Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/214.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 无法从Firebase DB填充回收器视图_Android_Firebase_Android Recyclerview_Firebase Realtime Database_Firebaseui - Fatal编程技术网

Android 无法从Firebase DB填充回收器视图

Android 无法从Firebase DB填充回收器视图,android,firebase,android-recyclerview,firebase-realtime-database,firebaseui,Android,Firebase,Android Recyclerview,Firebase Realtime Database,Firebaseui,虽然这个问题被回答了几次,但任何一个答案都不能解决我的问题 我有一个firebase DB,它只有两个键值对。下面是json { "route" : { "routeA" : { "stop" : "Street 1", "time" : "11:00 PM" } } } 下面是我从firebase DB获取数据的代码 recyclerView = (RecyclerView) findViewById(R.id.recyclerView); recy

虽然这个问题被回答了几次,但任何一个答案都不能解决我的问题

我有一个firebase DB,它只有两个键值对。下面是json

{
   "route" : {
   "routeA" : {
   "stop" : "Street 1",
   "time" : "11:00 PM"
  }
 }
}
下面是我从firebase DB获取数据的代码

  recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    rootRef = new Firebase("https://listrecycler.firebaseio.com/route/");
}

@Override
protected void onStart() {
    super.onStart();
    itemsRef = rootRef.child("route B");
    Toast.makeText(MainActivity.this, "crossed itemRef", Toast.LENGTH_SHORT).show();
    firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Route, RouteViewHolder>(Route.class, android.R.layout.two_line_list_item, RouteViewHolder.class, itemsRef) {
        @Override
        public void populateViewHolder(RouteViewHolder routeViewHolder, Route route, int i) {
            Toast.makeText(MainActivity.this, "reached populate view holder", Toast.LENGTH_SHORT).show();
            Log.d(TAG, "populateViewHolder: " + route);
            routeViewHolder.place.setText(route.getStop());
            routeViewHolder.time.setText(route.getTime());
        }
    };
    recyclerView.setAdapter(firebaseRecyclerAdapter);

}

public static class RouteViewHolder extends RecyclerView.ViewHolder {

    TextView place;
    TextView time;

    public RouteViewHolder(View itemView) {
        super(itemView);
        place = (TextView) itemView.findViewById(android.R.id.text1);
        time = (TextView) itemView.findViewById(android.R.id.text2);
    }
}
这是我的POJO

public class Route {
public String stop;
public String time;

public Route() {
}

public Route(String stop, String time) {
    this.stop = stop;
    this.time = time;
}

public String getStop() {
    return stop;
}

public void setStop(String stop) {
    this.stop = stop;
}

public String getTime() {
    return time;
}

public void setTime(String time) {
    this.time = time;
}
}
代码的问题是,当我使用POJO时,Jackson无法反序列化Firebase发送的数据。当我使用一个简单的String类而不是Route POJO时,数据会完美地写入到recycler视图项中。下面是错误日志

05-06 22:38:03.724 30051-30051/abc.com.example.vijsu.listrecyclerfirebase E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                        Process: abc.com.example.vijsu.listrecyclerfirebase, PID: 30051
                                                                                        com.firebase.client.FirebaseException: Failed to bounce to type
                                                                                            at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:183)
                                                                                            at com.firebase.ui.FirebaseRecyclerAdapter.parseSnapshot(FirebaseRecyclerAdapter.java:161)
                                                                                            at com.firebase.ui.FirebaseRecyclerAdapter.getItem(FirebaseRecyclerAdapter.java:150)
                                                                                            at com.firebase.ui.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:190)
                                                                                            at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5465)
                                                                                            at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5498)
                                                                                            at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4735)
                                                                                            at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4611)
                                                                                            at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1988)
                                                                                            at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1384)
                                                                                            at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1347)
                                                                                            at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:574)
                                                                                            at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3026)
                                                                                            at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2903)
                                                                                            at android.support.v7.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1457)
                                                                                            at android.support.v7.widget.RecyclerView.access$400(RecyclerView.java:147)
                                                                                            at android.support.v7.widget.RecyclerView$1.run(RecyclerView.java:285)
                                                                                            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
                                                                                            at android.view.Choreographer.doCallbacks(Choreographer.java:670)
                                                                                            at android.view.Choreographer.doFrame(Choreographer.java:603)
                                                                                            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
                                                                                            at android.os.Handler.handleCallback(Handler.java:739)
                                                                                            at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                            at android.os.Looper.loop(Looper.java:148)
                                                                                            at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                                         Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not instantiate value of type [simple type, class abc.com.example.vijsu.listrecyclerfirebase.Route] from String value; no single-String constructor/factory method
                                                                                            at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator._createFromStringFallbacks(StdValueInstantiator.java:428)
                                                                                            at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.createFromString(StdValueInstantiator.java:299)
                                                                                            at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromString(BeanDeserializerBase.java:1056)
                                                                                            at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:136)
                                                                                            at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:123)
                                                                                            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:181)
                                                                                            at com.firebase.ui.FirebaseRecyclerAdapter.parseSnapshot(FirebaseRecyclerAdapter.java:161) 
                                                                                            at com.firebase.ui.FirebaseRecyclerAdapter.getItem(FirebaseRecyclerAdapter.java:150) 
                                                                                            at com.firebase.ui.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:190) 
                                                                                            at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5465) 
                                                                                            at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5498) 
                                                                                            at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4735) 
                                                                                            at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4611) 
                                                                                            at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1988) 
                                                                                            at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1384) 
                                                                                            at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1347) 
                                                                                            at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:574) 
                                                                                            at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3026) 
                                                                                            at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2903) 
                                                                                            at android.support.v7.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1457) 
                                                                                            at android.support.v7.widget.RecyclerView.access$400(RecyclerView.java:147) 
                                                                                            at android.support.v7.widget.RecyclerView$1.run(RecyclerView.java:285) 
                                                                                            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858) 
                                                                                            at android.view.Choreographer.doCallbacks(Choreographer.java:670) 
                                                                                            at android.view.Choreographer.doFrame(Choreographer.java:603) 
                                                                                            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844) 
                                                                                            at android.os.Handler.handleCallback(Handler.java:739) 
                                                                                            at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                                            at android.os.Looper.loop(Looper.java:148) 
                                                                                            at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                                            at java.lang.reflect.Method.invoke(Native Method) 
                                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
我已刷新Firebase检查中的数据,以确保POJO和Firebase DB中的字符串名称多次匹配。我知道我遗漏了一些与POJO有关的东西,但我无法找到它


但是回收器视图中仍然存在错误

错误提到了这一点

无法实例化类型为的值。。。路线。。。没有单字符串构造函数/工厂方法

因此,您可能需要添加一个

public class Route {

    public String stop;
    public String time;

    public Route() {}

    public Route(String routeJSON) { 
        // TODO: Parse out JSON object
        // call setters...
    }

    public Route(String stop, String time) {
        this.stop = stop;
        this.time = time;
    }

错误提到了这一点

无法实例化类型为的值。。。路线。。。没有单字符串构造函数/工厂方法

因此,您可能需要添加一个

public class Route {

    public String stop;
    public String time;

    public Route() {}

    public Route(String routeJSON) { 
        // TODO: Parse out JSON object
        // call setters...
    }

    public Route(String stop, String time) {
        this.stop = stop;
        this.time = time;
    }

最愚蠢的错误是,我忘记了recycler视图包含属性列表,我有多个属性(停止和时间)显示在一行中。因此,我只需要获取路由a并将其交给firebasui进行显示。我在将数据上传到routeA时进入了更深层次,firebaseui无法识别属性。只有一级查询到route就足以在单行中显示stoptime属性


把它作为一个单独的答案写下来,这样可以帮助像我这样对小而重要的事情一无所知的人。这个概念也是@Frank van Puffelen对类似问题的回答的一部分。

最愚蠢的错误是,我忘记了回收器视图包含一个属性列表,我有多个属性(停止和时间)显示在一行中。因此,我只需要获取路由a并将其交给firebasui进行显示。我在将数据上传到routeA时进入了更深层次,firebaseui无法识别属性。只有一级查询到route就足以在单行中显示stoptime属性


把它作为一个单独的答案写下来,这样可以帮助像我这样对小而重要的事情一无所知的人。这个概念也是@Frank van Puffelen对一个类似问题的回答的一部分。

cricket\u 007我刚刚从参数化构造函数中删除了第二个参数,一切正常,使其成为一个单字符串构造函数。但问题是我需要参数化构造函数将数据推送到firebase DB。public Route(String stop){this.stop=stop;}我发现的另一件事是,无论单个字符串构造中存在哪个属性,都只从JSON中提取该值。另一个值返回null。这是因为您必须设置两个字符串。。。您将得到一个包含两个值的JSON字符串。这就是我所说的
routeJSON
和“呼叫设定者”的意思。您需要使用Jackson将该字符串转换为JSONObjectcricket_007。我刚刚从参数化构造函数中删除了第二个参数,并且一切正常,使其成为单个字符串构造函数。但问题是我需要参数化构造函数将数据推送到firebase DB。public Route(String stop){this.stop=stop;}我发现的另一件事是,无论单个字符串构造中存在哪个属性,都只从JSON中提取该值。另一个值返回null。这是因为您必须设置两个字符串。。。您将得到一个包含两个值的JSON字符串。这就是我所说的
routeJSON
和“呼叫设定者”的意思。您需要使用Jackson将该字符串转换为JSONObject。我们最近收到了一些此类异常的案例。例如。如果这不是原因,您是否可以将JSON的图片替换为实际的JSON,您可以通过单击Firebase仪表板中的Export按钮轻松获得。此外:您能否在一段不需要
RecyclerView
的代码中隔离问题?我很确定你可以用一个
ref.getValue(Route.class)
来复制它,这将使你更容易得到帮助。@FrankvanPuffelen按照你的要求附上了JSON。没有回收器视图的简单代码非常好用。但我需要在代码中使用recycler视图。谢谢。我们最近收到了一些这种例外情况。例如。如果这不是原因,您是否可以将JSON的图片替换为实际的JSON,您可以通过单击Firebase仪表板中的Export按钮轻松获得。此外:您能否在一段不需要
RecyclerView
的代码中隔离问题?我很确定你可以用一个
ref.getValue(Route.class)
来复制它,这将使你更容易得到帮助。@FrankvanPuffelen按照你的要求附上了JSON。没有回收器视图的简单代码非常好用。但我需要在代码中使用recycler视图。谢谢