3

My App gets a lot of crash reports

The App crashes when the onClickListener gets called. Only Android Version 7.0 and 7.1 are affected. I already tried to launch the App on a Emulator with Android Version 7 but on the Emulator everything works fine.

This is the crash report:

java.lang.OutOfMemoryError:
  at dalvik.system.VMRuntime.newNonMovableArray (Native Method)
  at android.graphics.BitmapFactory.nativeDecodeAsset (Native Method)
  at android.graphics.BitmapFactory.decodeStream (BitmapFactory.java:655)
  at android.graphics.BitmapFactory.decodeResourceStream             (BitmapFactory.java:483)
  at android.graphics.drawable.Drawable.createFromResourceStream (Drawable.java:1157)
  at android.content.res.ResourcesImpl.loadDrawableForCookie (ResourcesImpl.java:720)
  at android.content.res.ResourcesImpl.loadDrawable (ResourcesImpl.java:571)
  at android.content.res.Resources.getDrawable (Resources.java:889)
  at android.content.Context.getDrawable (Context.java:530)
  at android.support.v4.content.ContextCompat.getDrawable (ContextCompat.java:351)
  at android.support.v7.widget.AppCompatDrawableManager.getDrawable (AppCompatDrawableManager.java:200)
  at android.support.v7.widget.AppCompatDrawableManager.getDrawable (AppCompatDrawableManager.java:188)
  at android.support.v7.content.res.AppCompatResources.getDrawable (AppCompatResources.java:100)
  at android.support.v7.widget.AppCompatImageHelper.setImageResource (AppCompatImageHelper.java:85)
  at android.support.v7.widget.AppCompatImageView.setImageResource (AppCompatImageView.java:94)
  at com.future.king.johncena.tabs.Tab1$1.onClick (Tab1.java:94)
  at android.view.View.performClick (View.java:5675)
  at android.view.View$PerformClick.run (View.java:22641)
  at android.os.Handler.handleCallback (Handler.java:836)
  at android.os.Handler.dispatchMessage (Handler.java:103)
  at android.os.Looper.loop (Looper.java:203)
  at android.app.ActivityThread.main (ActivityThread.java:6251)
  at java.lang.reflect.Method.invoke (Native Method)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:1063)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:924)    

and this is my onClickListener:

public void button() {
  button.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View view) {
      cleanUpMediaPlayer();
      mp = MediaPlayer.create(getActivity(), R.raw.sound);

      mp.start();
      if(!sound1){
        mp.setVolume(0.0f, 0.0f);
      }

      button.setImageResource(R.drawable.button1);
      mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mediaPlayer) {
          button.setImageResource(R.drawable.button);
        }
      });
    }
  });
}

Line 94 is

button.setImageResource(R.drawable.button1);

I hope someone can tell me what's causing this crash.

Norbert Bicsi
  • 1,562
  • 2
  • 19
  • 33
Fisch Gesicht
  • 81
  • 1
  • 9

1 Answers1

0

I would highly recommend using selector state drawables for the button and change the button state rather than setting image resources in onClick method.

See State List documentation for reference.

Kuba Pawłowski
  • 365
  • 2
  • 10
  • Okay thank you, do you have any idea how I can test it? Instead of updating the app and waiting for user crash reports? – Fisch Gesicht Feb 07 '19 at 13:23
  • 1
    I have a theory that this OOM is a result of a button spam click, since you're not preventing it in the code. Try to spam it repeatedly, this might be the cause. Either way, set button `setClickable(false);` when user first clicks it and make it clickable again after the sound has been played. – Kuba Pawłowski Feb 07 '19 at 14:12
  • Yes that may cause the crash. Is is possible that I put everything in the onClick Method in a Try and Catch? So the app dont crash? – Fisch Gesicht Feb 07 '19 at 14:55
  • `try { } catch { }` is not a solution to the problem. Overrall, catching OOMException is not a good idea ([Refer this question for further explanations](https://stackoverflow.com/questions/2679330/catching-java-lang-outofmemoryerror/2680784)) – Kuba Pawłowski Feb 07 '19 at 15:29
  • The thing is that the user should be allowed to spam the button. – Fisch Gesicht Feb 07 '19 at 15:44
  • Onclick execution should atleast be blocked before last invoke is finished. Furthermore, try using some library like `Glide` to set image resources. – Kuba Pawłowski Feb 07 '19 at 17:13
  • How do I block it? – Fisch Gesicht Feb 07 '19 at 18:24
  • Invoke `button.setClickable(false)` In your listener, then, when Media player oncompletion is invoked, make it clickable again – Kuba Pawłowski Feb 07 '19 at 21:48