我正在使用mvvm遵循干净的体系结构。当我在片段中注入viewmodel工厂并尝试从片段viewmodel执行用例时,它会在用例上提供NPE。我也已经注入了用例。
请仔细检查代码并指出错误
MainModule.java
@Module
public class MainModule {
@Provides
LoginViewModel provideLoginViewModel() {
return new LoginViewModel();
}
@Provides
@Named("LoginActivity")
ViewModelProvider.Factory provideActivityViewModelFactory(LoginViewModel loginViewModel) {
return new ViewModelProviderFactory<>(loginViewModel);
}
@Provides
PasscodeViewModel providePasscodeViewModel(SetPasscode setPasscode) {
return new PasscodeViewModel(setPasscode);
}
@Provides
@Named("PasscodeFragment")
ViewModelProvider.Factory provideFragmentViewModelFactory(PasscodeViewModel passcodeViewModel) {
return new ViewModelProviderFactory<>(passcodeViewModel);
}
}
PasscodeFragment.java
public class PasscodeFragment extends BaseFragment<PasscodeViewModel> implements PasscodeNavigator, View.OnClickListener {
@Inject
@Named("PasscodeFragment")
ViewModelProvider.Factory viewModelFactory;
PasscodeViewModel passcodeViewModel;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
passcodeViewModel.setNavigator(this);
}
public PasscodeFragment() {
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
((MainApplication) getActivity().getApplicationContext()).getComponent().inject(this);
View view = inflater.inflate(R.layout.fragment_passcode, container, false);
initViews(view);
return view;
}
@Override
public PasscodeViewModel getViewModel() {
passcodeViewModel = ViewModelProviders.of(this, viewModelFactory).get(PasscodeViewModel.class);
return passcodeViewModel;
}
PasscodeViewModel.java(此处SETPASSCODE即将为空)
public class PasscodeViewModel extends BaseViewModel<PasscodeNavigator> {
//use case
private SetPasscode setPasscode;
//data
private String tempPin;
private boolean isConfirming = false;
public PasscodeViewModel() {
}
public PasscodeViewModel(SetPasscode setPasscode) {
this.setPasscode = setPasscode;
}
public void checkForConfirmation(String passcode) {
try {
if (passcode.length() == 4) {
if (isConfirming) {
//second attempt
if (tempPin.equals(passcode)) {
//done
Log.e("ganesh", passcode);
setPasscode(passcode);
} else {
//reset
isConfirming = false;
tempPin = "";
getNavigator().onPinNotMatched();
getNavigator().clearViewsForFirstAttempt();
}
} else {
//first attempt
tempPin = passcode;
isConfirming = true;
getNavigator().clearViewsForSecondAttempt();
}
}
} catch (NumberFormatException e) {
getNavigator().wrongPinFormat();
getNavigator().clearViewsForFirstAttempt();
}
}
private void setPasscode(String passcode) {
//setPasscode IS NULL HERE
setPasscode.execute(new DisposableSingleObserver<Boolean>() {
@Override
public void onSuccess(Boolean aBoolean) {
if (aBoolean)
getNavigator().onPinSetSuccess();
else
getNavigator().onPinSetFailed();
}
@Override
public void onError(Throwable e) {
getNavigator().onPinSetFailed();
}
}, SetPasscode.Params.setPasscode(passcode));
}
@Override
protected void onCleared() {
if (setPasscode != null)
setPasscode.dispose();
}
}
SetPasscode.java
public class SetPasscode extends SingleUseCase<Boolean, SetPasscode.Params> {
private UserRepository repository;
public SetPasscode(PostExecutionThread postExecutionThread, UserRepository repository) {
super(postExecutionThread);
this.repository = repository;
}
@Override
public Single<Boolean> buildUseCaseObservable(SetPasscode.Params params) {
return repository.setPasscode(params.passcode);
}
public static final class Params {
private String passcode;
private Params(String passcode) {
this.passcode = passcode;
}
public static SetPasscode.Params setPasscode(String passcode) {
return new SetPasscode.Params(passcode);
}
}
}
LoginActivity.java
public class LoginActivity extends BaseActivity<LoginViewModel> implements LoginNavigator {
@Inject
@Named("LoginActivity")
ViewModelProvider.Factory viewModelFactory;
LoginViewModel loginViewModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
getSupportActionBar().hide();
((MainApplication) getApplicationContext()).getComponent().inject(this);
loginViewModel = ViewModelProviders.of(this, viewModelFactory).get(LoginViewModel.class);
loginViewModel.setNavigator(this);
initViews();
}
@Override
public LoginViewModel getViewModel() {
return loginViewModel;
}