简体   繁体   中英

how to have a splash screen in mvvm pattern with room in android.?

I'm trying to have a splash screen with my MVVM patterned login activity. my splash screen does work perfectly but when it tries to finish and start LoginActivity, the app crashes. I'm getting this error as follows.

please help me to get this solved. I need to have a slapsh screen in my project with MVVM pattern and room db included for LoginActivity. An explanation or a code sample is more than welcome.

My Splash Activity

public class SplashActivity extends AppCompatActivity {

public void onAttachedToWindow() {
    super.onAttachedToWindow();
    Window window = getWindow();
    window.setFormat(PixelFormat.RGBA_8888);
}

/**
 * Called when the activity is first created.
 */
Thread splashTread;

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

    StartAnimations();
}

private void StartAnimations() {
    Animation anim = AnimationUtils.loadAnimation(this, R.anim.alpha);
    anim.reset();
    LinearLayout l = findViewById(R.id.splash_layout);
    l.clearAnimation();
    l.startAnimation(anim);

    anim = AnimationUtils.loadAnimation(this, R.anim.translate);
    anim.reset();
    ImageView iv = findViewById(R.id.splash);
    iv.clearAnimation();
    iv.startAnimation(anim);

    splashTread = new Thread() {
        @Override
        public void run() {
            try {
                int waited = 0;
                // Splash screen pause time
                while (waited < 3500) {
                    sleep(100);
                    waited += 100;
                }
                Intent intent = new Intent(SplashActivity.this,
                        LoginActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                startActivity(intent);
                SplashActivity.this.finish();
            } catch (InterruptedException e) {
                // do nothing
            } finally {
                SplashActivity.this.finish();
            }

        }
    };
    splashTread.start();
}
}

My LoginActivity

public class LoginActivity extends AppCompatActivity {

private LoginViewModel loginViewModel;
private View mProgressView;
private View mLoginFormView;

private ActivityLoginBinding binding;

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

    mLoginFormView = findViewById(R.id.login_form);
    mProgressView = findViewById(R.id.login_progress);

    ActivityLoginBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_login);
    //get the view model
    loginViewModel = ViewModelProviders.of(this).get(LoginViewModel.class);
    binding.setUser(loginViewModel);

    // The observer updates the UI when Login Operation is successful
    loginViewModel.getUser().observe(this, userResponse -> {
        if (userResponse != null) {
            Toast.makeText(LoginActivity.this, "Login success", Toast.LENGTH_SHORT).show();

            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
            finish();

        } else {
            Log.d("LoginActivity", "value user is null");
            // Show ERROR
        }
    });

    binding.loginButton.setOnClickListener (
            (
                    View view) ->

            {
                loginViewModel.onBtnLoginClick();

            });
}

/**
 * Shows the progress UI and hides the login form.
 */
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
private void showProgress(final boolean show) {
    // On Honeycomb MR2 we have the ViewPropertyAnimator APIs, which allow
    // for very easy animations. If available, use these APIs to fade-in
    // the progress spinner.
    int shortAnimTime = getResources().getInteger(android.R.integer.config_shortAnimTime);

    mLoginFormView.setVisibility(show ? View.GONE : View.VISIBLE);
    mLoginFormView.animate().setDuration(shortAnimTime).alpha(
            show ? 0 : 1).setListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(Animator animation) {
            mLoginFormView.setVisibility(show ? View.GONE : View.VISIBLE);
        }
    });

    mProgressView.setVisibility(show ? View.VISIBLE : View.GONE);
    mProgressView.animate().setDuration(shortAnimTime).alpha(
            show ? 1 : 0).setListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(Animator animation) {
            mProgressView.setVisibility(show ? View.VISIBLE : View.GONE);
        }
    });
}

}

My LoginViewModel

