我试图从我的公司构建一个应用程序,但当我将构建变体更改为release(该变体仅更改项目配置文件,如端点和凭据)时,我得到此错误:
FATAL EXCEPTION: main
Process: com.timo.ecocloud.paciente, PID: 15435
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.timo.ecocloud.paciente/com.timo.ecocloudpaciente.ui.login.LoginActivity}: java.lang.ClassCastException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3635)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3792)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2210)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7839)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
Caused by: java.lang.ClassCastException
at android.support.v4.media.session.b.a(SourceFile:1)
at j1.a.<init>(SourceFile:1)
at com.timo.ecocloudpaciente.ui.login.f.a(SourceFile:1)
at androidx.lifecycle.s.b(SourceFile:1)
at androidx.lifecycle.s.a(SourceFile:1)
at com.timo.ecocloudpaciente.ui.login.LoginActivity.onCreate(Unknown Source:68)
at android.app.Activity.performCreate(Activity.java:8051)
at android.app.Activity.performCreate(Activity.java:8031)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1329)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3608)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3792)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2210)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7839)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
生成错误的代码是这样的:
loginViewModel = ViewModelProviders.of(this, new LoginViewModelFactory())
.get(LoginViewModel.class);
我试着更新所有内容,甚至使用更新的版本:
new ViewModelProvider(this, new LoginViewModelFactory()).get(LoginViewModel.class);
但是到目前为止还没有任何效果,有人知道如何解决这个问题吗?
编辑:viewmodel,viewmodelfactory代码如下:在活动中(生成错误的代码)
loginViewModel = ViewModelProviders.of(this, new LoginViewModelFactory())
.get(LoginViewModel.class);
网址:LoginViewModel.java
package com.timo.ecocloudpaciente.ui.login;
import android.app.Activity;
import android.util.Log;
import android.util.Patterns;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
import com.timo.ecocloudpaciente.data.LoginRepository;
import com.timo.ecocloudpaciente.R;
import com.timo.ecocloudpaciente.model.DefaultResponse;
import com.timo.ecocloudpaciente.model.DefaultResponseResult;
import com.timo.ecocloudpaciente.model.User;
public class LoginViewModel extends ViewModel {
private MutableLiveData<LoginFormState> loginFormState = new MutableLiveData<>();
private MutableLiveData<RecoverFormState> recoverFormState = new MutableLiveData<>();
private MutableLiveData<LoginResult> loginResult = new MutableLiveData<>();
private MutableLiveData<DefaultResponseResult> defaultResult = new MutableLiveData<>();
private LoginRepository loginRepository;
LoginViewModel(LoginRepository loginRepository) {
this.loginRepository = loginRepository;
}
/**
* Login
*/
LiveData<LoginFormState> getLoginFormState() {
return loginFormState;
}
LiveData<LoginResult> getLoginResult() {
return loginResult;
}
public void login(String username, String password) {
// can be launched in a separate asynchronous job
loginRepository.login(username, password, this);
}
public void loginDataChanged(String username, String password) {
if (!isUserNameValid(username)) {
loginFormState.setValue(new LoginFormState(R.string.login_invalid_username, null));
} else if (!isPasswordValid(password)) {
loginFormState.setValue(new LoginFormState(null, R.string.login_invalid_password));
} else {
loginFormState.setValue(new LoginFormState(true));
}
}
// A placeholder username validation check
private boolean isUserNameValid(String username) {
return username != null && !username.equals("");
}
// A placeholder password validation check
private boolean isPasswordValid(String password) {
return password != null && password.trim().length() == 6;
}
/**
* Used for callback
*/
public void onSuccess(User user) {
loginResult.setValue(new LoginResult(user));
}
public void onError(String message) {
loginResult.setValue(new LoginResult(message));
}
/**
* Recover password
*/
LiveData<RecoverFormState> getRecoverFormState() {
return recoverFormState;
}
public void recoverDataChanged(String useremail) {
if (!isEmailValid(useremail)) {
recoverFormState.setValue(new RecoverFormState(R.string.recover_invalid_email));
}else {
recoverFormState.setValue(new RecoverFormState(true));
}
}
LiveData<DefaultResponseResult> getDefaultResult() {
return defaultResult;
}
public void recoverPwd(Activity activity, LoginViewModel loginViewModel, String email){
loginRepository.recoverPwd(activity, loginViewModel, email);
}
private boolean isEmailValid(String email){
if (email == null || email.trim().isEmpty())
return false;
if (email.contains("@"))
return Patterns.EMAIL_ADDRESS.matcher(email).matches();
else
return false;
}
/**
* Used for callback
*/
public void defaultReturnOnSuccess(DefaultResponse defaultResponse) {
defaultResult.setValue(new DefaultResponseResult(defaultResponse, ""));
}
public void defaultReturnOnError(String message) {
defaultResult.setValue(new DefaultResponseResult(message));
}
}
LoginViewModelFactory.java
package com.timo.ecocloudpaciente.ui.login;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;
import androidx.annotation.NonNull;
import com.timo.ecocloudpaciente.data.LoginDataSource;
import com.timo.ecocloudpaciente.data.LoginRepository;
public class LoginViewModelFactory implements ViewModelProvider.Factory {
@NonNull
@Override
@SuppressWarnings("unchecked")
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
if (modelClass.isAssignableFrom(LoginViewModel.class)) {
return (T) new LoginViewModel(LoginRepository.getInstance(new LoginDataSource()));
} else {
throw new IllegalArgumentException("Unknown ViewModel class");
}
}
}
1条答案
按热度按时间oyt4ldly1#
如果您最近将AGP升级到8。+
只需将这一行添加到您的gradle.properties文件