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

Time:12-05

I'm trying to bind LiveData directly to xml.

The Code is compiling.. but at Runtime When I enter this fragment I'm getting this error

I have no idea what this error means..

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

Here is my 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) }
}

Here is my fragment Layout

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

and Here is my Fragment 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))
            }
        }
    }
}

Please ask me if you need any other data to diagnose the problem

CodePudding user response:

I figured out the answer while I was watching another tutorial on Youtube

so basically this type of error occurs when you try to bind "non-string" data to text attr in textView

a work-around to this was to use " String.valueOf() " for all non-String data type you want to use in you xml layout

eg:-

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

incase you want to concat some generic text, use ` //string ` (grave accent tags)

eg:-

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

CodePudding user response:

change your onCreateView method to this method

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