java - 您无法在使用 Glide 的回收 View 时启动已销毁 Activity 的加载

标签 java android android-recyclerview android-adapter android-glide

我正在使用适配器加载附加到 Activity A 的 fragment 内的图像和文本。但是当我关闭应用程序并打开应用程序时,应用程序崩溃并出现错误

You cannot start a load on a not yet attached View or a Fragment where getActivity() returns null (which usually occurs when getActivity() is called before the Fragment is attached or after the Fragment is destroyed)

但是当我第二次打开应用程序时,它运行顺利。

但是,如果我再次关闭应用程序并重新打开,则应用程序会崩溃并出现相同的错误,并且一旦发生错误,第二次它就会顺利运行。

这是我的适配器类。家是我的 fragment

public class TopAdapter extends RecyclerView.Adapter<TopAdapter.MyViewHolder> {
    ArrayList<location> bookslist;
    CardView cv;
    FirebaseAuth fauth;
    FirebaseDatabase database;
    DatabaseReference dbreference;
    location g;
    private Home context;





    public TopAdapter(Home context, ArrayList<location> bookslist){
        this.bookslist = bookslist;
        this.context = context; // add this as a field in your adapter class.
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_top,parent,false);
        return new MyViewHolder(v);

    }


    public class MyViewHolder extends RecyclerView.ViewHolder {

        TextView teacher_name,teacher_location;
        LinearLayout profile_details;
        ImageView iv;




        MyViewHolder(final View itemView) {
            super(itemView);
            cv = (CardView) itemView.findViewById(R.id.my_card_view);
            iv = (ImageView) itemView.findViewById(R.id.placePic);
            teacher_name= (TextView) itemView.findViewById(R.id.teacher_name);
            teacher_location = (TextView) itemView.findViewById(R.id.teacher_location);
            profile_details = (LinearLayout) itemView.findViewById(R.id.profile_details);

            fauth = FirebaseAuth.getInstance();



        }
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {

        database = FirebaseDatabase.getInstance();

        g = bookslist.get(position);

        holder.teacher_location.setText(g.getBlocas());
        holder.teacher_name.setText(g.getSellername());

        holder.profile_details.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                g = bookslist.get(position);
                Intent intent = new Intent(v.getContext(), gender_details.class);
                

                intent.putExtra(MOBILE, g.getSellermobile());
                intent.putExtra(EMAIL, g.getSelleremail());


                v.getContext().startActivity(intent);
            }
        });


      if (getContext() != null) {
           Glide.with(context).load(Uri.parse(g.getPics())).apply(RequestOptions.circleCropTransform().placeholder(R.drawable.bshelf).error(R.drawable.userphoto)).into(holder.iv);


    }



    @Override
    public int getItemCount() {
        return bookslist.size();
    }
}

最佳答案

使用 Activity 的Context 通过 Glide 加载图像。通过适配器的构造函数传递它,如下所示。

// Declare a Context variable in your adapter
private Context context;

public TopAdapter(ArrayList<location> bookslist, Context context){
    this.bookslist = bookslist;
    this.context = context; // add this as a field in your adapter class.
}

然后在 Activity 上下文中使用 glide。

Glide.with(context).load(Uri.parse(g.getPics())).apply(RequestOptions.circleCropTransform().placeholder(R.drawable.bshelf).error(R.drawable.userphoto)).into(holder.iv);

关于java - 您无法在使用 Glide 的回收 View 时启动已销毁 Activity 的加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57378077/

相关文章:

java - 如何在android项目中包含HTML5WebView

java - 如何在另一个 Activity 上调用 onclick?

android - RecyclerView 在回收时导致问题

android - ViewPager fragment 都引用相同的 RecyclerView 和/或适配器

android - USB_STATE 广播的附加功能

android - 在子 Recyclerview android 中禁用滚动

java - 多态性 - 简单

java - 从表达式树打印后序表达式时出现问题。 ( java )

java - JOptionPane 取消时抛出错误

android - 如何在不启用 ADB 集成的情况下进行调试