在 xml 中设置/观察 livedata 的正确方法是啥?调用观察者方法失败

Posted

技术标签:

【中文标题】在 xml 中设置/观察 livedata 的正确方法是啥?调用观察者方法失败【英文标题】:what is the Correct way to set/observe livedata in xml ? Failed to call observer method在 xml 中设置/观察 livedata 的正确方法是什么?调用观察者方法失败 【发布时间】:2022-01-10 20:54:12 【问题描述】:

我正在尝试将 LiveData 直接绑定到 xml。

代码正在编译.. 但是在运行时当我输入这个片段时,我收到了这个错误

我不知道这个错误是什么意思..

2021-12-05 11:43:00.759 8215-8215/com.example.fragmentnavigation E/androidRuntime: FATAL EXCEPTION: main
    Process: com.example.fragmentnavigation, PID: 8215
    java.lang.RuntimeException: Failed to call observer method
        at androidx.lifecycle.ClassesInfoCache$MethodReference.invokeCallback(ClassesInfoCache.java:232)
        at androidx.lifecycle.ClassesInfoCache$CallbackInfo.invokeMethodsForEvent(ClassesInfoCache.java:199)
        at androidx.lifecycle.ClassesInfoCache$CallbackInfo.invokeCallbacks(ClassesInfoCache.java:190)
        at androidx.lifecycle.ReflectiveGenericLifecycleObserver.onStateChanged(ReflectiveGenericLifecycleObserver.java:40)
        at androidx.lifecycle.LifecycleRegistry$ObserverWithState.dispatchEvent(LifecycleRegistry.java:354)
        at androidx.lifecycle.LifecycleRegistry.forwardPass(LifecycleRegistry.java:265)
        at androidx.lifecycle.LifecycleRegistry.sync(LifecycleRegistry.java:307)
        at androidx.lifecycle.LifecycleRegistry.moveToState(LifecycleRegistry.java:148)
        at androidx.lifecycle.LifecycleRegistry.handleLifecycleEvent(LifecycleRegistry.java:134)
        at androidx.fragment.app.FragmentViewLifecycleOwner.handleLifecycleEvent(FragmentViewLifecycleOwner.java:88)
        at androidx.fragment.app.Fragment.performStart(Fragment.java:3028)
        at androidx.fragment.app.FragmentStateManager.start(FragmentStateManager.java:589)
        at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:300)
        at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189)
        at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2106)
        at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:2002)
        at androidx.fragment.app.FragmentManager$5.run(FragmentManager.java:524)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
     Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x1
        at android.content.res.Resources.getText(Resources.java:444)
        at android.widget.TextView.setText(TextView.java:6412)
        at com.example.fragmentnavigation.databinding.FragmentCheckoutBindingImpl.executeBindings(FragmentCheckoutBindingImpl.java:246)
        at androidx.databinding.ViewDataBinding.executeBindingsInternal(ViewDataBinding.java:512)
        at androidx.databinding.ViewDataBinding.executePendingBindings(ViewDataBinding.java:484)
        at androidx.databinding.ViewDataBinding$OnStartListener.onStart(ViewDataBinding.java:1706)
        at java.lang.reflect.Method.invoke(Native Method)
        at androidx.lifecycle.ClassesInfoCache$MethodReference.invokeCallback(ClassesInfoCache.java:222)
        at androidx.lifecycle.ClassesInfoCache$CallbackInfo.invokeMethodsForEvent(ClassesInfoCache.java:199) 
        at androidx.lifecycle.ClassesInfoCache$CallbackInfo.invokeCallbacks(ClassesInfoCache.java:190) 
        at androidx.lifecycle.ReflectiveGenericLifecycleObserver.onStateChanged(ReflectiveGenericLifecycleObserver.java:40) 
        at androidx.lifecycle.LifecycleRegistry$ObserverWithState.dispatchEvent(LifecycleRegistry.java:354) 
        at androidx.lifecycle.LifecycleRegistry.forwardPass(LifecycleRegistry.java:265) 
        at androidx.lifecycle.LifecycleRegistry.sync(LifecycleRegistry.java:307) 
        at androidx.lifecycle.LifecycleRegistry.moveToState(LifecycleRegistry.java:148) 
        at androidx.lifecycle.LifecycleRegistry.handleLifecycleEvent(LifecycleRegistry.java:134) 
        at androidx.fragment.app.FragmentViewLifecycleOwner.handleLifecycleEvent(FragmentViewLifecycleOwner.java:88) 
        at androidx.fragment.app.Fragment.performStart(Fragment.java:3028) 
        at androidx.fragment.app.FragmentStateManager.start(FragmentStateManager.java:589) 
        at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:300) 
        at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189) 
        at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2106) 
        at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:2002) 
        at androidx.fragment.app.FragmentManager$5.run(FragmentManager.java:524) 
        at android.os.Handler.handleCallback(Handler.java:938) 
        at android.os.Handler.dispatchMessage(Handler.java:99) 
        at android.os.Looper.loop(Looper.java:223) 
        at android.app.ActivityThread.main(ActivityThread.java:7656) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 

