2017-10-13 103 views
0

我正在嘗試添加一個ImageView庫,它顯示點擊特色RecyclerView(Github的回收站視圖庫)圖像。圖像顯示沒有任何問題,但是當我試圖單擊它給下面的錯誤。我也提到了一些在計算器問題類似的問題在圖像上但沒能解決我的問題:如何解決Android中的java.lang.ClassCastException?

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) 

我綁定觀點持有者代碼:

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); 
    } 
} 

}

回答

0

您的問題是

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

因此,您可以將其更改爲Context

試試這個。

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 
} 

編輯

context.getApplicationContext(); 
+0

爵士第一我曾嘗試過,然後通過引用一些stackoverflow問題將上下文更改爲當前表單 –

+0

並且您可以嘗試@creonilso rodrigues的答案。 – KeLiuyue

+0

我也試過,但得到相同的錯誤 –

1

您可以獲取上下文中的所有視圖,因此你不需要傳似參數的情況下,例如:

Context contex = holder.ivBackground.getContext(); 
+0

獲取相同的錯誤Sir –

+0

看這個例子https://stackoverflow.com/a/40481103/5749462 –

相關問題