public class LoginViewModel extends ViewModel {

private boolean errorMsg = true;
private Validations validations;

// Create a LiveData
private LiveData<User> userResponse;

public static final ObservableField<String> username = new ObservableField<>();
public static final ObservableField<String> password = new ObservableField<>();

public static final ObservableField<String> errorUsername = new ObservableField<>();
public static final ObservableField<String> errorPassword = new ObservableField<>();

private UserRepository userRepository;

public LoginViewModel(Context mContext) {

    userRepository = new UserRepository(mContext);
    username.set("ccuser");
    password.set("67890");

    userResponse = userRepository.getUser();

    validations=new Validations();

}

public LiveData<User> getUser() {
    return userResponse;
}

public void onBtnLoginClick() {

//        attemptLogin();
    if (validateInputs()) {
        userRepository.loginUser(username.get(), password.get());
    }
}
@BindingAdapter("error")
public static void setError(EditText editText, Object strOrResId) {
    if (strOrResId instanceof Integer) {
        editText.setError(
                editText.getContext().getString((Integer) strOrResId));
    } else {
        editText.setError((String) strOrResId);
    }
}

@BindingAdapter("onFocus")
public static void bindFocusChange(EditText editText, View.OnFocusChangeListener onFocusChangeListener) {
    if (editText.getOnFocusChangeListener() == null) {
        editText.setOnFocusChangeListener(onFocusChangeListener);
    }
}

//--
public boolean validateInputs() {
    boolean isValid = true;

    if (username.get() == null || !Validations.isUserNameValid(username.get())) {
        errorUsername.set(String.valueOf(R.string.error_invalid_username));
        isValid = false;
    } else {
        errorUsername.set(null);
    }
    if (password.get() == null || password.get().length() < 8 && !Validations.isPasswordValid(password.get())) {
        errorPassword.set(String.valueOf(R.string.error_incorrect_password));
        isValid = false;
    } else {
        errorPassword.set(null);
    }
    return isValid;
}
}

The Error i get

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.comcredit.ccui.cclogin, PID: 19969 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.comcredit.ccui.cclogin/com.comcredit.ccui.cclogin.view.LoginActivity}: java.lang.RuntimeException: Cannot create an instance of class com.comcredit.ccui.cclogin.view_model.LoginViewModel at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2858) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2933) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1612) at android.os.Handler.dispatchMessage(Handler.java:105) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6710) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:770) Caused by: java.lang.R untimeException: Cannot create an instance of class com.comcredit.ccui.cclogin.view_model.LoginViewModel at android.arch.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.java:153) at android.arch.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:210) at android.arch.lifecycle.ViewModelProvider.get(ViewModelProvider.java:134) at android.arch.lifecycle.ViewModelProvider.get(ViewModelProvider.java:102) at com.comcredit.ccui.cclogin.view.LoginActivity.onCreate(LoginActivity.java:45) at android.app.Activity.performCreate(Activity.java:6980) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2811) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2933) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1612) at android.os.Handler.dispatchMessage( Handler.java:105) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6710) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:770) Caused by: java.lang.InstantiationException: java.lang.Class has no zero argument constructor at java.lang.Class.newInstance(Native Method) at android.arch.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.java:151) at android.arch.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:210) at android.arch.lifecycle.ViewModelProvider.get(ViewModelProvider.java:134) at android.arch.lifecycle.ViewModelProvider.get(ViewModelProvider.java:102) at com.comcredit.ccui.cclogin.view.LoginActivity.onCreate(LoginActivity.java:45) at android.app.Activity.performCreate(Activity.java:6980) at android.app.Instrumentation.callActivityOnC reate(Instrumentation.java:1214) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2811) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2933) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1612) at android.os.Handler.dispatchMessage(Handler.java:105) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6710) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:770)

this in your Intent declaration is referring to your view model. Try passing complete activity name instead.

Intent intent = new Intent(LoginActivity.this, MainActivity.class);

Use this code :

new Handler().postDelayed(new Runnable()
    {
        @Override
        public void run()
        {
            Intent intent = new Intent(SplashActivity.this,
                    LoginActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
            startActivity(intent);
            SplashActivity.this.finish();
        }
    },3500);

replace your code :

splashTread = new Thread() {
    @Override
    public void run() {
        try {
            int waited = 0;
            // Splash screen pause time
            while (waited < 3500) {
                sleep(100);
                waited += 100;
            }
            Intent intent = new Intent(SplashActivity.this,
                    LoginActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
            startActivity(intent);
            SplashActivity.this.finish();
        } catch (InterruptedException e) {
            // do nothing
        } finally {
            SplashActivity.this.finish();
        }

    }
};
splashTread.start();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM