当我尝试在recyclerview适配器中使用progressdialog时,我有异常消息:
Unable to add window -- token null is not for an application
我知道使用上下文的所有内容都可以,因为Glide在ProgressBar下面的几行使用相同的上下文。有什么想法是什么问题? 我的适配器类:
import android.app.ProgressDialog;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import java.util.List;
public class ShopsAdapter extends RecyclerView.Adapter<ShopsAdapter.MyViewHolder> {
Context mContext;
List<String> shopsList;
ProgressDialog progressDialog;
public class MyViewHolder extends RecyclerView.ViewHolder {
public ImageView thumbnail;
public MyViewHolder(View view) {
super(view);
thumbnail = (ImageView) view.findViewById(R.id.thumbnail);
}
}
public ShopsAdapter(Context mContext, List<String> shopsList) {
this.mContext = mContext;
this.shopsList = shopsList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.shop_card,parent,false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
progressDialog = new ProgressDialog(mContext);
shopsList.get(position);
try{
progressDialog.show();
String stringformat = String.format(".../%s.png", shopsList.get(position));
Glide.with(mContext).load(stringformat).listener(new RequestListener<String, GlideDrawable>() {
@Override
public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
return false;
}
@Override
public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
//progressDialog.setVisibility(View.GONE);
return false;
}
}).centerCrop().fitCenter().into(holder.thumbnail);
}catch (Exception e){
Log.d("Dsdfs", e.getMessage());
Glide.with(mContext).load("http://...Zabka.png").centerCrop().fitCenter().into(holder.thumbnail);
}
}
@Override
public int getItemCount() {
return shopsList.size();
}
}
答案 0 :(得分:2)
更改
adapter = new ShopsAdapter(getApplicationContext(),shopsNames);
要
adapter = new ShopsAdapter(this,shopsNames);