膨胀类 com.facebook.ads.MediaView 时出错

Posted

技术标签:

【中文标题】膨胀类 com.facebook.ads.MediaView 时出错【英文标题】:Error inflating class com.facebook.ads.MediaView 【发布时间】:2017-10-20 06:14:07 【问题描述】:

我已经在我的 android 应用程序上集成 Facebook Audience Network 有一段时间了,到目前为止我没有收到任何问题。

最近我将 Facebook API 调用从 v2.5 更新到了 v3.0,并且我开始在我的 Crashlytic 上获取这些报告。它在更新之前有效,但在更新之后不再有效。我可以尝试恢复到 v2.5,看看情况是否有所改善,但我只是把它放在这里,希望其他遇到类似问题的人可以为此提供一些答案。

private void processLoadedAd(final NativeAd nativeAd) 
    nativeAd.setAdListener(new AdListener() 
        @Override
        public void onError(Ad ad, AdError adError) 

        

        @Override
        public void onAdLoaded(Ad ad) 
            Log.e(TAG, "onAdLoaded: " + ad);
            if (nativeAd != null) 
                nativeAd.unregisterView();
            

            arrayNativeAd.add(nativeAd);
            adapterBulletinsRecommended.notifyDataSetChanged();
            rlRecommended.setVisibility(View.VISIBLE);
        

        @Override
        public void onAdClicked(Ad ad) 

        

        @Override
        public void onLoggingImpression(Ad ad) 

        
    );
    nativeAd.loadAd();


 // Display our text and image at the specified position in the list
    @Override
    public void onBindViewHolder(final ViewHolder viewHolder, final int position) 
        final NativeAd nativeAd = nativeAds.get(position);
        viewHolder.itemView.setTag(nativeAd);

        viewHolder.mvAdPicture.setNativeAd(nativeAd);

        try 
            Glide.with(context)
                    .load(nativeAd.getAdIcon().getUrl())
                    .thumbnail(0.1f)
                    .dontAnimate()
                    .diskCacheStrategy(DiskCacheStrategy.RESULT)
                    .into(viewHolder.ivAdIcon);
         catch (NullPointerException e) 
            e.printStackTrace();
        

        try 
            Glide.with(context)
                    .load(nativeAd.getAdChoicesIcon().getUrl())
                    .thumbnail(0.1f)
                    .dontAnimate()
                    .diskCacheStrategy(DiskCacheStrategy.RESULT)
                    .into(viewHolder.ivAdChoicesIcon);
         catch (NullPointerException e) 
            e.printStackTrace();
        

        viewHolder.tvTitle.setText(nativeAd.getAdTitle());
        viewHolder.tvSocialContext.setText(nativeAd.getAdSocialContext());
        viewHolder.tvAdBody.setText(nativeAd.getAdBody());
        viewHolder.tvCTA.setText(nativeAd.getAdCallToAction());

        // Register the Title & CTA button to listen for clicks
        List<View> clickableViews = new ArrayList<>();
        clickableViews.add(viewHolder.tvTitle);
        clickableViews.add(viewHolder.tvCTA);
        nativeAd.registerViewForInteraction(viewHolder.cvContent, clickableViews);

        Log.i(TAG, "Title: " + nativeAd.getAdTitle());
        Log.i(TAG, "Description: " + nativeAd.getAdBody());
        Log.i(TAG, "Subtitle: " + nativeAd.getAdSubtitle());
        Log.i(TAG, "Image: " + nativeAd.getAdCoverImage().getUrl());
        Log.i(TAG, "Destination URL: " + nativeAd.getAdChoicesLinkUrl());
    

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_
    android:layout_>

    <android.support.v7.widget.CardView
        android:id="@+id/cv_content"
        android:layout_
        android:layout_
        android:layout_margin="5dp"
        android:elevation="20dp"
        app:cardCornerRadius="10dp">

        <RelativeLayout
            android:layout_
            android:layout_>

            <com.facebook.ads.MediaView
                android:id="@+id/mv_ad_picture"
                android:layout_
                android:layout_
                android:background="@color/weak_tint_profile" />

            <ImageView
                android:id="@+id/iv_adchoices_icon"
                android:layout_
                android:layout_
                android:layout_alignParentRight="true"
                android:layout_alignParentTop="true"
                android:layout_margin="10dp"
                android:background="@color/transparent" />

            <ImageView
                android:id="@+id/iv_ad_icon"
                android:layout_
                android:layout_
                android:layout_below="@id/mv_ad_picture"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="10dp"
                android:background="@color/transparent"
                android:scaleType="centerCrop" />

            <RelativeLayout
                android:layout_
                android:layout_
                android:layout_alignBottom="@id/iv_ad_icon"
                android:layout_alignTop="@id/iv_ad_icon"
                android:layout_toRightOf="@id/iv_ad_icon">

                <LinearLayout
                    android:id="@+id/ll_ad_header"
                    android:layout_
                    android:layout_
                    android:layout_toLeftOf="@+id/ll_cta"
                    android:gravity="center_vertical"
                    android:orientation="vertical">

                    <TextView
                        android:id="@+id/tv_ad_title"
                        android:layout_
                        android:layout_
                        android:ellipsize="end"
                        android:maxLines="2"
                        android:text="Ad Title"
                        android:textSize="14sp"
                        android:textStyle="bold" />

                    <TextView
                        android:id="@+id/tv_social_context"
                        android:layout_
                        android:layout_
                        android:ellipsize="end"
                        android:maxLines="1"
                        android:text="Social Context"
                        android:textColor="@color/secondary_text"
                        android:textSize="10sp" />
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/ll_cta"
                    android:layout_
                    android:layout_
                    android:layout_alignParentRight="true"
                    android:layout_centerVertical="true"
                    android:layout_marginBottom="10dp"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp"
                    android:background="@drawable/modern_btn_primary">

                    <TextView
                        android:id="@+id/tv_cta"
                        android:layout_
                        android:layout_
                        android:gravity="center"
                        android:paddingBottom="5dp"
                        android:paddingLeft="10dp"
                        android:paddingRight="10dp"
                        android:paddingTop="5dp"
                        android:text="CTA Button"
                        android:textColor="@color/white"
                        android:textSize="12sp"
                        android:textStyle="bold" />

                </LinearLayout>

            </RelativeLayout>

            <TextView
                android:id="@+id/tv_ad_body"
                android:layout_
                android:layout_
                android:layout_below="@+id/iv_ad_icon"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:ellipsize="end"
                android:gravity="center_vertical"
                android:maxLines="1"
                android:text="Ad Body"
                android:textColor="@color/text_on_alpha"
                android:textSize="12sp" />

        </RelativeLayout>

    </android.support.v7.widget.CardView>

