本文介绍了微调监听器LiveData问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的片段中,我有两个微调工具。

两者都由在ViewModel中观察到的LiveData填充,如下所示:

数据-lang="js"数据-隐藏="假"数据-控制台="真"数据-巴贝尔="假">
      // Observe Filtered ProductGroups and populate Spinner
        businessViewModel.filteredAppDataProductGroups.observe(viewLifecycleOwner, { productGroupArrayList ->
            if (!productGroupArrayList.isNullOrEmpty()){
                val adapter = ArrayAdapter(requireContext(), android.R.layout.simple_spinner_dropdown_item, productGroupArrayList)
                binding.inventoryAddEditProductGroupSpinner.adapter = adapter
            }
        })


// Observe Filtered ProductTypes and populate Spinner
        businessViewModel.filteredAppDataProductTypes.observe(viewLifecycleOwner, { productTypeArrayList ->
            if (productTypeArrayList != null){
                val adapter = ArrayAdapter(requireContext(), android.R.layout.simple_spinner_dropdown_item, productTypeArrayList)
                binding.inventoryAddEditProductTypeSpinner.adapter = adapter
            }
        })

这很好用,但我正在尝试根据当前选择的第一个微调控件来过滤第二个微调控件中的数据,方法是设置一个侦听器并更新视图模型数据,如下所示:

数据-lang="js"数据-隐藏="假"数据-控制台="真"数据-巴贝尔="假">
        binding.inventoryAddEditProductGroupSpinner.onItemSelectedListener = object: AdapterView.OnItemSelectedListener{

            override fun onItemSelected(parent:AdapterView<*>, view: View, position: Int, id: Long){

                val productGroupObject = parent.selectedItem as ProductGroupObject
                if (productGroupObject.productGroupID.isNotEmpty()){
                    businessViewModel.updateCurrentProductGroupVMLiveData(productGroupObject.productGroupID)
                }
            }

            override fun onNothingSelected(parent: AdapterView<*>){
            }
        }

更新ViewModel中的筛选列表,如下所示:

数据-lang="js"数据-隐藏="假"数据-控制台="真"数据-巴贝尔="假">
    fun updateCurrentProductGroupVMLiveData (currentProductGroupId: String) {
        val newProductGroup = allAppDataProductGroups.value?.find { productGroup -> productGroup.productGroupID == currentProductGroupId }
        _currentProductGroup.value = newProductGroup
        if(newProductGroup?.productGroup != null) {
            val filteredProductsList = allAppDataProductTypes.value?.filter { productTypeObject -> productTypeObject.productGroup == newProductGroup.productGroup} as ArrayList<ProductTypeObject>
            _filteredAppDataProductTypes.value = filteredProductsList
        }
        Log.d(TAG, "updateCurrentProductGroupVMLiveData(): '_currentProductGroupId.value' updated ($currentProductGroupId)")
    }

但是,如果我将显示的代码添加到onItemSelected中,应用程序会崩溃,并看到以下错误:

数据-lang="js"数据-隐藏="假"数据-控制台="真"数据-巴贝尔="假">
    --------- beginning of crash
2021-10-06 23:06:34.740 17510-17510/com.xxxxx.acorn E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.locators.acorn, PID: 17510
    java.lang.NullPointerException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkNotNullParameter, parameter view
        at com.xxxxx.acorn.business.inventory.InventoryAddEditFragment$setSpinnerListeners$2.onItemSelected(InventoryAddEditFragment.kt)
        at android.widget.AdapterView.fireOnSelected(AdapterView.java:931)
        at android.widget.AdapterView.dispatchOnItemSelected(AdapterView.java:920)
        at android.widget.AdapterView.-wrap1(AdapterView.java)
        at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:890)
        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:6077)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

我发现ViewModel列表正在更改,所以我不知道这为什么会导致NullPointerException。

推荐答案

错误日志似乎指向view(在第3行)

java.lang.NullPointerException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkNotNullParameter, parameter view
我认为parent: AdapterView<*>view: View是可以为空的类型。因此需要为该类型添加空的安全调用运算符

binding.inventoryAddEditProductGroupSpinner.onItemSelectedListener = object: AdapterView.OnItemSelectedListener{

         override fun onItemSelected(parent:AdapterView<*>?, view: View?, position: Int, id: Long){
            ...
         }

         override fun onNothingSelected(parent: AdapterView<*>?){
         }
     }

这篇关于微调监听器LiveData问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 19:56