简体   繁体   English

java.lang.RuntimeException:无法创建 ViewModel class 的实例

[英]java.lang.RuntimeException: Cannot create an instance of ViewModel class

I am trying to instantiate UserViewModel in my activity however it keeps giving me a java.lang.RuntimeException: Cannot create an instance of viewmodel class kindly assist.我正在尝试在我的活动中实例化UserViewModel ,但是它一直给我一个java.lang.RuntimeException: Cannot create an instance of viewmodel class kindly assist.

This is how my ViewModel looks like这就是我的 ViewModel 的样子

public class UserViewModel extends AndroidViewModel {

    private NodeAuthService api;
    private SharedPreferences pref;
    private static MutableLiveData<List<User>> userDetails = new MutableLiveData<>();

    public UserViewModel(@NonNull Application application) {
        super(application);
        api = AuthRetrofitClient.getInstance().create(NodeAuthService.class);
    }


    private String email = pref.getString("email", "");

    public void loadUser(){
        Call<List<User>> call;
        call = api.getUser(email);
        call.enqueue(new Callback<List<User>>() {
            @Override
            public void onResponse(Call<List<User>> call, Response<List<User>> response) {
                userDetails.postValue(response.body());

            }

            @Override
            public void onFailure(Call<List<User>> call, Throwable t) {
                Log.d("USER",t.getMessage());
            }
        });

    }

   public MutableLiveData<List<User>>getUserDetails(){
        return userDetails;
   }
}

This is how my activity is setup这就是我的活动设置方式

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.navigation_drawer);
    String nameVm;

    userViewModel = ViewModelProviders.of(this).get(UserViewModel.class);

    userViewModel.loadUser();

    userViewModel.getUserDetails().observe(this, new Observer<List<User>>() {
        @Override
        public void onChanged(List<User> users) {
            if (users != null){
                for (int i = 0; i<users.size(); i++){
                    nameVm = String.valueOf(users.get(0));
                }
            }
        }
    });

}

Create ViewModelFactory class创建 ViewModelFactory class

public class MyViewModelFactory implements ViewModelProvider.Factory {
    private Application mApplication;
    public MyViewModelFactory(Application application) {
        mApplication = application;           
    }

    @Override
    public <T extends ViewModel> T create(Class<T> modelClass) {
        // Replace UserViewModel →  with whatever or however you create your ViewModel
        return (T) new UserViewModel(mApplication);
    }
}

and init ViewModel like并像初始化ViewModel

UserViewModel myViewModel = ViewModelProviders.of(this, new MyViewModelFactory(this.getApplication())).get(UserViewModel.class);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.RuntimeException:无法创建 class ViewModel [Kotlin] 的实例 - java.lang.RuntimeException: Cannot create an instance of class ViewModel [Kotlin] java.lang.RuntimeException:无法创建 class com.example.cookpadapp.viewmodel.CookpadViewModel+ 的实例 - java.lang.RuntimeException: Cannot create an instance of class com.example.cookpadapp.viewmodel.CookpadViewModel+ at java.lang.RuntimeException:当android重新创建Activity和Fragment时,无法创建类ViewModel的实例 - java.lang.RuntimeException: Cannot create an instance of class ViewModel, when android recreates Activity and Fragment java.lang.RuntimeException:无法创建 yodgorbek.komilov.musobaqayangiliklari.viewmodel.MainViewModel 类的实例? - java.lang.RuntimeException: Cannot create an instance of class yodgorbek.komilov.musobaqayangiliklari.viewmodel.MainViewModel? Jetpack Compose + Hilt:java.lang.RuntimeException:无法创建 class ViewModel 的实例 - Jetpack Compose + Hilt: java.lang.RuntimeException: Cannot create an instance of class ViewModel java.lang.RuntimeException:无法在单元测试中创建 class ViewModel 的实例 - java.lang.RuntimeException: Cannot create an instance of class ViewModel in unit test java.lang.RuntimeException:无法创建类MovieViewModel的实例&存储库尚未初始化 - java.lang.RuntimeException: Cannot create an instance of class MovieViewModel& repository has not been initialized java.lang.RuntimeException:无法创建类 com.example.homeactivity.activities.editprofile.EditProfileViewModel 的实例 - java.lang.RuntimeException: Cannot create an instance of class com.example.homeactivity.activities.editprofile.EditProfileViewModel 刀柄匕首错误 java.lang.RuntimeException:无法创建 class HomeFragmentViewModel 的实例 - Hilt Dagger Error java.lang.RuntimeException: Cannot create an instance of class HomeFragmentViewModel java.lang.RuntimeException:无法序列化 - java.lang.RuntimeException: Cannot serialize
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM