当我尝试从活动应用程序转换到自定义应用程序类时出现致命异常

发布于 2025-01-11 05:14:07 字数 1913 浏览 0 评论 0原文

我需要从活动应用程序到我的自定义应用程序类进行转换 (需要它将存储库传递给 viewModelFactory 并在 viewModel 中使用它) 但在运行时我遇到了致命异常...我做错了什么?

这是我的自定义应用程序类:

class UserApplication : Application() {

  
    private val database by lazy { UsersRoomDatabase.getDatabase(this@UserApplication) }

   
    val repository by lazy { Repository(database.userDou()) }
}

尝试运行此:

val userApplication = requireActivity().application as UserApplication

例外:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.mytaskgame, PID: 7703
    java.lang.ClassCastException: android.app.Application cannot be cast to com.mytaskgame.application.UserApplication
        at com.mytaskgame.view.logIn.fragment.SignInFragment.onCreate(SignInFragment.kt:35)
        at androidx.fragment.app.Fragment.performCreate(Fragment.java:3009)
        at androidx.fragment.app.FragmentStateManager.create(FragmentStateManager.java:474)
        at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:257)
        at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1862)
        at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1786)
        at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1723)
        at androidx.fragment.app.FragmentManager$4.run(FragmentManager.java:510)
        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)

I need to make casting from application of the activity to my custom application class
(need it to pass the repository to viewModelFactory and use it in the viewModel )
but in runTime I'm getting fatal exeption... what I do wrong?

This is my custom appliction class:

class UserApplication : Application() {

  
    private val database by lazy { UsersRoomDatabase.getDatabase(this@UserApplication) }

   
    val repository by lazy { Repository(database.userDou()) }
}

Try to run this:

val userApplication = requireActivity().application as UserApplication

The Exeption:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.mytaskgame, PID: 7703
    java.lang.ClassCastException: android.app.Application cannot be cast to com.mytaskgame.application.UserApplication
        at com.mytaskgame.view.logIn.fragment.SignInFragment.onCreate(SignInFragment.kt:35)
        at androidx.fragment.app.Fragment.performCreate(Fragment.java:3009)
        at androidx.fragment.app.FragmentStateManager.create(FragmentStateManager.java:474)
        at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:257)
        at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1862)
        at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1786)
        at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1723)
        at androidx.fragment.app.FragmentManager$4.run(FragmentManager.java:510)
        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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文