</RelativeLayout>

Fatal Exception: android.view.InflateException: Binary XML file line #19: Binary XML file line #19: Error inflating class com.facebook.ads.MediaView

Caused by android.view.InflateException: Binary XML file line #19: Error inflating class com.facebook.ads.MediaView

Caused by java.lang.reflect.InvocationTargetException
       at java.lang.reflect.Constructor.newInstance0(Constructor.java)
       at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
       at android.view.LayoutInflater.createView(LayoutInflater.java:652)
       at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:794)
       at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:734)
       at android.view.LayoutInflater.rInflate(LayoutInflater.java:865)
       at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828)
       at android.view.LayoutInflater.rInflate(LayoutInflater.java:873)
       at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828)
       at android.view.LayoutInflater.rInflate(LayoutInflater.java:873)
       at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828)
       at android.view.LayoutInflater.inflate(LayoutInflater.java:525)
       at android.view.LayoutInflater.inflate(LayoutInflater.java:427)
       at worka.biz.worka.adapter.BulletinsRecommendedAdapter.onCreateViewHolder(Unknown Source)
       at worka.biz.worka.adapter.BulletinsRecommendedAdapter.onCreateViewHolder(Unknown Source)
       at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(Unknown Source)
       at android.support.v7.widget.RecyclerView$Recycler.clear(Unknown Source)
       at android.support.v7.widget.RecyclerView$Recycler.clear(Unknown Source)
       at android.support.v7.widget.RecyclerView$Recycler.getScrapList(Unknown Source)
       at android.support.v7.widget.LinearLayoutManager$LayoutState.hasMore(Unknown Source)
       at android.support.v7.widget.LinearLayoutManager.generateDefaultLayoutParams(Unknown Source)
       at android.support.v7.widget.LinearLayoutManager.generateDefaultLayoutParams(Unknown Source)
       at android.support.v7.widget.LinearLayoutManager.onSaveInstanceState(Unknown Source)
       at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(Unknown Source)
       at android.support.v7.widget.RecyclerView.onMeasure(Unknown Source)
       at android.view.View.measure(View.java:21121)
       at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
       at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
       at android.view.View.measure(View.java:21121)
       at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
       at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
       at android.view.View.measure(View.java:21121)
       at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:2249)
       at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
       at android.widget.ScrollView.onMeasure(ScrollView.java:543)
       at android.view.View.measure(View.java:21121)
       at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
       at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
       at android.view.View.measure(View.java:21121)
       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6462)
       at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
       at android.view.View.measure(View.java:21121)
       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6462)
       at android.support.design.widget.CoordinatorLayout.releaseTempRect(Unknown Source)
       at android.support.design.widget.HeaderScrollingViewBehavior.onMeasureChild(Unknown Source)
       at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.layoutDependsOn(Unknown Source)
       at android.support.design.widget.CoordinatorLayout.onMeasure(Unknown Source)
       at android.view.View.measure(View.java:21121)
       at android.support.v4.widget.DrawerLayout.onMeasure(Unknown Source)
       at android.view.View.measure(View.java:21121)
       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6462)
       at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
       at android.support.v7.widget.ContentFrameLayout.onMeasure(Unknown Source)
       at android.view.View.measure(View.java:21121)
       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6462)
       at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
       at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
       at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
       at android.view.View.measure(View.java:21121)
       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6462)
       at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
       at android.view.View.measure(View.java:21121)
       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6462)
       at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
       at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
       at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
       at android.view.View.measure(View.java:21121)
       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6462)
       at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
       at com.android.internal.policy.DecorView.onMeasure(DecorView.java:872)
       at android.view.View.measure(View.java:21121)
       at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2625)
       at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1677)
       at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1928)
       at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1550)
       at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7190)
       at android.view.Choreographer$CallbackRecord.run(Choreographer.java:959)
       at android.view.Choreographer.doCallbacks(Choreographer.java:734)
       at android.view.Choreographer.doFrame(Choreographer.java:670)
       at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:945)
       at android.os.Handler.handleCallback(Handler.java:751)
       at android.os.Handler.dispatchMessage(Handler.java:95)
       at android.os.Looper.loop(Looper.java:154)
       at android.app.ActivityThread.main(ActivityThread.java:6776)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)
