gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 09:37:30 25 4
gpt4 key购买 nike

我正在使用适配器加载附加到 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com