FirebaseRecyclerAdapter用于消息传递应用程序

时间:2017-09-02 15:52:35

标签: android firebase firebase-realtime-database android-recyclerview

我已经搜索了FirebaseRecyclerAdapter和相关类的文档,但找不到一种方法来做我想做的事情(没有自己重建库)。

我正在开发一个内置消息传递的应用程序(包括组和其他用户的直接消息传递)。

群聊工作正常,因为它会从群聊节点下的数据库引用中提取每条消息。

直接消息很棘手。我想跳过引用中不包含当前用户的模型ID的节点,以及他们尝试发送消息的用户(以便他们看不到其他人的消息)。

在下面的代码中,如果viewType = 10,我试图返回null,因为我不希望显示的聊天气泡......正如您所期望的那样,我从RecyclerViewAdapter类中获得了一个空指针异常。 / p>

我知道为什么我收到错误。我故意将null传递给它,试图跳过(或迭代当前项),而不在任何位置显示聊天气泡。

这是我的firebase结构(导出的JSON):Database Structure

这是一个堆栈跟踪供参考:

70

这是我的FirebaseRecyclerAdapter:

FATAL EXCEPTION: main
                                                                         Process: com.aztechdev.squad, PID: 21249
                                                                 java.lang.NullPointerException: Attempt to write to field 'int android.support.v7.widget.RecyclerView$ViewHolder.mItemViewType' on a null object reference
                                                                     at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6368)
                                                                     at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5555)
                                                                     at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5440)
                                                                     at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5436)
                                                                     at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2224)
                                                                     at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1551)
                                                                     at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1511)
                                                                     at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:570)
                                                                     at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3583)
                                                                     at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3312)
                                                                     at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3844)
                                                                     at android.view.View.layout(View.java:17637)
                                                                     at android.view.ViewGroup.layout(ViewGroup.java:5575)
                                                                     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
                                                                     at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                                                                     at android.view.View.layout(View.java:17637)
                                                                     at android.view.ViewGroup.layout(ViewGroup.java:5575)
                                                                     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
                                                                     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
                                                                     at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
                                                                     at android.view.View.layout(View.java:17637)
                                                                     at android.view.ViewGroup.layout(ViewGroup.java:5575)
                                                                     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
                                                                     at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                                                                     at android.view.View.layout(View.java:17637)
                                                                     at android.view.ViewGroup.layout(ViewGroup.java:5575)
                                                                     at com.mxn.soul.flowingdrawer_core.FlowingDrawer.onLayout(FlowingDrawer.java:138)
                                                                     at android.view.View.layout(View.java:17637)
                                                                     at android.view.ViewGroup.layout(ViewGroup.java:5575)
                                                                     at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1795)
                                                                     at android.view.View.layout(View.java:17637)
                                                                     at android.view.ViewGroup.layout(ViewGroup.java:5575)
                                                                     at android.support.constraint.ConstraintLayout.onLayout(ConstraintLayout.java:1197)
                                                                     at android.view.View.layout(View.java:17637)
                                                                     at android.view.ViewGroup.layout(ViewGroup.java:5575)
                                                                     at android.support.design.widget.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:131)
                                                                     at android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42)
                                                                     at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1391)
                                                                     at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:870)
                                                                     at android.view.View.layout(View.java:17637)
                                                                     at android.view.ViewGroup.layout(ViewGroup.java:5575)
                                                                     at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1193)
                                                                     at android.view.View.layout(View.java:17637)
                                                                     at android.view.ViewGroup.layout(ViewGroup.java:5575)
                                                                     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
                                                                     at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                                                                     at android.view.View.layout(View.java:17637)
                                                                     at android.view.ViewGroup.layout(ViewGroup.java:5575)
                                                                     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
                                                                     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
                                                                     at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
                                                                     at android.view.View.layout(View.java:17637)
                                                                     at android.view.ViewGroup.layout(ViewGroup.java:5575)
                                                                     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
                                                                     at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
09-02 10:47:22.899 21249-21249/com.aztechdev.squad E/AndroidRuntime:     at android.view.View.layout(View.java:17637)
                                                                     at android.view.ViewGroup.layout(ViewGroup.java:5575)
                                                                     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
                                                                     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
                                                                     at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
                                                                     at android.view.View.layout(View.java:17637)
                                                                     at android.view.ViewGroup.layout(ViewGroup.java:5575)
                                                                     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
                                                                     at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                                                                     at com.android.internal.policy.DecorView.onLayout(DecorView.java:726)
                                                                     at android.view.View.layout(View.java:17637)
                                                                     at android.view.ViewGroup.layout(ViewGroup.java:5575)
                                                                     at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2346)
                                                                     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2068)
                                                                     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)
                                                                     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6337)
                                                                     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:874)
                                                                     at android.view.Choreographer.doCallbacks(Choreographer.java:686)
                                                                     at android.view.Choreographer.doFrame(Choreographer.java:621)
                                                                     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:860)
                                                                     at android.os.Handler.handleCallback(Handler.java:751)
                                                                     at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                     at android.os.Looper.loop(Looper.java:154)
                                                                     at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

1 个答案:

答案 0 :(得分:0)

您有三种选择:

  1. 更改数据结构,因此您无需跳过行。这是长期表现的最佳选择,但如果您只是试图通过POC,可能不是您的偏好。

  2. 不要使用FirebaseUI。使用基本Firebase库自行运行查询。然后在客户端上对结果进行后处理,以删除/省略您不想要的行。您必须将实际行计数返回到RecyclerView,而不包含您想要省略的那些行,并且只有您想要的行的干净数组是最简单的方法。

  3. 快速入侵:而不是返回null,而是返回0高度的视图!这是一个非常难看的黑客,但确实有效。