I want to separate my business logic from the ui and I actually wanted to implement MVVM architecture for that. I have this class that I perform all my business logic but the issue is that the app crashes when it tries to show the AlertDialog that I used in the class because I was using application.getApplicationContext. I cannot be able to access getActivity in a normal class. I really need a way to resolve that because I don't want to move the dialog out of that class. This is the error message that I am getting.
FATAL EXCEPTION: main
Process: com.elijahukeme.assessmentapp, PID: 11350
java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:926)
at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:889)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:772)
at androidx.appcompat.app.AppCompatDialog.setContentView(AppCompatDialog.java:95)
at androidx.appcompat.app.AlertController.installContent(AlertController.java:232)
at androidx.appcompat.app.AlertDialog.onCreate(AlertDialog.java:279)
at android.app.Dialog.dispatchOnCreate(Dialog.java:407)
at android.app.Dialog.show(Dialog.java:302)
at androidx.appcompat.app.AlertDialog$Builder.show(AlertDialog.java:1009)
at com.elijahukeme.assessmentapp.repositories.AuthenticationRepository.showMessage(AuthenticationRepository.java:205)
at com.elijahukeme.assessmentapp.repositories.AuthenticationRepository$1.onDataChange(AuthenticationRepository.java:68)
at com.google.firebase.database.Query$1.onDataChange(Query.java:191)
at com.google.firebase.database.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:75)
my repository class
public class AuthenticationRepository implements ShowErrorMessage {
private Application application;
private MutableLiveData<FirebaseDatabase> mutableLiveData;
private FirebaseDatabase firebaseDatabase;
private StorageReference profileImageStorageRef;
private String myUri ;
public MutableLiveData<FirebaseDatabase> getMutableLiveData() {
return mutableLiveData;
}
public FirebaseDatabase getFirebaseDatabase() {
return firebaseDatabase;
}
public StorageReference getProfileImageStorageRef() {
return profileImageStorageRef;
}
private StudentModel studentModel;
private RegistrationNumberModel registrationNumberModel;
public AuthenticationRepository(Application application){
this.application = application;
mutableLiveData = new MutableLiveData<>();
firebaseDatabase = FirebaseDatabase.getInstance();
profileImageStorageRef = FirebaseStorage.getInstance().getReference("Profile Images");
myUri = "";
}
public void registerRegistrationNumber(String regNumber, ProgressBar progressBar){
progressBar.setVisibility(View.VISIBLE);
firebaseDatabase.getReference().child("Registration Number")
.child(regNumber).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if (snapshot.exists()){
progressBar.setVisibility(View.GONE);
showMessage("Error","You have already added this Registration Number");
}else {
HashMap<String,Object> hashMap = new HashMap<>();
hashMap.put("regNumber",regNumber);
firebaseDatabase.getReference().child("Registration Number")
.child(regNumber).updateChildren(hashMap)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()){
progressBar.setVisibility(View.GONE);
Toast.makeText(application, "Registration Number Added Successfully",Toast.LENGTH_SHORT).show();
}else{
progressBar.setVisibility(View.GONE);
Toast.makeText(application, "Error Occurred", Toast.LENGTH_SHORT).show();
}
}
});
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
progressBar.setVisibility(View.GONE);
Toast.makeText(application, error.toString(), Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void showMessage(String title, String message) {
AlertDialog.Builder builder = new AlertDialog.Builder(application.getApplicationContext());
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(message);
builder.show();
}
}
public interface ShowErrorMessage {
void showMessage(String title, String message);
}