This crash only occurs when you compile with YYC, not with VM. I suggest trying to compile your app with VM instead.
Answer from user17153451 on Stack OverflowThis crash only occurs when you compile with YYC, not with VM. I suggest trying to compile your app with VM instead.
This crash randomly happens when you testing with a Huawei device, it can be solved by invalidating caches and restarting AS.
FatalException (null pointer dereference) while driving.
null pointer dereference in Android
Null pointer dereference in Android
Null pointer dereference on android native methods - Frida 14.2.3
You totally messed up the concept of Object Oriented Programming.
You defined PatientAlarm class to incapsulate all logic of working with MediaPlayer. It's OK.
But why did you make all methods static? Why new PatientAlarm() called inside these methods and created instance even not assigned to some variable or field that makes it target for garbage collection?
This is example of how you should do it:
public class PatientAlarm {
private MediaPlayer md;
public PatientAlarm(Context context) {
md = MediaPlayer.create(context, R.raw.patient_alarm);
md.setLooping(true);
md.prepare();
}
public void start() {
if (!md.isPlaying()) {
md.start();
}
}
public void stop() {
if (md.isPlaying()) {
md.stop();
}
}
}
Adapt it to your case and read more about using MediaPlayer class.
which md.start(); line? there are 2... anyway your code is totally vulnerable for concurrency issues, you may read about locks to prevent it, but I would start form refactoring, before you get into advanced stuff. Creating a new object just to initialize a static member of the class is probably the most ugly approach for singleton I have ever seen.. If you really need to, use static method to do it for you, at least there wont be a new object for GC to collect. But in fact the use of static object in your example seems to be a miss first of all. Try rewriting it without static fields or methods and most likely your code will be simpler and more reliable.