Home > other >  Type mismatch: inferred type is String! but Unit was expected
Type mismatch: inferred type is String! but Unit was expected

Time:03-01

I'm have the error "Type mismatch: inferred type is String! but Unit was expected", but if I try to use quick fix I do an infinite loop with that error and "Return type of 'run' is not a subtype of the return type of the overridden member [...]" while using this code in theses files:

ClockViewModel.kt:

package com.everyapp.clock20.ui.clock

import android.annotation.SuppressLint
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import java.text.SimpleDateFormat
import java.util.*


class ClockViewModel : ViewModel() {

    private fun timing() {
        Timer().schedule(object : TimerTask() {
            @SuppressLint("SimpleDateFormat")
            override fun run() {
                val simpleDateFormat = SimpleDateFormat("HH:mm:ss")
                return simpleDateFormat.format(Date())

            }
        },0, 100)
    }

    private val _text = MutableLiveData<String>().apply {
        value = timing().toString()
    }
    val text: LiveData<String> = _text
}

ClockFragment.kt:

package com.everyapp.clock20.ui.clock

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import com.everyapp.clock20.databinding.FragmentClockBinding

class ClockFragment : Fragment() {

    private var _binding: FragmentClockBinding? = null

    // This property is only valid between onCreateView and
    // onDestroyView.
    private val binding get() = _binding!!

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        val clockViewModel =
            ViewModelProvider(this).get(ClockViewModel::class.java)

        _binding = FragmentClockBinding.inflate(inflater, container, false)
        val root: View = binding.root

        val textView: TextView = binding.textClock
        clockViewModel.text.observe(viewLifecycleOwner) {
            textView.text = it
        }
        return root
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}

I was expecting to show text and updating it every second.

CodePudding user response:

Run doesn't have a return value. It returns void. So your run function shouldn't return anything.

CodePudding user response:

You need to change the value of variable in ViewModel.

And I delete some unnecessary code.

try this code

class ClockViewModel : ViewModel() {

    private fun timing() {
        Timer().schedule(object : TimerTask() {
            @SuppressLint("SimpleDateFormat")
            override fun run() {
                val simpleDateFormat = SimpleDateFormat("HH:mm:ss")
                text.postValue(simpleDateFormat.format(Date()))
            }
        },0, 100)
    }

    val text = MutableLiveData<String>()
}
  • Related