title: How to implement by viewModels?
body: <p>I have been trying to implement the LiveData and View Model. In this App, a number is displayed on the screen and we can add five or minus one. Now Earlier I used viewModelProvider but now it is deprecated and I am using "by viewmodels" but it is showing ann error </p>
  
  <p><strong>fragOne.kt</strong></p>
  
  <pre><code>class fragOne : Fragment() {
      override fun onCreateView(
          inflater: LayoutInflater, container: ViewGroup?,
          savedInstanceState: Bundle?
      ): View? {
          // Inflate the layout for this fragment
          var binding = DataBindingUtil.inflate&lt;FragmentFragoneBinding&gt;(
              inflater,
              R.layout.fragment_fragone,
              container,
              false
          )
  
          //viewModel = ViewModelProviders.of(this).get(fragViewModel::class.java)
          // Create the observer which updates the UI.
          val viewModel: fragViewModel by **viewModels()**
          val nameObserver = Observer&lt;Int&gt; {
              viewModel.num.value=it
  
          }
          // Observe the LiveData, passing in this activity as the LifecycleOwner and the observer.
          viewModel.num.observe(viewLifecycleOwner,nameObserver)
          // setting on Click listener for add button
          binding.add.setOnClickListener()
          {
  
              //updateNumber()
              viewModel.num.setValue(viewModel.addFive())
          }
  
          // setting on on Click Listener for minus button
          binding.minus.setOnClickListener()
          {
              viewModel.num.setValue(viewModel.minusOne())
              //updateNumber()
          }
  
  
          return binding.root
      }
  
          // Always call the superclass so it can save the view hierarchy state
  }
  </code></pre>
  
  <p><strong>fragViewModel</strong></p>
  
  <pre><code>import androidx.lifecycle.ViewModel
  
  class fragViewModel:ViewModel()
  {
      val num: MutableLiveData&lt;Int&gt; by lazy {
          MutableLiveData&lt;Int&gt;()
      }
      // Initializing num=0
      init {
           num .value= 0
      }
      // Functions to add five or subtract one
  
      fun addFive():Int
      {
          var newNumber:Int?=num.value
  
          return newNumber?.plus(5)?:0
      }
  
       fun minusOne():Int
      {
          var newNumber:Int?=num.value
  
          return newNumber?.minus(1)?:0
      }
  }
  </code></pre>
  
  <p>In <strong>fragOne</strong> there is an error showing in <strong>by viewModels</strong> . So whats the error.
  please let me know if anyone wants the code I will upload it in Github</p>
  
  <p><strong>build.gradle</strong></p>
  
  <pre><code>    def lifecycle_version = "2.2.0"
      implementation fileTree(dir: 'libs', include: ['*.jar'])
      implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
      implementation 'androidx.appcompat:appcompat:1.1.0'
      implementation 'androidx.core:core-ktx:1.2.0'
      implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
      implementation 'androidx.legacy:legacy-support-v4:1.0.0'
      testImplementation 'junit:junit:4.12'
      androidTestImplementation 'androidx.test.ext:junit:1.1.1'
      androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
      implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
      implementation "androidx.core:core-ktx:1.2.0"
      // ViewModel
      implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
  
  
  }
  </code></pre>