Search code examples
androiddebuggingcrashandroid-glideillegalargumentexception

IllegalArgumentException when loading Image from URL with Glide?


I get a lot of crash reports in my Dev console containing this error message:

java.lang.IllegalArgumentException: 
  at com.bumptech.glide.manager.RequestManagerRetriever.assertNotDestroyed (RequestManagerRetriever.java:317)
  at com.bumptech.glide.manager.RequestManagerRetriever.get (RequestManagerRetriever.java:128)
  at com.bumptech.glide.manager.RequestManagerRetriever.get (RequestManagerRetriever.java:108)
  at com.bumptech.glide.Glide.with (Glide.java:776)
  at com.mypackagename.censored.GlobalBanner$7.run (GlobalBanner.java:241)
  at android.os.Handler.handleCallback (Handler.java:888)
  at android.os.Handler.dispatchMessage (Handler.java:100)
  at android.os.Looper.loop (Looper.java:213)
  at android.app.ActivityThread.main (ActivityThread.java:8178)
  at java.lang.reflect.Method.invoke (Native Method)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:513)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1101)

GlobalBanner.java:241 is:

            ImageView imageView = dialog.findViewById(R.id.dialog);

            RequestOptions options = new RequestOptions()
                    .placeholder(R.drawable.globaldialog_placeholder)
                    .error(R.drawable.globaldialog_failedtoload);

            Glide.with(mContext).load(imageURLGlobal).apply(options).into(imageView ); //This is Line 241

The Code works on all of my devices and Emulators and it loads a 540 x 722 JPG and displays it in the App as an Dialog. The Image is 182 KB big.

I really don't have more information, the error is occuring on many different Android Versions and Devices.

Can someone explain to me how I can fix this?

EDIT Here is the full function which gets called on App start in the onCreate of the MainActivity:

public static void setGlobalBanner(final Context mContext) {

    final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {


            if (isDislayingGlobal == 1) {


                final Dialog dialog = new Dialog(mContext);
                dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                dialog.setCancelable(true);
                dialog.setContentView(R.layout.dialog_global_banner);
                dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
                dialog.getWindow().setDimAmount(0.5f);
                WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
                lp.copyFrom(dialog.getWindow().getAttributes());
                lp.width = WindowManager.LayoutParams.MATCH_PARENT;
                lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
                dialog.getWindow().setAttributes(lp);


                ImageView dialogButton1 = dialog.findViewById(R.id.button_close);
                dialogButton1.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {

                        dialog.dismiss();

                    }
                });

                ImageView imageView = dialog.findViewById(R.id.dialogImage);

                RequestOptions options = new RequestOptions()
                        .placeholder(R.drawable.globaldialog_placeholder)
                        .error(R.drawable.globaldialog_failedtoload);

                Glide.with(mContext).load(imageURLGlobal).apply(options).into(imageView);



                imageView .setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {

                        mContext.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(playstorelinkGlobal)));
                        dialog.dismiss();
                    }
                });

                dialog.show();


            }

        }
    }, displayingSecondsGlobal);


}

displaysecondsGlobal is getting pulled from my Database and is the amount of seconds the Dialog should wait till it gets shown, mostly these are 120 Seconds. Is it possible that it crashes because the MainActivity is onPause? Because of another Activity or maybe the Interstitial or RewardedVideo ad?

It crashes only on a small percentage of user devices, and also it is not neccessary for the user to get the dialog displayed, its just a Dialog I use to advertise my own Apps. Is it possible to wrap a simple try and catch blocka round the Glide statement so it gets catched and doesn't crash?


Solution

  • you are creating new Handler in UI thread which waits for 120 secs and is never ever dismissed. your code fires after Activity is destroyed and is causing Toast or finish call on null Context (dead Activity)

    declare you Handler on top of class and always call handler.removeCallbacksAndMessages(null); when Context (or Thread) is destroyed, e.g. for Activity it would be onDestroy method, before super call