2017-09-23 7 views
0

私は、Googleのサンプルリサイクラービューからネイティブエクスペリエンス広告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; 

そして、私のMenuItemクラスは次のようになります。

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

問題を解決したことを知りました! –

関連する問題