当我将一个对象注入到我的匕首柄模块中明确提供的视图模型时,为什么我会遇到 nosuchmethod 异常

问题描述 投票:0回答:1

任何帮助将不胜感激,因为我已经处理这个问题好几天了。

这是错误消息;

由以下原因引起:java.lang.NoSuchMethodException:com.example.funditech.presentation.navigationDrawer.NavigationDrawerViewModel。 [] 在 java.lang.Class.getConstructor0(Class.java:2332) 在 java.lang.Class.getDeclaredConstructor(Class.java:2170) 在 androidx.lifecycle.ViewModelProvider$NewInstanceFactory.create

这是我的依赖;

`plugins {
    id("com.android.application")
    id("org.jetbrains.kotlin.android")
    id("org.jetbrains.kotlin.plugin.serialization") version "1.9.0"
    kotlin("kapt")
    id("com.google.dagger.hilt.android") version "2.48.1."

}`

  implementation ("com. google. dagger: hilt-android:2.48") kapt("com. google. dagger: hilt-android-compiler:2.48")

这就是我的类路径中的内容;`

plugins {
    id("com.android.application") version "8.1.2" apply false.
    id("org.jetbrains.kotlin.android") version "1.9.0" apply false.
}
buildscript {

    dependencies {

        classpath ("com.google.dagger: hilt-android-gradle-plugin:2.48.1")
        classpath ("com.google.gms: google-services:4.4.0")
        classpath ("org.jetbrains.kotlin:kotlin-gradle-plugin")


    }
    repositories {
        mavenCentral()
    }
}`

这是我试图注入的类。

package com.example.funditech.preferenceManager

import android.content.Context
import android.content.SharedPreferences
class FunditechPreferenceManager(val context: Context)  {


    private val sharedPreferences: SharedPreferences =
        context.getSharedPreferences("funditechUser", Context.MODE_PRIVATE)


    override fun saveString(key: String, value: String) {
        sharedPreferences.edit().putString(key, value).apply()
    }

    override fun getString(key: String, defaultValue: String): String {
        return sharedPreferences.getString(key, defaultValue) ?: defaultValue
    }

}

这是我的刀柄模块对象。

@Module
@InstallIn(SingletonComponent::class)
object Module {

    @Provides
    @Singleton
    fun shared Preference(@ApplicationContext context: Context): FunditechPreferenceManager {
        return FunditechPreferenceManager(context)
    }
}

这是视图模型;

@HiltViewModel
class NavigationDrawerViewModel @Inject constructor(
  private Val funditechPreferenceManager: FunditechPreferenceManager
) : ViewModel() {
}

我尝试过使用不同版本的匕首,但没有结果。 我也尝试过ksp插件,同样的错误。

我还使用最新的 gradle 版本 8.4。

dependency-injection sharedpreferences android-viewmodel dagger-hilt nosuchmethoderror
1个回答
0
投票

您需要在 FunditechPreferenecManager 类中注入构造函数

更新代码:

// Inject constructor here
class FunditechPreferenceManager @Inject constructor(val context: Context)  {


private val sharedPreferences: SharedPreferences =
    context.getSharedPreferences("funditechUser", Context.MODE_PRIVATE)


override fun saveString(key: String, value: String) {
    sharedPreferences.edit().putString(key, value).apply()
}

override fun getString(key: String, defaultValue: String): String {
    return sharedPreferences.getString(key, defaultValue) ?: defaultValue
}
}
© www.soinside.com 2019 - 2024. All rights reserved.