E/AndroidRuntime: FATAL EXCEPTION: the main
Process: com. Example. Homework, PID: 2895
Java. Lang. RuntimeException: Unable to start the activity ComponentInfo {com. Example. Homework/com. Example. Homework. MainActivity} : Java. Lang. RuntimeException: always create an instance of a class. Com example. Homework. The ViewModel. UserViewModel
. An android app. ActivityThread. PerformLaunchActivity (ActivityThread. Java: 2327)
. An android app. ActivityThread. HandleLaunchActivity (ActivityThread. Java: 2392)
. An android app. ActivityThread. Access the $800 (153) ActivityThread. Java:
An android. App. ActivityThread $H.h andleMessage (ActivityThread. Java: 1305)
An android. OS. Handler. DispatchMessage (Handler. Java: 102)
. An android OS. Stars. Loop (135). Which Java:
. An android app. ActivityThread. Main (5305) ActivityThread. Java:
The at Java. Lang. Reflect. Method. Invoke (Native Method)
The at Java. Lang. Reflect. Method. Invoke (372) Method. The Java:
At com. Android. Internal. OS. $MethodAndArgsCaller ZygoteInit. Run (ZygoteInit. Java: 903)
At com. Android. Internal. OS. ZygoteInit. Main (ZygoteInit. Java: 698)
Under Caused by: Java. Lang. RuntimeException: always create an instance of the class. Com example. Homework. The ViewModel. UserViewModel
The at androidx. Lifecycle. ViewModelProvider $AndroidViewModelFactory. Create (ViewModelProvider. Java: 269)
The at androidx. Lifecycle. SavedStateViewModelFactory. Create (SavedStateViewModelFactory. Java: 106)
The at androidx. Lifecycle. ViewModelProvider. Get (ViewModelProvider. Java: 185)
The at androidx. Lifecycle. ViewModelProvider. Get (ViewModelProvider. Java: 150)
At com. Example. Homework. MainActivity. OnCreate (MainActivity. Java: 36)
. An android app. Activity. PerformCreate (5990) Activity. Java:
. An android app. Instrumentation. CallActivityOnCreate (1106) Instrumentation. Java:
. An android app. ActivityThread. PerformLaunchActivity (ActivityThread. Java: 2280)
. An android app. ActivityThread. HandleLaunchActivity (ActivityThread. Java: 2392)
. An android app. ActivityThread. Access the $800 (153) ActivityThread. Java:
An android. App. ActivityThread $H.h andleMessage (ActivityThread. Java: 1305)
An android. OS. Handler. DispatchMessage (Handler. Java: 102)
. An android OS. Stars. Loop (135). Which Java:
. An android app. ActivityThread. Main (5305) ActivityThread. Java:
The at Java. Lang. Reflect. Method. Invoke (Native Method)
The at Java. Lang. Reflect. Method. Invoke (372) Method. The Java:
At com. Android. Internal. OS. $MethodAndArgsCaller ZygoteInit. Run (ZygoteInit. Java: 903)
At com. Android. Internal. OS. ZygoteInit. Main (ZygoteInit. Java: 698)
Under Caused by: Java. Lang. NoSuchMethodException: & lt; init> [class android. App. Application]
The at Java. Lang. Class. GetConstructor (Class. Java: 531)
The at Java. Lang. Class. GetConstructor (Class. Java: 495)
The at androidx. Lifecycle. ViewModelProvider $AndroidViewModelFactory. Create (ViewModelProvider. Java: 267)
The at androidx. Lifecycle. SavedStateViewModelFactory. Create (SavedStateViewModelFactory. Java: 106)
The at androidx. Lifecycle. ViewModelProvider. Get (ViewModelProvider. Java: 185)
The at androidx. Lifecycle. ViewModelProvider. Get (ViewModelProvider. Java: 150)
At com. Example. Homework. MainActivity. OnCreate (MainActivity. Java: 36)
. An android app. Activity. PerformCreate (5990) Activity. Java:
. An android app. Instrumentation. CallActivityOnCreate (1106) Instrumentation. Java:
. An android app. ActivityThread. PerformLaunchActivity (ActivityThread. Java: 2280)
. An android app. ActivityThread. HandleLaunchActivity (ActivityThread. Java: 2392)
. An android app. ActivityThread. Access the $800 (153) ActivityThread. Java:
An android. App. ActivityThread $H.h andleMessage (ActivityThread. Java: 1305)
An android. OS. Handler. DispatchMessage (Handler. Java: 102)
. An android OS. Stars. Loop (135). Which Java:
. An android app. ActivityThread. Main (5305) ActivityThread. Java:
The at Java. Lang. Reflect. Method. Invoke (Native Method)
The at Java. Lang. Reflect. Method. Invoke (372) Method. The Java:
At com. Android. Internal. OS. $MethodAndArgsCaller ZygoteInit. Run (ZygoteInit. Java: 903)
At com. Android. Internal. OS. ZygoteInit. Main (ZygoteInit. Java: 698)
Bosses think that may be where the problem (crying)
CodePudding user response:
Under Caused by: Java. Lang. RuntimeException: always create an instance of a class of com. Example. Homework. The ViewModel. UserViewModel, it will tell you why, can't create UserViewModel hereCodePudding user response:
Ah, but not to build in the activity, is also prompt the error in fragments, where it should be created?CodePudding user response:
Constructor can have only one parameter is the application, you enter the viewmodel to see, is your constructor is wrong