Caused by java.lang.UnsupportedOperationException: TextureView doesn't support displaying a background drawable
       at android.view.TextureView.setBackgroundDrawable(TextureView.java:315)
       at android.view.View.setBackground(View.java:19498)
       at android.view.View.(View.java)
       at android.view.View.(View.java)
       at android.view.View.(View.java)
       at android.view.TextureView.(TextureView.java)
       at com.facebook.ads.internal.view.d.c.b.(Unknown Source)
       at com.facebook.ads.internal.view.o.(Unknown Source)
       at com.facebook.ads.internal.view.j.(Unknown Source)
       at com.facebook.ads.MediaViewVideoRenderer.(Unknown Source)
       at com.facebook.ads.internal.view.h.(Unknown Source)
       at com.facebook.ads.MediaView.(Unknown Source)
       at java.lang.reflect.Constructor.newInstance0(Constructor.java)
       at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
       at android.view.LayoutInflater.createView(LayoutInflater.java:652)
       at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:794)
       at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:734)
       at android.view.LayoutInflater.rInflate(LayoutInflater.java:865)
       at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828)
       at android.view.LayoutInflater.rInflate(LayoutInflater.java:873)
       at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828)
       at android.view.LayoutInflater.rInflate(LayoutInflater.java:873)
       at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828)
       at android.view.LayoutInflater.inflate(LayoutInflater.java:525)
       at android.view.LayoutInflater.inflate(LayoutInflater.java:427)
       at worka.biz.worka.adapter.BulletinsRecommendedAdapter.onCreateViewHolder(Unknown Source)
       at worka.biz.worka.adapter.BulletinsRecommendedAdapter.onCreateViewHolder(Unknown Source)
       at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(Unknown Source)
       at android.support.v7.widget.RecyclerView$Recycler.clear(Unknown Source)
       at android.support.v7.widget.RecyclerView$Recycler.clear(Unknown Source)
       at android.support.v7.widget.RecyclerView$Recycler.getScrapList(Unknown Source)
       at android.support.v7.widget.LinearLayoutManager$LayoutState.hasMore(Unknown Source)
       at android.support.v7.widget.LinearLayoutManager.generateDefaultLayoutParams(Unknown Source)
       at android.support.v7.widget.LinearLayoutManager.generateDefaultLayoutParams(Unknown Source)
       at android.support.v7.widget.LinearLayoutManager.onSaveInstanceState(Unknown Source)
       at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(Unknown Source)
       at android.support.v7.widget.RecyclerView.onMeasure(Unknown Source)
       at android.view.View.measure(View.java:21121)
       at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
       at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
       at android.view.View.measure(View.java:21121)
       at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
       at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
       at android.view.View.measure(View.java:21121)
       at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:2249)
       at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
       at android.widget.ScrollView.onMeasure(ScrollView.java:543)
       at android.view.View.measure(View.java:21121)
       at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
       at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
       at android.view.View.measure(View.java:21121)
       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6462)
       at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
       at android.view.View.measure(View.java:21121)
       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6462)
       at android.support.design.widget.CoordinatorLayout.releaseTempRect(Unknown Source)
       at android.support.design.widget.HeaderScrollingViewBehavior.onMeasureChild(Unknown Source)
       at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.layoutDependsOn(Unknown Source)
       at android.support.design.widget.CoordinatorLayout.onMeasure(Unknown Source)
       at android.view.View.measure(View.java:21121)
       at android.support.v4.widget.DrawerLayout.onMeasure(Unknown Source)
       at android.view.View.measure(View.java:21121)
       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6462)
       at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
       at android.support.v7.widget.ContentFrameLayout.onMeasure(Unknown Source)
       at android.view.View.measure(View.java:21121)
       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6462)
       at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
       at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
       at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
       at android.view.View.measure(View.java:21121)
       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6462)
       at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
       at android.view.View.measure(View.java:21121)
       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6462)
       at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
       at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
       at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
       at android.view.View.measure(View.java:21121)
       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6462)
       at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
       at com.android.internal.policy.DecorView.onMeasure(DecorView.java:872)
       at android.view.View.measure(View.java:21121)
       at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2625)
       at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1677)
       at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1928)
       at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1550)
       at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7190)
       at android.view.Choreographer$CallbackRecord.run(Choreographer.java:959)
       at android.view.Choreographer.doCallbacks(Choreographer.java:734)
       at android.view.Choreographer.doFrame(Choreographer.java:670)
       at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:945)
       at android.os.Handler.handleCallback(Handler.java:751)
       at android.os.Handler.dispatchMessage(Handler.java:95)
       at android.os.Looper.loop(Looper.java:154)
       at android.app.ActivityThread.main(ActivityThread.java:6776)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)

