lateinit var结果在Kotlin中始终未初始化

z9gpfhce  于 2023-11-21  发布在  Kotlin
关注(0)|答案(1)|浏览(189)

我必须在我的覆盖函数notifyAllIslogic中使用selectAllCheckBox,但是变量selectAllCheckBox显示为未初始化,即使它已经在onCreate方法中初始化了。
问题出在onCreate、lateinit声明和notifyAllIslogy函数中。

package com.example.lslmbeta

import android.annotation.SuppressLint
import android.os.Bundle
import android.util.Log
import android.view.Menu
import android.view.MenuItem
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.WindowCompat
import androidx.core.view.isVisible
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.lslmbeta.databinding.ActivityMainBinding
import com.example.lslmbeta.datamanagement.dataReader
import com.example.lslmbeta.recyclerview.ItemAdapter
import com.google.android.material.appbar.MaterialToolbar
import com.google.android.material.checkbox.MaterialCheckBox
import kotlinx.coroutines.launch
import java.text.SimpleDateFormat

class MainActivity : AppCompatActivity(), OnAllIsCheckedListener {
    companion object {
        var isInSelectionMode: Boolean? = null
    }

    private val MAIN_ACTIVITY_TAG = "Main Activity"

    private lateinit var recyclerView: RecyclerView

    private lateinit var closeView: MenuItem
    private lateinit var selectView: MenuItem
    private lateinit var moreView: MenuItem

    private lateinit var itemAdapter: ItemAdapter
    private lateinit var selectAllCheckBox: MaterialCheckBox

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        menuInflater.inflate(R.menu.top_app_bar, menu)

        // Menu Group Selection
        closeView = menu.findItem(R.id.menu_item_close)

        // Menu Group Non Selection
        selectView = menu.findItem(R.id.menu_item_select)
        moreView = menu.findItem(R.id.menu_item_more)

        enableOrDisableSelection()

        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        return when (item) {
            closeView -> {
                enableOrDisableSelection()
                true
            }

            selectView -> {
                if (::itemAdapter.isInitialized) enableOrDisableSelection()
                true
            }

            moreView -> {
                // Gestisci il clic sull'elemento "More" (menu a scomparsa)
                true
            }

            else -> super.onOptionsItemSelected(item)
        }
    }

    //TODO togliere il suppress
    @SuppressLint("SimpleDateFormat")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val binding = ActivityMainBinding.inflate(layoutInflater)
        val view = binding.root
        setContentView(view)

        val topToolBar = binding.topToolBar
        setSupportActionBar(topToolBar)

        WindowCompat.setDecorFitsSystemWindows(window, false)

        val dateString = "19/10/2023"
        val dateFormat = SimpleDateFormat("dd/MM/yyyy")
        val date = dateFormat.parse(dateString)

        recyclerView = binding.homeRecycleView
        selectAllCheckBox = binding.selectAllCheckbox

        Log.d(MAIN_ACTIVITY_TAG, "Select All Checkbox: ${::selectAllCheckBox.isInitialized}")

        selectAllCheckBox.setOnClickListener {
            if (selectAllCheckBox.isChecked) {
                itemAdapter.checkAll()
            } else {
                itemAdapter.clearCheckedItems()
            }
        }

        lifecycleScope.launch {
            val myDataset = dataReader(this@MainActivity, date)
            itemAdapter = ItemAdapter(myDataset)
            recyclerView.layoutManager = LinearLayoutManager(this@MainActivity)
            recyclerView.adapter = itemAdapter
            recyclerView.setHasFixedSize(true)

            itemAdapter.setCustomListener(MainActivity())
        }
    }

    override fun notifyAllIsChecked(isAllChecked: Boolean) {
        Log.d(MAIN_ACTIVITY_TAG, "Is Ready: ${::selectAllCheckBox.isInitialized}")

        if (::selectAllCheckBox.isInitialized) selectAllCheckBox.isChecked = isAllChecked
    }

    @SuppressLint("NotifyDataSetChanged")
    private fun enableOrDisableSelection() {
        val topToolBar = findViewById<MaterialToolbar>(R.id.topToolBar)
        val checkBox = findViewById<MaterialCheckBox>(R.id.select_all_checkbox)

        if (isInSelectionMode == null) {
            isInSelectionMode = false
            Log.w("Enable Or Disable Selection", "Is NULL")
        } else {
            isInSelectionMode = !isInSelectionMode!!
        }

        Log.d("Enable Or Disable Selection", isInSelectionMode.toString())

        // Group Selection
        closeView.isVisible = isInSelectionMode!!

        checkBox.isVisible = isInSelectionMode!!

        // Group Non Selection
        selectView.isVisible = !isInSelectionMode!!
        moreView.isVisible = !isInSelectionMode!!

        topToolBar.title = if (isInSelectionMode!!) "" else getString(R.string.app_name)

        if (isInSelectionMode!!) {
            itemAdapter.clearCheckedItems()
        } else {
            recyclerView.adapter?.notifyDataSetChanged()
        }
    }
}

字符串
我使用了logs,注意到它在notifyAllIslogs方法调用之前被初始化。

2023-10-29 11:56:16.938 24963-24963 Main Activity           com.example.lslmbeta                 D  Select All Checkbox: true
2023-10-29 12:09:12.864 24963-24963 Main Activity           com.example.lslmbeta                 D  Is Ready: false

gcmastyq

gcmastyq1#

我认为问题就在这里:

itemAdapter.setCustomListener(MainActivity())

字符串
我们不应该手动示例化Activity。作为侦听器传递的Activity未初始化,它不能用于任何事情。
相反,您可能应该传递当前Activity:this@MainActivity

相关问题