leanback Presenter.onCreateViewHolder 上的 NEP 被抛出时如何找出问题所在

How to figure out what is wrong when NEP on Presenter.onCreateViewHolder in leanback was thrown

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

    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.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.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)

在我的例子中,它没有在 ClassPresenterSelector 中注册 class presenter。

我必须添加行:

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
    }