尝试将数据库中的数据绑定到它时在fastadapter中崩溃

时间:2019-03-24 10:17:25

标签: android kotlin android-recyclerview fastadapter

使用快速适配器时出现以下崩溃

java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
        at java.util.ArrayList.get(ArrayList.java:411)
        at com.android.server.wm.WindowAnimator.updateAppWindowsLocked(WindowAnimator.java:248)
        at com.android.server.wm.WindowAnimator.animateLocked(WindowAnimator.java:919)
        at com.android.server.wm.WindowAnimator.-wrap0(WindowAnimator.java)
        at com.android.server.wm.WindowAnimator$1.doFrame(WindowAnimator.java:180)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:925)
        at android.view.Choreographer.doCallbacks(Choreographer.java:702)
        at android.view.Choreographer.doFrame(Choreographer.java:635)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:913)
        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.os.HandlerThread.run(HandlerThread.java:61)
        at com.android.server.ServiceThread.run(ServiceThread.java:46)

有时会导致崩溃

java.lang.NullPointerException: Attempt to invoke interface method 'androidx.recyclerview.widget.RecyclerView$ViewHolder com.mikepenz.fastadapter.IItem.getViewHolder(android.view.ViewGroup)' on a null object reference
        at com.mikepenz.fastadapter.listeners.OnCreateViewHolderListenerImpl.onPreCreateViewHolder(OnCreateViewHolderListenerImpl.java:23)
        at com.mikepenz.fastadapter.FastAdapter.onCreateViewHolder(FastAdapter.java:684)
        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.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2230)
        at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1557)
        at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1517)
        at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:612)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3924)
        at androidx.recyclerview.widget.RecyclerView.onMeasure(RecyclerView.java:3336)
        at android.view.View.measure(View.java:21045)
        at androidx.recyclerview.widget.RecyclerView$LayoutManager.measureChildWithMargins(RecyclerView.java:9119)
        at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1583)
        at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1517)
        at androidx.recyclerview.widget.LinearLayoutManager.scrollBy(LinearLayoutManager.java:1331)
        at androidx.recyclerview.widget.LinearLayoutManager.scrollVerticallyBy(LinearLayoutManager.java:1075)
        at androidx.recyclerview.widget.RecyclerView.scrollStep(RecyclerView.java:1832)
        at androidx.recyclerview.widget.RecyclerView$ViewFlinger.run(RecyclerView.java:5067)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:927)
        at android.view.Choreographer.doCallbacks(Choreographer.java:702)
        at android.view.Choreographer.doFrame(Choreographer.java:635)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:913)
        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:6682)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)

这是我的代码

class ListingAttributes(private val activity: ListingDetailsActivity, private val viewModel: ListingAttributesViewModel) : AbstractItem<ListingAttributes, ListingAttributes.ViewHolder>() {

    override fun getLayoutRes() = R.layout.recycler_view_listing_attributes_item
    override fun getType() = R.id.recycler_view_listing_attributes_item

    override fun getViewHolder(v: View) = ListingAttributes.ViewHolder(
            RecyclerViewListingAttributesItemBinding.bind(v), activity, viewModel)

    class ViewHolder(private val binding: RecyclerViewListingAttributesItemBinding, private val activity: ListingDetailsActivity, private val viewModel: ListingAttributesViewModel) :
            FastAdapter.ViewHolder<ListingAttributes>(binding.root) {

        private var itemAdapter = ItemAdapter<IItem<*, *>>()

        private val loadAttributesFromDBObserver = Observer<Unit> {

            //binding.recyclerView.addView(AttributeBoolViewGrid(activity, viewModel.attributes.filter { it.type == AttributeType.BOOL.value }))

            viewModel.attributes.forEach { item ->
                when (item.type) {

                    AttributeType.TEXT.value, AttributeType.NUMBER.value, AttributeType.DROP_DOWN.value -> {
                        val listAttr = viewModel.listingAttribute.find { it.id.toInt() == item.id }
                        listAttr?.let { AttributeView(activity, listAttr, item, AttributeViewViewModel(viewModel.optionDao)).also { item -> itemAdapter.add(item) } }
                    }
                }
            }



            itemAdapter?.let {

                val fastAdapter: FastAdapter<IItem<*, *>> = FastAdapter.with(
                    listOf(
                        itemAdapter
                    )
                )

                fastAdapter.apply {
                    setHasStableIds(true)
                    withSelectable(true)
                }

                binding.recyclerView.apply {

                    layoutManager = LinearLayoutManager(context)
                    adapter = fastAdapter
                }

            }
        }

        override fun bindView(item: ListingAttributes, payloads: MutableList<Any>) {

            viewModel.loadAttributesFromDBEvent?.observe(activity, loadAttributesFromDBObserver)
        }

        override fun unbindView(item: ListingAttributes) {

            viewModel.loadAttributesFromDBEvent.removeObservers(activity)
        }
    }
}

这是我的视图模型

class ListingAttributesViewModel(val activity: Activity, val listingAttribute: List<ListingExtraAttr>) : BaseStatefulViewModel() {

    private val extraAttributesRepository: ExtraAttributeRawRepository by inject()
    val optionDao: ExtraAttributeOptionDao by inject()
    lateinit var attributes: List<ExtraAttributeEntity>
    val loadAttributesFromDBEvent = LiveEvent<Unit>()

    init {
        getAttributes(listingAttribute.joinToString { it.id })
    }

    private fun getAttributes(attributesIds: String) {

        launch(Dispatchers.IO) {
            extraAttributesRepository?.let {
                attributes = extraAttributesRepository.getExtraAttributesByIds(attributesIds)
                loadAttributesFromDBEvent.call()
            }
        }
    }
}

任何人都可以帮忙,崩溃没有说明发生的任何信息吗?

0 个答案:

没有答案