这是我的 ViewModel 类

package com.example.fragmentnavigation.vm

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.example.fragmentnavigation.products

import androidx.lifecycle.ViewModel
import com.example.fragmentnavigation.Product

class CheckoutViewModel(id:Int, quantity:Int): ViewModel()

    private val noice = products.find  it.id == id 

    private val _product = MutableLiveData<Product>(noice)
    private val _product_name = MutableLiveData<String>(noice?.name)
    private val _product_price = MutableLiveData<Float>(noice?.price)
    private val _product_shortDescription = MutableLiveData<String>(noice?.shortDescription)
    private val _product_longDescription = MutableLiveData<String>(noice?.longDescription)
    private var _qty = MutableLiveData(quantity)

    val product:LiveData<Product>
        get() = _product

    val product_name:LiveData<String>
        get() = _product_name

    val product_price: LiveData<Float>
        get() = _product_price

    val product_short_desription:LiveData<String>
        get() = _product_shortDescription

    val product_long_desription:LiveData<String>
        get() = _product_longDescription

    val product_image_id:Int?
        get() = _product.value?.imageId

    val order_total_price:String
        get() = "Order Total: " + ((_product.value?.price)?.times(qty)) .toString()

    val qty:LiveData<Int>
        get() = _qty


    fun addQty(quantity:Int)
        _qty.value?.let 
            _qty.value = it + quantity
        
    

    fun decrQty(quantity: Int)
        _qty.value?.let 
            if(quantity - it > 0)
                _qty.value = it - quantity
            
        
    



private fun Float?.times(qty: LiveData<Int>): Float? 

    return this?.let  qty.value?.times(it) 

这是我的片段布局

<?xml version="1.0" encoding="utf-8"?>
<layout>
    <data>
        <variable
            name="viewmodel"
            type="com.example.fragmentnavigation.vm.CheckoutViewModel" />
    </data>
    <androidx.constraintlayout.widget.ConstraintLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_
        android:layout_
        tools:context=".CheckoutFragment">

        <TextView
            android:id="@+id/shopping_cart"
            android:layout_
            android:layout_
            android:layout_marginStart="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="16dp"
            android:layout_marginEnd="16dp"
            android:layout_marginRight="16dp"
            android:text="@string/shopping_cart"
            android:textSize="30sp"
            android:textStyle="bold"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <ImageView
            android:id="@+id/product_image"
            android:layout_
            android:layout_
            android:layout_marginStart="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="16dp"
            android:contentDescription="@string/product_image"
            android:src="@drawable/pixel3"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/shopping_cart" />

        <TextView
            android:id="@+id/product_price"
            android:layout_
            android:layout_
            android:layout_marginStart="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="16dp"
            android:layout_marginEnd="16dp"
            android:layout_marginRight="16dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toEndOf="@+id/product_image"
            app:layout_constraintTop_toBottomOf="@+id/product_name"

            android:text="@String.valueOf(viewmodel.product_price)"

            tools:text="Price: Rs 65000" />

        <TextView
            android:id="@+id/product_name"
            android:layout_
            android:layout_
            android:layout_marginStart="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="16dp"
            android:layout_marginEnd="16dp"
            android:layout_marginRight="16dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/product_image"
            app:layout_constraintTop_toBottomOf="@+id/shopping_cart"

            android:text="@viewmodel.product.name"

            tools:text="PIXEL 3a"/>

        <TextView
            android:id="@+id/product_quantity"
            android:layout_
            android:layout_
            android:layout_marginStart="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="16dp"
            app:layout_constraintStart_toEndOf="@+id/product_image"
            app:layout_constraintTop_toBottomOf="@+id/product_price"

            android:text="@viewmodel.qty"

            tools:text="Qty: 1"/>

        <TextView
            android:id="@+id/order_total"
            android:layout_
            android:layout_
            android:layout_marginTop="24dp"
            android:textSize="24sp"
            android:textStyle="bold"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/product_quantity"

            android:text="@String.valueOf(viewmodel.product_price)"

            tools:text="@string/order_total" />

        <Button
            android:id="@+id/checkout"
            android:layout_
            android:layout_
            android:layout_marginTop="16dp"
            android:text="@string/checkout"
            app:layout_constraintEnd_toEndOf="@+id/order_total"
            app:layout_constraintStart_toStartOf="@+id/order_total"
            app:layout_constraintTop_toBottomOf="@+id/order_total" />

        <Button
            android:id="@+id/add_btn"
            android:layout_
            android:layout_
            android:layout_marginVertical="8dp"
            android:text="+"

            android:onClick="@()-> viewmodel.addQty(1)"

            app:layout_constraintBottom_toTopOf="@+id/order_total"
            app:layout_constraintEnd_toEndOf="@+id/product_price"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toEndOf="@+id/del_btn"
            app:layout_constraintTop_toBottomOf="@+id/product_price" />

        <Button
            android:id="@+id/del_btn"
            android:layout_
            android:layout_
            android:layout_marginVertical="8dp"
            android:text="-"

            android:onClick="@()-> viewmodel.decrQty(1)"

            app:layout_constraintBottom_toTopOf="@+id/order_total"
            app:layout_constraintEnd_toStartOf="@+id/add_btn"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toEndOf="@+id/checkout"
            app:layout_constraintTop_toBottomOf="@+id/product_price" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