【问题讨论】:

您是否也更改了您的targetSdkVersion?因为TextureView doesn't support displaying a background drawable错误是你问题的根源出现在Android N(api 24)上 @pleft 出现错误后,我已将 targetSdkVersion 从 25 更新为 26... 以及 gradle 文件中受影响的其他行,以反映 Google 上可用的最新版本。但是是的,目前唯一受此影响的设备是 API 24。 嗨,我在小米设备上遇到了类似的问题。你有解决这个问题的办法吗? @Sarun1001 不幸的是,我现在只捕获异常只是为了让较新的设备无崩溃。 @Reynolds R. G. G 现在我在降级到 Facebook SDK v.4.23.0 版本时没有问题,也许你应该看看 【参考方案1】:

要解决这个问题,你必须添加

compile 'com.android.support:support-v4:*.*.*'
compile 'com.android.support:recyclerview-v7:*.*.*'

与sdk版本无关。

【讨论】:

我两个都有。仍然不适用于 sdk 4.23 以上。

以上是关于膨胀类 com.facebook.ads.MediaView 时出错的主要内容,如果未能解决你的问题,请参考以下文章

膨胀类 CollapsingToolbarLayout 时出错

膨胀类片段时出错

使用上下文类膨胀布局

如何为android修复“错误膨胀类GeckoEngineView”

在 RecyclerView 上膨胀类 ImageView 时出错

膨胀类 com.google.android.material.slider.Slider 时出错