在 xml 中 set/observe 实时数据的正确方法是什么?调用观察者方法失败

what is the Correct way to set/observe livedata in xml ? Failed to call observer method

我正在尝试将 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.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.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 Class

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_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".CheckoutFragment">

        <TextView
            android:id="@+id/shopping_cart"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            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_width="wrap_content"
            android:layout_height="wrap_content"
            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_width="0dp"
            android:layout_height="wrap_content"
            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_width="0dp"
            android:layout_height="wrap_content"
            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_width="0dp"
            android:layout_height="wrap_content"
            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_width="wrap_content"
            android:layout_height="wrap_content"
            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_width="wrap_content"
            android:layout_height="wrap_content"
            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_width="0dp"
            android:layout_height="0dp"
            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_width="0dp"
            android:layout_height="0dp"
            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>

这是我的片段Class

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

请问我是否需要任何其他数据来诊断问题

我在 Youtube 上观看另一个教程时找到了答案

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

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

例如:-

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

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

例如:-

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

将您的 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
}