这是我的片段类

package com.example.fragmentnavigation


import android.annotation.SuppressLint
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.fragment.findNavController
import com.example.fragmentnavigation.databinding.FragmentCheckoutBinding
import com.example.fragmentnavigation.vm.CheckoutVMFactory
import com.example.fragmentnavigation.vm.CheckoutViewModel
import kotlinx.android.synthetic.main.fragment_checkout.*


class CheckoutFragment : Fragment() 

    lateinit var bind: FragmentCheckoutBinding

    lateinit var vm : CheckoutViewModel

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

        val id = CheckoutFragmentArgs.fromBundle(requireArguments()).id

        val vmFactory = CheckoutVMFactory(id,1)

        vm  = ViewModelProvider(viewModelStore,vmFactory).get(CheckoutViewModel::class.java)
    

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View? 
        // Inflate the layout for this fragment
        
        bind = DataBindingUtil.inflate(inflater,R.layout.fragment_checkout,container,false)

        bind.viewmodel = vm

        bind.lifecycleOwner = viewLifecycleOwner

        return bind.root
    


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) 
    
        vm.product.observe(viewLifecycleOwner, 
            setData(it)
        )

    
    
    fun setData(noi_product : Product) 
        with(noi_product)
            product_image.setImageResource(imageId)

            checkout.setOnClickListener 
               
    findNavController().navigate(CheckoutFragmentDirections.actionCheckoutToThanks(this.id))
            
        
    


如果您需要任何其他数据来诊断问题,请询问我

【问题讨论】:

为什么要使用实时数据,为什么不直接使用ObservableField&lt;Type&gt; 我不熟悉 [ObservableField] 但似乎应该在片段/活动文件中使用它我试图直接将实时数据绑定到 xml ObservableField 作为示例,可以在您的视图模型中使用并清理您的代码 【参考方案1】:

我在 Youtube 上看另一个教程时想出了答案

所以当您尝试将“非字符串”数据绑定到 textView 中的文本属性时,基本上会发生这种类型的错误

解决这个问题的方法是对您要在 xml 布局中使用的所有非字符串数据类型使用“String.valueOf()”

例如:-

android:text = " @ String.valueOf( //code )  "

如果您想连接一些通用文本,请使用 ` //string `(重音标签)

例如:-

android:text = " @ `something` + String.valueOf( //code )  "

【讨论】:

【参考方案2】:

把你的 onCreateView 方法改成这个方法

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
): View? 
    // Inflate the layout for this fragment
    
    bind = DataBindingUtil.inflate(inflater,R.layout.fragment_checkout,container,false)

    bind.viewmodel = vm

    bind.lifecycleOwner = viewLifecycleOwner

    bind.executePendingBindings()

    return bind.root

【讨论】:

.executePendingBindings() 有什么作用? "评估挂起的绑定,更新任何将表达式绑定到已修改变量的视图。"

以上是关于在 xml 中设置/观察 livedata 的正确方法是啥?调用观察者方法失败的主要内容,如果未能解决你的问题,请参考以下文章

如何在 DataProvider 类 Alamofire 函数 swift 中设置协议函数不能正确调用

jetpack-liveData 原理解析

视图不会在 RelativeLayout 中正确对齐(Java 代码中设置的视图宽度)

在科尔多瓦插件中设置 NSNotification 观察者

exec-maven-plugin 使用 DynamoDBLocal 时如何在 pom.xml 中设置 mainClass [重复]

如何使用 LiveData 正确更新 Android 的 RecyclerView?