Srinivas Nahak
Srinivas Nahak

Reputation: 37

How to solve java.lang.ClassCastException in Android?

I'm trying to add an ImageView library which shows the image on click to the Featured RecyclerView(A recycler view library of github). The image is shown without any problem but when I'm trying to click on the image it's giving following error .I have also referred some of the similar problems in stackoverflow questions but that couldn't solve my problem :

java.lang.ClassCastException: android.support.v7.widget.TintContextWrapper cannot be cast to android.app.Activity
    at com.mostafaaryan.transitionalimageview.TransitionalImageView$1.onClick(TransitionalImageView.java:82)
    at android.view.View.performClick(View.java:4438)
    at android.view.View$PerformClick.run(View.java:18422)
    at android.os.Handler.handleCallback(Handler.java:733)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5001)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
    at dalvik.system.NativeStart.main(Native Method)

My bind view holder code is:

public class CustomRecyclerViewAdapter extends FeatureRecyclerViewAdapter<CustomRecyclerViewAdapter.CustomRecyclerViewHolder> {

private List<String> dataList;
private Context context;
private int[] images = new int[5];

public CustomRecyclerViewAdapter(Context context, List<String> list) {
    this.dataList = list;
    this.context = context;

    images[0] = R.drawable.image_one;
    images[1] = R.drawable.image_three;
    images[2] = R.drawable.image_two;
    images[3] = R.drawable.image_four;
    images[4] = R.drawable.image_five;

}

@Override
public CustomRecyclerViewHolder onCreateFeaturedViewHolder(ViewGroup parent, int viewType) {
    return new CustomRecyclerViewHolder(
            LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.timeline_holder, parent, false));
}

@Override
public void onBindFeaturedViewHolder(CustomRecyclerViewHolder holder, int position) {
   /* Glide.with(context)
            .load(images[position % 4]).into(holder.ivBackground);*/
    TransitionalImage transitionalImage = new TransitionalImage.Builder()
            .duration(500)
            .backgroundColor(ContextCompat.getColor(holder.ivBackground.getContext(), R.color.cardview_light_background))
            .image(images[position % 4])
            .create();
    holder.ivBackground.setTransitionalImage(transitionalImage);

   // holder.tvHeading.setText(dataList.get(position));
}

@Override
public int getFeaturedItemsCount() {
    return dataList.size();
}

@Override
public void onSmallItemResize(CustomRecyclerViewHolder holder, int position, float offset) {
   // holder.tvHeading.setAlpha(offset / 100f);
}

@Override
public void onBigItemResize(CustomRecyclerViewHolder holder, int position, float offset) {
   // holder.tvHeading.setAlpha(offset / 100f);
}

public static class CustomRecyclerViewHolder extends RecyclerView.ViewHolder {

    TransitionalImageView ivBackground;
    TextView tvHeading;

    public CustomRecyclerViewHolder(View itemView) {
        super(itemView);

        ivBackground = (TransitionalImageView) itemView.findViewById(R.id.iv_background);
       // tvHeading = (TextView) itemView.findViewById(R.id.tv_heading);
    }
}

}

Upvotes: 1

Views: 2699

Answers (2)

KeLiuyue
KeLiuyue

Reputation: 8237

You problem is

java.lang.ClassCastException: android.support.v7.widget.TintContextWrapper cannot be cast to android.app.Activity`

So you can change it to Context .

Try this .

private Context context;

public YouAdapter(Context context){
    this.context = context;
}
@Override
public void onBindViewHolder(CustomRecyclerViewHolder holder, int position) {
/* Glide.with(context)
        .load(images[position % 4]).into(holder.ivBackground);*/
    TransitionalImage transitionalImage = new TransitionalImage.Builder()
            .duration(500)
            .backgroundColor(ContextCompat.getColor(context, R.color.cardview_light_background))
            .image(images[position % 4])
            .create();
    holder.ivBackground.setTransitionalImage(transitionalImage);
    //ivBackground is the imageView
}

Edit

context.getApplicationContext();

Upvotes: 0

creonilso rodrigues
creonilso rodrigues

Reputation: 479

You can get the context in all the view, so you do not need to pass the context like parameter, for example:

Context contex = holder.ivBackground.getContext();

Upvotes: 2

Related Questions