当在leanback中的Presenter.onCreateViewHolder上抛出NEP时,如何找出错误所在

时间:2019-07-02 15:46:52

标签: android-tv leanback

数据从服务器到达后,我会做简单的事情:

    private fun updateItemsOnUi(items: List<Item>) {
        val listRowAdapter = ArrayObjectAdapter(ItemPresenterSelector())
        val header = HeaderItem(0, "")
        for (item in items) listRowAdapter.add(item)
        rowsAdapter.add(ItemListRow(header, listRowAdapter, null))
    }

我用详细信息屏幕重写了简单的示例。之后,我得到了这样的异常,但是我的代码没有意义,那么如何调查我做错了什么?

 java.lang.NullPointerException: Attempt to invoke virtual method 'androidx.leanback.widget.Presenter$ViewHolder androidx.leanback.widget.Presenter.onCreateViewHolder(android.view.ViewGroup)' on a null object reference
        at androidx.leanback.widget.ItemBridgeAdapter.onCreateViewHolder(ItemBridgeAdapter.java:352)
        at androidx.recyclerview.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6794)
        at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5975)
        at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5858)
        at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5854)
        at androidx.leanback.widget.GridLayoutManager.getViewForPosition(GridLayoutManager.java:1085)
        at androidx.leanback.widget.GridLayoutManager$2.createItem(GridLayoutManager.java:1613)
        at androidx.leanback.widget.SingleRow.appendVisibleItems(SingleRow.java:113)
        at androidx.leanback.widget.Grid.appendVisibleItems(Grid.java:397)
        at androidx.leanback.widget.GridLayoutManager.appendVisibleItems(GridLayoutManager.java:1934)
        at androidx.leanback.widget.GridLayoutManager.onLayoutChildren(GridLayoutManager.java:2291)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3924)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3641)
        at androidx.recyclerview.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1888)
        at androidx.recyclerview.widget.RecyclerView$1.run(RecyclerView.java:407)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
        at android.view.Choreographer.doCallbacks(Choreographer.java:723)
        at android.view.Choreographer.doFrame(Choreographer.java:655)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

1 个答案:

答案 0 :(得分:0)

就我而言,它不是在ClassPresenterSelector中注册的班级主持人。

我必须添加一行:

rowPresenterSelector.addClassPresenter(ItemListRow::class.java,  ListRowPresenter())

查看完整代码:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setupUi()
    setupEventListeners()
}

private fun setupUi() {

        val rowPresenterSelector = ClassPresenterSelector()
        //this line was needed!
        rowPresenterSelector.addClassPresenter(ItemListRow::class.java,  ListRowPresenter())

        rowsAdapter = ArrayObjectAdapter(rowPresenterSelector)
        adapter = rowsAdapter
    }