2017-09-23 98 views
0

我有一个应用程序,它使用回收视图,代码从谷歌的示例回收视图与本机快速广告github集成。当我启动应用程序时,广告加载在顶部,但是当我开始滚动时,我的适配器中出现异常。MenuItem不能转换为com.android.gms.ads.NativeExpressAdView

java.lang.ClassCastException:dstudios.project.Utilities.MenuItem 不能转换到com.google.android.gms.ads.NativeExpressAdView

的代码在我的适配器如下所示:

case NATIVE_EXPRESS_AD_VIEW_TYPE: 

     default: 
      NativeExpressAdViewHolder nativeExpressHolder = 
        (NativeExpressAdViewHolder) holder; 
      NativeExpressAdView adView = (NativeExpressAdView) mData.get(position); 
      ViewGroup adCardView = (ViewGroup) nativeExpressHolder.itemView; 
      // The NativeExpressAdViewHolder recycled by the RecyclerView may be a different 
      // instance than the one used previously for this position. Clear the 
      // NativeExpressAdViewHolder of any subviews in case it has a different 
      // AdView associated with it, and make sure the AdView for this position doesn't 
      // already have a parent of a different recycled NativeExpressAdViewHolder. 
      if (adCardView.getChildCount() > 0) { 
       adCardView.removeAllViews(); 
      } 
      if (adView.getParent() != null) { 
       ((ViewGroup) adView.getParent()).removeView(adView); 
      } 

      // Add the Native Express ad to the native express ad view. 
      adCardView.addView(adView); 

唯一的例外是在这条线发生的事情:

   NativeExpressAdView adView = (NativeExpressAdView) mData.get(position); 

即使用在顶部宣布在适配器列表:

private final List<Object> mData; 

我的菜单项类看起来是这样的:

public class MenuItem { 
    public String Name; 
    public String UCPC; 
    public String QR; 
    public String URL; 
    public String Image; 
    public String Company; 
    public String Reviews; 
    public String CreatedAt; 
    public String CountryOrigin; 
    public String UpdatedAt; 
    public String Lineage; 
    public String Genetics; 
    public String Countries; 


    public MenuItem(String name, String UCPC, String Genetics, String URL, 
        String image, String updatedAt, String QR) { 
     this.Name = name; 
     this.UCPC = UCPC; 
     this.Genetics = Genetics; 
     this.URL = URL; 
     this.Image = image; 
     this.UpdatedAt = updatedAt; 
     this.QR = QR; 
    } 

    public String getName() { 
     return Name; 
    } 

    public String getUCPC() { 
     return UCPC; 
    } 

    public String getGenetics() { 
     return Genetics; 
    } 

    public String getURL() { 
     return URL; 
    } 

    public String getImage() { 
     return Image; 
    } 
    public String updatedAt() { 
     return UpdatedAt; 
    } 
    public String QR() { 
     return QR; 
    } 
} 

我不知道为什么它抛出异常和任何帮助将不胜感激!

回答

0

ClassCastExceptions当您尝试将对象转换为不是实例的类时会发生。

在你的情况下,mData.get(position)返回一个MenuItem类型的对象。而你试图把它转换为NativeExpressAdView - 因此,出现以下错误: -

java.lang.ClassCastException:dstudios.project.Utilities.MenuItem 不能转换为com.google.android。 gms.ads.NativeExpressAdView

所以,我不知道你是如何构建你的数据,但所有你需要做的,解决这个问题是要确保mData.get(position)返回NativeExpressAdView类型的对象。

+0

谢谢。我将广告设置添加到了异步任务,并加载了数据,然后添加了广告。 – doamn123

+0

很高兴知道你解决了这个问题! –

相关问题