lateinit属性适配器尚未在Kotlin4Android中初始化

lateinit属性适配器尚未在Kotlin4Android中初始化,android,kotlin,android-recyclerview,kotlin-android-extensions,Android,Kotlin,Android Recyclerview,Kotlin Android Extensions,我在fragment的activity中创建了RecyclerView,所有这些都工作正常,但是当我从activity通过接口创建notifyDataSetChanged()到适配器时,我得到了一个错误“lateinit属性适配器尚未初始化””但是我已经初始化了适配器 class BuildingListFragment : Fragment(), MainActivity.EditInterface { lateinit var adapter: BuildingListA

我在
fragment
activity
中创建了
RecyclerView
,所有这些都工作正常,但是当我从
activity
通过
接口
创建
notifyDataSetChanged()
适配器
时,我得到了一个错误“lateinit属性
适配器
尚未初始化””但是我已经初始化了适配器

    class BuildingListFragment : Fragment(), MainActivity.EditInterface {


    lateinit var adapter: BuildingListAdapter

    private var mListener: OnFragmentInteractionListener? = null

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

    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        // Inflate the layout for this fragment
        return inflater!!.inflate(R.layout.fragment_building_list, container, false)
    }

    override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        val buildingList = ArrayList<BuildingDetailModel>()

        val alphaList = arrayOf("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K",
                "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "z")
        for (i in alphaList.indices) {

            val building = BuildingDetailModel()
            building.buildingName = alphaList[i] + " Building"
            buildingList.add(building)

        }

        //initialize adapter
        adapter = BuildingListAdapter(buildingList)
        // RV_Building_List.layoutManager = LinearLayoutManager(context, LinearLayout.VERTICAL, false)
        RV_Building_List.adapter = adapter

        RV_Building_List.layoutManager = object : LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false) {
            override fun onLayoutChildren(recycler: RecyclerView.Recycler?, state: RecyclerView.State) {
                super.onLayoutChildren(recycler, state)
                //TODO if the items are filtered, considered hiding the fast scroller here
                val firstVisibleItemPosition = findFirstVisibleItemPosition()
                if (firstVisibleItemPosition != 0) {
                    // this avoids trying to handle un-needed calls
                    if (firstVisibleItemPosition == -1)
                    //not initialized, or no items shown, so hide fast-scroller
                    {
                        fastscroller.visibility = View.GONE
                    }
                    return
                }
                val lastVisibleItemPosition = findLastVisibleItemPosition()
                val itemsShown = lastVisibleItemPosition - firstVisibleItemPosition + 1
                //if all items are shown, hide the fast-scroller
                fastscroller.visibility = if (adapter.itemCount > itemsShown) View.VISIBLE else View.GONE
            }
        }
        fastscroller.setRecyclerView(RV_Building_List)
        fastscroller.setViewsToUse(R.layout.recycler_view_fast_scroller__fast_scroller, R.id.fastscroller_bubble, R.id.fastscroller_handle)

    }

    override fun editClickFromMainActivity() {
        // TODO("not implemented") //To change body of created functions use File | Settings | File Templates.

        //at this line error  is lateinit property adapter has not been initialized
        if (adapter.getIsSelected()) adapter.setIsSelected(false) else adapter.setIsSelected(true)
    }

    override fun onDetach() {
        super.onDetach()
        mListener = null
    }

    override fun onResume() {
        super.onResume()

    }

    interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        fun onFragmentInteraction(uri: Uri)
    }

    companion object {

        // TODO: Rename and change types and number of parameters
        fun newInstance(): BuildingListFragment {
            val fragment = BuildingListFragment()
            return fragment
        }
    }
}
请帮我解决这个问题
谢谢..

您的问题是,在主活动行中调用单击处理程序时,您正在创建片段的新实例

(BuildingListFragment.newInstance() as EditInterface).editClickFromMainActivity()
您需要在当前屏幕上的实际片段实例上调用该方法。有各种各样的方法来解决这个问题,但我认为目前最安全的方法是做类似的事情

iv_edit.setOnClickListener {
    val fragment = supportFragmentManager.findFragmentByTag(FRAGMENT_TAG) as? BuildingListFragment
    fragment?.editClickFromMainActivity()
}

尽管这意味着您还必须在
上的
addFragment
中使用相同的
FRAGMENT\u标记
。替换(R.id.fragmentContainer,FRAGMENT,null)
行(
FRAGMENT\u标记
而不是
null

分享你的等级//RxJava编译'io.reactivex.rxjava2:RxJava:2.1.3'编译'io.reactivex.rxjava2:rxandroid:2.0.1'//Databinding kapt“com.android.Databinding:compiler:3.0.1”//Dagger 2 kapt'com.google.Dagger:Dagger编译器:2.14.1'compile'com.google.Dagger:Dagger:2.14.1'compile'io.nlopez.smartlocation:rx:3.3.1'您还可以包括活动代码吗?我感觉您在创建视图之前调用了
editClickFromMainActivity()
,但没有得到更多,我认为recycleview需要像这样设置适配器recycleview.adapter=片段中的适配器。试试这个,仔细检查你的代码可能是你犯了一个小错误。我已经添加了我的主要活动。谢谢。。。这是我的工作。。。您的一个帮助是如何使用活动和fragment@umeshbaldaniya也许这会有帮助:-如果没有,创建一个新问题
iv_edit.setOnClickListener {
    val fragment = supportFragmentManager.findFragmentByTag(FRAGMENT_TAG) as? BuildingListFragment
    fragment?.editClickFromMainActivity()
}