0

Last time I did this the project ran just fine but I am getting these errors now, I tried installing kotlin-reflect but still it didn't work. It doesn't recognise lazy and with either and I simply don't know what to do anymore. Lazy and ::class.java unresolved references

package io.keepcoding.mvvmarchitecture.ui

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import androidx.navigation.fragment.findNavController
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import io.keepcoding.mvvmarchitecture.utils.CustomViewModelFactory
import io.keepcoding.mvvmarchitecture.R
import kotlinx.android.synthetic.main.fragment_first.*

/**
 * A simple [Fragment] subclass as the default destination in the navigation.
 */
class FirstFragment : Fragment() {

    private val viewModel: FragmentOrActivityViewModel by lazy {
        val factory = CustomViewModelFactory(requireActivity().application)
        ViewModelProvider(this, factory).get(FragmentOrActivityViewModel::class.java)
    }
    override fun onCreateView(
            inflater: LayoutInflater, container: ViewGroup?,
            savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_first, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        //With kotlinx we replace by button_first.setOnClickListener {
        // }
        //view.findViewById<Button>(R.id.button_first).setOnClickListener {
         //   findNavController().navigate(R.id.action_FirstFragment_to_SecondFragment)
        //}
    }
    private fun fetchData() {
        //viewModel.fetchPOJORetrofitResponses
    }
    private fun setUpObservers() {
        fetchData()
        //viewModel.getPOJORetrofitResponses().observe(viewLifecycleOwner, Observer {
        //   when(it.status) {
        //      Status.SUCCESS -> {
        //          we fill our POJORetrofitResponseItemViewModel array by setting it to it.data then we modify the ui
        //          then we set the elements of our recyclerview adapter
        //          and set the recyclerview adapter here
        //      }
        //      Status.LOADING -> {
        //
        //      }
        //      Status.ERROR -> {
        //
        //      }
        //   }
        //
        // })
    }
}

with and ::class.java unresolved references

package io.keepcoding.mvvmarchitecture.utils

import android.app.Application
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import io.keepcoding.mvvmarchitecture.repository.local.LocalHelper
import io.keepcoding.mvvmarchitecture.repository.local.LocalHelperImpl
import io.keepcoding.mvvmarchitecture.repository.remote.ApiHelper
import io.keepcoding.mvvmarchitecture.repository.remote.ApiHelperImpl
import io.keepcoding.mvvmarchitecture.ui.FragmentOrActivityViewModel

import java.lang.IllegalArgumentException
//@Suppress("UNCHECKED_CAST")
class CustomViewModelFactory(private val application: Application) : ViewModelProvider.NewInstanceFactory() {
    private val apiHelper: ApiHelper = ApiHelperImpl()
    private val localHelper: LocalHelper = LocalHelperImpl(context = application.applicationContext)

    override fun <T : ViewModel> create(modelClass: Class<T>) : T {
        return with(modelClass) {
            when {
                //We must add another choice we may have to add isAssignableFrom(viewModel::class.java)
                FragmentOrActivityViewModel::class.java -> FragmentOrActivityViewModel(application, apiHelper, localHelper)
                else -> throw IllegalArgumentException("Unknown ViewModel")
            }
        } as T
    }
}

Gradle file

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-kapt'
    id 'kotlin-android-extensions'
}

android {
    compileSdkVersion 29

    defaultConfig {
        applicationId "io.keepcoding.mvvmarchitecture"
        minSdkVersion 16
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
}

dependencies {

    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.4.0'
    implementation 'com.squareup.retrofit2:retrofit:2.9.0'
    implementation 'org.jetbrains.kotlin:kotlin-reflect:1.6.10'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.2'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0'
    implementation 'androidx.navigation:navigation-fragment-ktx:2.2.2'
    implementation 'androidx.navigation:navigation-ui-ktx:2.2.2'
    implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
    implementation 'com.squareup.okhttp3:logging-interceptor:4.0.1'
    implementation 'org.jetbrains.kotlin:kotlin-reflect:1.6.20'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

    // Gson
    implementation 'com.google.code.gson:gson:2.8.6'

    // Glide
    implementation ("com.github.bumptech.glide:glide:4.11.0") {
        exclude group: "com.android.support"
    }

    // Coroutines

    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.7'
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.0"

    implementation 'androidx.room:room-runtime:2.2.5'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    kapt "androidx.room:room-compiler:2.3.0-alpha02"
}
3
  • try this ..stackoverflow.com/a/34625454/15597511 Commented Apr 13, 2022 at 11:22
  • Hi what you mean is that the android studio and my kotlin versions don’t match? So I have to update both am I right? Commented Apr 13, 2022 at 11:57
  • yes ! if that doesnt fix it u have other ways by clearing the cache and cleaning the maven and reinstalling them Commented Apr 13, 2022 at 12:00

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.