Glide 在 android "java.lang.NullPointerException: Argument must not be null" 中加载图像时出现错误

Glide giving error while loading image in android "java.lang.NullPointerException: Argument must not be null"

2021-08-30 22:31:54.727 15117-15117/com.eosphorus.flockflairapp E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.eosphorus.flockflairapp, PID: 15117
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.eosphorus.flockflairapp/com.eosphorus.flockflairapp.DisplayQuestions}: java.lang.NullPointerException: Argument must not be null
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2946)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3081)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1831)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:201)
        at android.app.ActivityThread.main(ActivityThread.java:6806)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
     Caused by: java.lang.NullPointerException: Argument must not be null
        at com.bumptech.glide.util.Preconditions.checkNotNull(Preconditions.java:31)
        at com.bumptech.glide.util.Preconditions.checkNotNull(Preconditions.java:25)
        at com.bumptech.glide.RequestBuilder.into(RequestBuilder.java:651)
        at com.eosphorus.flockflairapp.DisplayQuestions.onCreate(DisplayQuestions.java:116)
        at android.app.Activity.performCreate(Activity.java:7224)
        at android.app.Activity.performCreate(Activity.java:7213)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1272)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2926)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3081) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1831) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:201) 
        at android.app.ActivityThread.main(ActivityThread.java:6806) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873) 
<ImageView
            android:id = "@+id/imageView"
            android:layout_width = "wrap_content"
            android:layout_height = "wrap_content" />
        ImageView imageView = findViewById(R.id.imageView);

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

        Glide.with(this).load("https://www.tutorialspoint.com/images/tp-logo-diamond.png").apply(options).into(imageView);

添加 .apply(new RequestOptions() 以继续使用最新版本。 只需替换以下代码:

Glide
 .with(this)
 .load(R.drawable.image_default_profile_picture)
 .apply(new RequestOptions()
 .placeholder(R.mipmap.ic_launcher_background)
 .fitCenter())
 .into(imageView);