任何人都可以在 Glide 上下文中解决这个错误[关闭]

Posted

技术标签:

【中文标题】任何人都可以在 Glide 上下文中解决这个错误[关闭]【英文标题】:can anyone sort this error out in Glide context [closed] 【发布时间】:2022-01-24 04:56:02 【问题描述】:

我在 Glide 中遇到错误,

这个错误是这样的

任何人都可以解决这个错误吗请我需要帮助 它的构建非常好,但我在运行时面临错误

这条线导致了主要问题

Glide.with(context).load(R.drawable.disc).into(holder.sound_dis);

E/androidRuntime: FATAL EXCEPTION: main
    Process: com.paradox.projectsp3, PID: 27908
    java.lang.NullPointerException: Argument must not be null
        at com.bumptech.glide.util.Preconditions.checkNotNull(Preconditions.java:29)
        at com.bumptech.glide.util.Preconditions.checkNotNull(Preconditions.java:23)
        at com.bumptech.glide.RequestBuilder.into(RequestBuilder.java:768)
        at com.paradox.projectsp3.Adapter.DemoAdapter.onBindViewHolder(DemoAdapter.java:44)
        at com.paradox.projectsp3.Adapter.DemoAdapter.onBindViewHolder(DemoAdapter.java:19)
        at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:7065)
        at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:7107)
        at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:6012)
        at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6279)
        at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6118)
        at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6114)
        at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2303)
        at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1627)
        at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1587)
        at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:665)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4134)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3851)
        at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4404)
        at android.view.View.layout(View.java:23304)
        at android.view.ViewGroup.layout(ViewGroup.java:6539)
        at androidx.constraintlayout.widget.ConstraintLayout.onLayout(ConstraintLayout.java:1873)
        at android.view.View.layout(View.java:23304)
        at android.view.ViewGroup.layout(ViewGroup.java:6539)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
        at android.view.View.layout(View.java:23304)
        at android.view.ViewGroup.layout(ViewGroup.java:6539)
        at androidx.appcompat.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:536)
        at android.view.View.layout(View.java:23304)
        at android.view.ViewGroup.layout(ViewGroup.java:6539)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
        at android.view.View.layout(View.java:23304)
        at android.view.ViewGroup.layout(ViewGroup.java:6539)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1841)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1582)
        at android.view.View.layout(View.java:23304)
        at android.view.ViewGroup.layout(ViewGroup.java:6539)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
        at com.android.internal.policy.DecorView.onLayout(DecorView.java:871)
        at android.view.View.layout(View.java:23304)
        at android.view.ViewGroup.layout(ViewGroup.java:6539)
        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:3979)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:3380)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:2345)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:9046)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1066)
        at android.view.Choreographer.doCallbacks(Choreographer.java:889)
        at android.view.Choreographer.doFrame(Choreographer.java:816)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1051)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:264)
        at android.app.ActivityThread.main(ActivityThread.java:8282)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:632)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1049)

这是我正在使用的适配器

package com.paradox.projectsp3.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


import com.bumptech.glide.Glide;
import com.paradox.projectsp3.Model.MediaObject;
import com.paradox.projectsp3.R;

import java.util.List;

public class DemoAdapter extends RecyclerView.Adapter<DemoAdapter.DemoViewHolder> 



    List<MediaObject> mediaObjectList;
    private ImageView imageView;
    public Context context;


    public DemoAdapter(List<MediaObject> mediaObjectList,Context context) 
        this.mediaObjectList = mediaObjectList;
        this.context = context;

    

    @NonNull
    @Override
    public DemoViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup , int viewType) 
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_main,viewGroup,false);
        return new DemoViewHolder(view);
    

    @Override
    public void onBindViewHolder( DemoViewHolder holder, int position) 
        MediaObject mediaObject = mediaObjectList.get(position);
        Glide.with(context).load(R.drawable.disc).into(holder.sound_dis);

        


    @Override
    public int getItemCount() 
        return mediaObjectList.size();
    

    public class DemoViewHolder extends RecyclerView.ViewHolder 

        private ImageView sound_dis;


        public DemoViewHolder(@NonNull View itemView) 
            super(itemView);

            ImageView sound_dis = (ImageView) itemView.findViewById(R.id.imageView3);
        
    

【问题讨论】:

【参考方案1】:

您的 sound_dis 为空,因为您不小心在 DemoViewHolder 构造函数中创建了一个局部变量并将 ImageView 引用存储在其中。类中声明的那个没有值(null)。

在您的 DemoViewholder 函数中删除 ImageView 数据类型,因为您已经在 DemoviewHolder 类中声明了它。

您的 DemoViewHolder 构造函数应该是这样的:

public DemoViewHolder(@NonNull View itemView) 
        super(itemView);
        sound_dis = (ImageView) itemView.findViewById(R.id.imageView3);
    

【讨论】:

【参考方案2】:

这不是 glide 的错误,实际上你错误地声明了 imageview。你的 DemoViewHolder 会是这样,然后就可以正常工作了。

public class DemoViewHolder extends RecyclerView.ViewHolder 

    private ImageView sound_dis;


    public DemoViewHolder(@NonNull View itemView) 
        super(itemView);
        sound_dis = itemView.findViewById(R.id.imageView3);


    

【讨论】:

@paradoxcodee 你有没有像这样添加你的图像视图? 是的,我成功了

以上是关于任何人都可以在 Glide 上下文中解决这个错误[关闭]的主要内容,如果未能解决你的问题,请参考以下文章

Glide如何优化内存消耗

存档在没有任何上下文的情况下停止,但在设备上构建时效果很好

Glide--------Golang依赖包解决工具之错误实践

我试图拟合和评分逻辑回归模型但出现错误,任何人都可以帮我解决这个错误

尝试在我的构建中添加新层,任何人都可以解决这个问题吗?

glide4.0以上在Android9.0以上加载图片不显示解决方案