Home > OS >  Cannot create an instance of class extends from ViewModel
Cannot create an instance of class extends from ViewModel

Time:09-21

i got this error and tried many different ways (from stackoverflow, github,etc) and most of it didn't work anymore, i hope to get help from the community. I use navigation component for project, i tried switching to the old way using fragmentmanger and all get the same results. Project using multi fragment and single activity

class LoginViewModel extends AndroidViewModel {
    public LoginViewModel(@NonNull Application application) {
        super(application);
    }
}

public class ScreenLogin extends Fragment {
    FragmentLoginBinding viewLoginBinding;
    private LoginViewModel mainViewModel;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        viewLoginBinding = FragmentLoginBinding.inflate(getLayoutInflater());
        mainViewModel = new ViewModelProvider(requireActivity()).get(LoginViewModel.class);
        viewLoginBinding.btnLogin.setOnClickListener(v -> {
            mainViewModel.login(viewLoginBinding.username.getText().toString(), viewLoginBinding.password.getText().toString());
        });
        return viewLoginBinding.getRoot();
    }
}

Error as under

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dxlampro.appchat/com.dxlampro.appchat.Main}: java.lang.RuntimeException: Cannot create an instance of class JavaCode.Screen.Login.LoginViewModel
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
     Caused by: java.lang.RuntimeException: Cannot create an instance of class JavaCode.Screen.Login.LoginViewModel
        at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.kt:316)
        at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.kt:304)
        at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.kt:278)
        at androidx.lifecycle.SavedStateViewModelFactory.create(SavedStateViewModelFactory.kt:128)
        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.kt:187)
        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.kt:153)
        at JavaCode.Screen.Login.ScreenLogin.onCreateView(ScreenLogin.java:34)
        at androidx.fragment.app.Fragment.performCreateView(Fragment.java:3104)
        at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:524)
        at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:261)
        at androidx.fragment.app.FragmentStore.moveToExpectedState(FragmentStore.java:113)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1424)
        at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:2968)
        at androidx.fragment.app.FragmentManager.dispatchViewCreated(FragmentManager.java:2879)
        at androidx.fragment.app.Fragment.performViewCreated(Fragment.java:3129)
        at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:552)
        at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:261)
        at androidx.fragment.app.FragmentStore.moveToExpectedState(FragmentStore.java:113)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1424)
        at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:2968)
        at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:2886)
        at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:263)
        at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:351)
        at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:246)
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1334)
        at android.app.Activity.performStart(Activity.java:7029)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2741)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
        at android.app.ActivityThread.-wrap11(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:164) 
        at android.app.ActivityThread.main(ActivityThread.java:6494) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 
     Caused by: java.lang.IllegalAccessException: java.lang.Class<JavaCode.Screen.Login.LoginViewModel> is not accessible from java.lang.Class<androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory>
        at java.lang.reflect.Constructor.newInstance0(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
        at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.kt:312)
        at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.kt:304) 
        at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.kt:278) 
        at androidx.lifecycle.SavedStateViewModelFactory.create(SavedStateViewModelFactory.kt:128) 
        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.kt:187) 
        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.kt:153) 
        at JavaCode.Screen.Login.ScreenLogin.onCreateView(ScreenLogin.java:34) 
        at androidx.fragment.app.Fragment.performCreateView(Fragment.java:3104) 
        at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:524) 
        at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:261) 
        at androidx.fragment.app.FragmentStore.moveToExpectedState(FragmentStore.java:113) 
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1424) 
        at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:2968) 
        at androidx.fragment.app.FragmentManager.dispatchViewCreated(FragmentManager.java:2879) 
        at androidx.fragment.app.Fragment.performViewCreated(Fragment.java:3129) 
        at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:552) 
        at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:261) 
        at androidx.fragment.app.FragmentStore.moveToExpectedState(FragmentStore.java:113) 
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1424) 
        at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:2968) 
        at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:2886) 
        at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:263) 
        at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:351) 
        at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:246) 
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1334) 
        at android.app.Activity.performStart(Activity.java:7029) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2741) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
        at android.app.ActivityThread.-wrap11(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:164) 
        at android.app.ActivityThread.main(ActivityThread.java:6494) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 

CodePudding user response:

You should pass a ViewModelProvider.AndroidViewModelFactory to ViewModelProvider.

see documentation here: https://developer.android.com/reference/android/arch/lifecycle/ViewModelProvider

CodePudding user response:

I created class Fractory

public class LoginModelFactory implements ViewModelProvider.Factory {

    @NonNull
    @Override
    public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
        if (modelClass.isAssignableFrom(LoginViewModel.class)) {
            return (T) new LoginViewModel();
        }
        throw new IllegalArgumentException("Unknown ViewModel class");
    }
}

and then I create ViewModel in Fragment as under

mainViewModel = new ViewModelProvider(requireActivity(), new LoginModelFactory()).get(LoginViewModel.class);

it worked

  • Related