简体   繁体   English

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

[英]lateinit property adapter has not been initialized in Kotlin4Android

I have created RecyclerView in fragment inside activity , all are working good but when i do notifyDataSetChanged() to adapter from activity through interface ,I got an error " lateinit property adapter has not been initialized " but I have already initialized adapter 我创建RecyclerViewfragmentactivity ,所有的工作都很好,但是当我这样做notifyDataSetChanged()adapteractivity通过interface ,我得到一个错误“lateinit财产adapter尚未初始化 ”,但我已经initialized adapter

    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
        }
    }
}

My mainActivity 我的主要活动

    class MainActivity : AppCompatActivity() {

    private val mOnNavigationItemSelectedListener =
            BottomNavigationView.OnNavigationItemSelectedListener { item ->
                when (item.itemId) {
                    R.id.navigation_list -> {
                        val fragment = BuildingListFragment.Companion.newInstance();
                        addFragment(fragment, R.anim.slide_re_in, R.anim.slide_re_out)
                        return@OnNavigationItemSelectedListener true
                    }
                    R.id.navigation_map -> {
                        val fragment = BuildingMapFragment.Companion.newInstance();
                        addFragment(fragment, R.anim.slide_in, R.anim.slide_out)
                        return@OnNavigationItemSelectedListener true
                    }
                }
                false
            }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)

        val fragment = BuildingListFragment.Companion.newInstance()
        addFragment(fragment, R.anim.slide_re_in, R.anim.slide_re_out)

        iv_Add.setOnClickListener {
            var intent = Intent(this, AddBuildingMapsActivity::class.java)
            startActivity(intent)
        }
        iv_edit.setOnClickListener {
            (BuildingListFragment.newInstance() as EditInterface).editClickFromMainActivity()
        }
    }


    /**
     * add/replace fragment in container [framelayout]
     */
    private fun addFragment(fragment: Fragment, slide_re_in: Int, slide_re_out: Int) {

        supportFragmentManager
                .beginTransaction()
                .setCustomAnimations(slide_re_in, slide_re_out)
                .replace(R.id.fragmentContainer, fragment, null)//fragment.javaClass.getSimpleName()
                .addToBackStack(null)//fragment.javaClass.getSimpleName()
                .commit()
    }

    override fun onBackPressed() {
        super.onBackPressed()
        Log.e("TAG", "TAG")
    }

    interface EditInterface {
        fun editClickFromMainActivity()
    }
}

please help me to solve this issue thanks.. 请帮我解决这个问题谢谢..

Your problem is that you're creating a new instance of the fragment when the click handler is being called in the main activity line 您的问题是,当您在主活动行中调用单击处理程序时,您正在创建该片段的新实例

(BuildingListFragment.newInstance() as EditInterface).editClickFromMainActivity()

You will need to call the method on the actual fragment instance that's currently on screen. 您需要在当前屏幕上的实际片段实例上调用该方法。 There's various ways of getting around this but I think the safest path for now is doing something like 有各种方法来解决这个问题,但我认为现在最安全的方法是做类似的事情

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

though this will mean that you must also use the same FRAGMENT_TAG in addFragment on the .replace(R.id.fragmentContainer, fragment, null) line ( FRAGMENT_TAG instead of null ) 虽然这意味着您还必须在.replace(R.id.fragmentContainer, fragment, null)行( FRAGMENT_TAG而不是null )的addFragment使用相同的FRAGMENT_TAG

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Lateinit 属性尚未初始化(Android、Kotlin) - Lateinit property has not been initialized (Android, Kotlin) lateinit 属性 mClickListener 尚未初始化 Adapter kotlin - lateinit property mClickListener has not been initialized Adapter kotlin Kotlin Android MVP + Dagger 2 lateinit属性演示者尚未初始化 - Kotlin Android MVP + Dagger 2 lateinit property presenter has not been initialized Kotlin Android 共享首选项 - lateinit 属性首选项尚未初始化 - Kotlin Android Shared Preferences - lateinit property prefs has not been initialized Kotlin / lateinit 属性绑定尚未初始化 - Kotlin / lateinit property binding has not been initialized Android-Kotlin与Dagger2,lateinit属性组件尚未初始化 - Android - Kotlin with Dagger2, lateinit property component has not been initialized Android Kotlin 测试。 lateinit 属性 _db 尚未初始化 - Android Kotlin Testing. lateinit property _db has not been initialized Android 中带有 Kotlin 的匕首:lateinit 属性 viewModelProviderFactory 尚未初始化 - Dagger with Kotlin in Android: lateinit property viewModelProviderFactory has not been initialized “kotlin.UninitializedPropertyAccessException: lateinit property madapter has not been initialized at...”设置 RecyclerView Adapter 时 - "kotlin.UninitializedPropertyAccessException: lateinit property madapter has not been initialized at..." when setting up RecyclerView Adapter Lateinit 属性适配器尚未初始化,使用 Fragment 设置 RecyclerView - lateinit property adapter has not been initialized setting up RecyclerView with Fragment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM