首页 > 解决方案 > 库折叠单元格 android 不工作,给 null 不能转换为非 null 错误

问题描述

图书馆

回购:

https://github.com/Ramotion/folding-cell-android/

版本:

com.ramotion.foldingcell:折叠单元:1.2.3

当我尝试从 github repo运行折叠单元列表示例时,我遇到了一些问题,我收到了这个错误:

E/AndroidRuntime:致命异常:主进程:com.rebocar.guincho7,PID:13693 kotlin.TypeCastException:null 不能在 com.rebocar.guincho7.listview.FoldingCellListAdapter 转换为非空类型 com.ramotion.foldingcell.FoldingCell。 getView(FoldingCellListAdapter.kt:26)

错误指向getView代码:

 override fun getView(position: Int, convertView: View?, parent: ViewGroup): View { // get item for selected view
        val item = getItem(position)

        // if cell is exists - reuse it, if not - create the new one from resource

        val cell = convertView as FoldingCell

        val viewHolder: ViewHolder

        // for existing cell set valid valid state(without animation)
        if (unfoldedIndexes.contains(position)) {
            cell.unfold(true)
        } else {
            cell.fold(true)
        }
        viewHolder = cell.tag as ViewHolder

        if (null == item) return cell

        // bind data from selected element to view through view holder
        // bind data from selected element to view through view holder
        viewHolder.price!!.text = item.price
        viewHolder.time!!.text = item.time
        viewHolder.date!!.text = item.date
        viewHolder.fromAddress!!.text = item.fromAddress
        viewHolder.toAddress!!.text = item.toAddress
        viewHolder.requestsCount!!.text = item.requestsCount.toString()
        viewHolder.pledgePrice!!.text = item.pledgePrice

        // set custom btn handler for list item from that item
        // set custom btn handler for list item from that item
        if (item.getRequestBtnClickListener() != null) {
            viewHolder.contentRequestBtn!!.setOnClickListener(item.getRequestBtnClickListener())
        } else { // (optionally) add "default" handler if no handler found in item
            viewHolder.contentRequestBtn!!.setOnClickListener(defaultRequestBtnClickListener)
        }

        return cell
    }

第 26 行是: val cell = convertView as FoldingCell

如果我评论以下几行,它不会给出错误(但不显示任何内容):

mainListView.adapter  = adapter
 mainListView.setOnItemClickListener { _, view, pos, _ ->
            // toggle clicked cell state
            (view as FoldingCell).toggle(false)
            // register in adapter that state for selected cell is toggled
            adapter.registerToggle(pos)
        }

我的代码有什么问题,我真的只是从 repo 中添加了示例,任何人都知道可能是什么?

标签: androidkotlin

解决方案


在基本用法部分,它说您的布局中必须具有此特定结构

    <com.ramotion.foldingcell.FoldingCell
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/folding_cell"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
​
        <FrameLayout
            android:id="@+id/cell_content_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/holo_green_dark"
            android:visibility="gone">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="250dp" />
        </FrameLayout>
​
        <FrameLayout
            android:id="@+id/cell_title_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:background="@android:color/holo_blue_dark" />
        </FrameLayout>
​
</com.ramotion.foldingcell.FoldingCell>

你遵循这个模式吗?也许你可以粘贴你的实际布局来仔细检查?


推荐阅读