当我尝试从活动应用程序转换到自定义应用程序类时出现致命异常
我需要从活动应用程序到我的自定义应用程序类进行转换 (需要它将存储库传递给 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论