我正在尝试在RecyclerView中使用原生广告,但是App中出现一个错误,它崩溃了。请检查以下代码:
错误
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.ats.sarkarijobs, PID: 32337
java.lang.ClassCastException: com.google.android.gms.ads.formats.MediaView cannot be cast to com.google.android.gms.ads.formats.UnifiedNativeAdView
at com.ats.sarkarijobs.NativeAdView.<init>(NativeAdView.java:18)
at com.ats.sarkarijobs.RecyclerViewAdapter.onCreateViewHolder(RecyclerViewAdapter.java:56)
at androidx.recyclerview.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6794)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5975)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5858)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5854)
at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2230)
at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1557)
at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1517)
at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:612)
at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3924)
at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3641)
at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4194)
at android.view.View.layout(View.java:17702)
at android.view.ViewGroup.layout(ViewGroup.java:5631)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)
at android.view.View.layout(View.java:17702)
... NativeAdView持有人
public class NativeAdView extends RecyclerView.ViewHolder {
private UnifiedNativeAdView adView;
UnifiedNativeAdView getAdView() {
return adView;
}
public NativeAdView(View view){
super(view);
adView = view.findViewById(R.id.ad_media);
adView.setHeadlineView(adView.findViewById(R.id.ad_headline));
adView.setBodyView(adView.findViewById(R.id.ad_body));
adView.setCallToActionView(adView.findViewById((R.id.ad_call)));
adView.setIconView(adView.findViewById(R.id.ad_icon));
adView.setAdvertiserView(adView.findViewById(R.id.ad_advertiser));
adView.setStoreView(adView.findViewById(R.id.ad_store));
adView.setStarRatingView(adView.findViewById(R.id.ad_store));
}
}
onCreateViewHolder函数
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType){
case native_ad_view:
View unifiedNativeAd = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_native_ad_view, parent, false);
return new NativeAdView(unifiedNativeAd);
case item_view:
default:
View view = LayoutInflater.from( parent.getContext()).inflate(R.layout.postdetails, parent, false);
return new ImageTypeViewHolder(view) ;
}
}
我的RecyclerView中的本地广告,但App中出现一个错误,它崩溃了。请检查上面的代码: