Android - 膨胀类 com.facebook.drawee.view.SimpleDraweeView 的异常错误

Posted

技术标签:

【中文标题】Android - 膨胀类 com.facebook.drawee.view.SimpleDraweeView 的异常错误【英文标题】:Android - Exception Error inflating class com.facebook.drawee.view.SimpleDraweeView 【发布时间】:2016-12-08 08:17:11 【问题描述】:

我在 Play 商店中发布了我的应用,在我的测试中没有错误,但我从 Firebase 收到的报告显示我的适配器存在一些问题、跟踪错误和代码:

Exception android.view.InflateException: Binary XML file line #22: Binary XML file line #22: Error inflating class com.facebook.drawee.view.SimpleDraweeView
android.view.LayoutInflater.inflate (LayoutInflater.java:539)
android.view.LayoutInflater.inflate (LayoutInflater.java:423)
br.com.topimagens.topimagens.adapters.ImageAdapter.onCreateViewHolder (ImageAdapter.java:85)
br.com.topimagens.topimagens.adapters.ImageAdapter.onCreateViewHolder (ImageAdapter.java:51)
android.support.v7.widget.RecyclerView$Adapter.createViewHolder (RecyclerView.java:5779)
android.support.v7.widget.RecyclerView$Recycler.getViewForPosition (RecyclerView.java:5003)
android.support.v7.widget.RecyclerView$Recycler.getViewForPosition (RecyclerView.java:4913)
android.support.v7.widget.LinearLayoutManager$LayoutState.next (LinearLayoutManager.java:2029)
android.support.v7.widget.LinearLayoutManager.layoutChunk (LinearLayoutManager.java:1414)
android.support.v7.widget.LinearLayoutManager.fill (LinearLayoutManager.java:1377)
android.support.v7.widget.LinearLayoutManager.onLayoutChildren (LinearLayoutManager.java:578)
android.support.v7.widget.RecyclerView.dispatchLayoutStep2 (RecyclerView.java:3260)
android.support.v7.widget.RecyclerView.dispatchLayout (RecyclerView.java:3069)
android.support.v7.widget.RecyclerView.onLayout (RecyclerView.java:3518)
android.view.View.layout (View.java:16646)
android.view.ViewGroup.layout (ViewGroup.java:5440)
android.support.design.widget.HeaderScrollingViewBehavior.layoutChild (HeaderScrollingViewBehavior.java:131)
android.support.design.widget.ViewOffsetBehavior.onLayoutChild (ViewOffsetBehavior.java:42)
android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild (AppBarLayout.java:1319)
android.support.design.widget.CoordinatorLayout.onLayout (CoordinatorLayout.java:817)
android.view.View.layout (View.java:16646)
android.view.ViewGroup.layout (ViewGroup.java:5440)
android.widget.RelativeLayout.onLayout (RelativeLayout.java:1079)
android.view.View.layout (View.java:16646)
android.view.ViewGroup.layout (ViewGroup.java:5440)
android.widget.FrameLayout.layoutChildren (FrameLayout.java:336)
android.widget.FrameLayout.onLayout (FrameLayout.java:273)
android.view.View.layout (View.java:16646)
android.view.ViewGroup.layout (ViewGroup.java:5440)
android.widget.LinearLayout.setChildFrame (LinearLayout.java:1743)
android.widget.LinearLayout.layoutVertical (LinearLayout.java:1586)
android.widget.LinearLayout.onLayout (LinearLayout.java:1495)
android.view.View.layout (View.java:16646)
android.view.ViewGroup.layout (ViewGroup.java:5440)
android.widget.FrameLayout.layoutChildren (FrameLayout.java:336)
android.widget.FrameLayout.onLayout (FrameLayout.java:273)
android.view.View.layout (View.java:16646)
android.view.ViewGroup.layout (ViewGroup.java:5440)
android.widget.LinearLayout.setChildFrame (LinearLayout.java:1743)
android.widget.LinearLayout.layoutVertical (LinearLayout.java:1586)
android.widget.LinearLayout.onLayout (LinearLayout.java:1495)
android.view.View.layout (View.java:16646)
android.view.ViewGroup.layout (ViewGroup.java:5440)
android.widget.FrameLayout.layoutChildren (FrameLayout.java:336)
android.widget.FrameLayout.onLayout (FrameLayout.java:273)
com.android.internal.policy.PhoneWindow$DecorView.onLayout (PhoneWindow.java:2678)
android.view.View.layout (View.java:16646)
android.view.ViewGroup.layout (ViewGroup.java:5440)
android.view.ViewRootImpl.performLayout (ViewRootImpl.java:2183)
android.view.ViewRootImpl.performTraversals (ViewRootImpl.java:1943)
android.view.ViewRootImpl.doTraversal (ViewRootImpl.java:1119)
android.view.ViewRootImpl$TraversalRunnable.run (ViewRootImpl.java:6060)
android.view.Choreographer$CallbackRecord.run (Choreographer.java:858)
android.view.Choreographer.doCallbacks (Choreographer.java:670)
android.view.Choreographer.doFrame (Choreographer.java:606)
android.view.Choreographer$FrameDisplayEventReceiver.run (Choreographer.java:844)
android.os.Handler.handleCallback (Handler.java:746)
android.os.Handler.dispatchMessage (Handler.java:95)
android.os.Looper.loop (Looper.java:148)
android.app.ActivityThread.main (ActivityThread.java:5443)

也是标记同一行的部分: 由 android.view.InflateException 引起:二进制 XML 文件第 22 行:膨胀类 com.facebook.drawee.view.SimpleDraweeView 时出错 由 java.lang.reflect.InvocationTargetException 引起: 由 java.lang.NullPointerException 引起:SimpleDraweeView 未初始化!

公共类 ImageAdapter 扩展 RecyclerView.Adapter 私有上下文 mContext; 私有列表 mList; 公共 SharedPreference sharedPreference ; 私人 LayoutInflater mLayoutInflater; 私人浮标; 私有 int 宽度、高度、roundPixels; 受保护的静态最终字符串标签=“日志”; 带有动画的私有布尔值; 带有ImagedLayout的私有布尔值;

public ImageAdapter(Context c, List<Image> l)
    this(c, l, true, true);

public ImageAdapter(Context c, List<Image> l, boolean wa, boolean wcl)
    mContext = c;
    mList = l;
    mLayoutInflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    sharedPreference = new SharedPreference();

    withAnimation = wa;
    withImagedLayout = wcl;

    scale = mContext.getResources().getDisplayMetrics().density;
    width = mContext.getResources().getDisplayMetrics().widthPixels - (int)(14 * scale + 0.5f);

    roundPixels = (int)(2 * scale + 0.5f);



@Override
public MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) 
    View v;
    MyViewHolder mvh;
        v = mLayoutInflater.inflate(R.layout.item_image_card, viewGroup, false);
        mvh = new MyViewHolder(v);
        v.setTag(viewGroup);


    return mvh;


@Override
public void onBindViewHolder(final MyViewHolder myViewHolder, int position) 

    myViewHolder.tvTitulo.setText(mList.get(position).getTitulo());

    Image image = getItem(position);
    myViewHolder.tvTitulo.setText(image.getTitulo());
    myViewHolder.tvTitulo.setText(image.getTitulo());

    if (checkFavoriteItem(image)) 
        myViewHolder.ivMenuFavorite.setImageResource(R.drawable.ico_heart_on);
        myViewHolder.ivMenuFavorite.setTag("red");
       // Log.e("Result: ", "red" );
     else 
        myViewHolder.ivMenuFavorite.setImageResource(R.drawable.ico_heart_off);
        myViewHolder.ivMenuFavorite.setTag("grey");
        //Log.e("Result: ", "grey" );
    

    ControllerListener listener = new BaseControllerListener()
        @Override
        public void onFinalImageSet(String id, Object imageInfo, Animatable animatable) 
            super.onFinalImageSet(id, imageInfo, animatable);
            updateViewSize((ImageInfo) imageInfo);
        

        @Override
        public void onFailure(String id, Throwable throwable) 
            super.onFailure(id, throwable);
        

        @Override
        public void onIntermediateImageFailed(String id, Throwable throwable) 
            super.onIntermediateImageFailed(id, throwable);
        

        @Override
        public void onIntermediateImageSet(String id, Object imageInfo) 
            super.onIntermediateImageSet(id, imageInfo);
            updateViewSize((ImageInfo) imageInfo);
        

        @Override
        public void onRelease(String id) 
            super.onRelease(id);
        

        @Override
        public void onSubmit(String id, Object callerContext) 
            super.onSubmit(id, callerContext);
        

        void updateViewSize(@Nullable ImageInfo imageInfo) 
            if (imageInfo != null) 
                    //myViewHolder.ivImage.getLayoutParams().width = imageInfo.getWidth();
                    myViewHolder.ivImage.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
                    myViewHolder.ivImage.setAspectRatio((float) imageInfo.getWidth() / imageInfo.getHeight());
            
        

    ;


    ImagePipeline imagePipeline = Fresco.getImagePipeline();

    ImageRequest imageRequest = ImageRequestBuilder
            .newBuilderWithSource(Uri.parse(mList.get(position).getPhoto()))
            .setRequestPriority(Priority.HIGH)
            .setLowestPermittedRequestLevel(ImageRequest.RequestLevel.FULL_FETCH)
            .build();

    DataSource<CloseableReference<CloseableImage>> dataSource =
            imagePipeline.fetchDecodedImage(imageRequest, mContext);

    try 
        dataSource.subscribe(new BaseBitmapDataSubscriber() 
            @Override
            public void onNewResultImpl(@Nullable Bitmap bitmap) 
                if (bitmap == null) 
                    Log.d(TAG, "Bitmap data source returned success, but bitmap null.");
                

            

            @Override
            public void onFailureImpl(DataSource dataSource) 
                // No cleanup required here
            
        , CallerThreadExecutor.getInstance());
     finally 
        if (dataSource != null) 
            dataSource.close();
        
    

    Uri uri = Uri.parse( mList.get(position).getPhoto() );
    DraweeController dc = Fresco.newDraweeControllerBuilder()
            .setUri( uri )
            .setTapToRetryEnabled(true)
            .setControllerListener( listener )
            .setOldController( myViewHolder.ivImage.getController() )
            .build();

    RoundingParams rp = RoundingParams.fromCornersRadii(roundPixels, roundPixels, 0, 0);
    myViewHolder.ivImage.setController(dc);
    myViewHolder.ivImage.getHierarchy().setRoundingParams( rp );

    if(withAnimation)
        try
            YoYo.with(Techniques.ZoomIn)
                    .duration(700)
                    .playOn(myViewHolder.itemView);
        
        catch(Exception e)
    



public Image getItem(int position) 
    return mList.get(position);


public int getItemCount() 
    return null!=mList?mList.size():0;


public void addListItem(Image c, int position)
    mList.add(position, c);
    notifyItemInserted(position);



public void removeListItem(int position)
    mList.remove(position);
    notifyItemRemoved(position);


public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
    public SimpleDraweeView ivImage;
    public TextView tvTitulo;
    public TextView ivContextMenuWhats;
    public TextView ivContextMenuFace;
    public TextView ivContextMenuShare;
    public ImageView ivMenuFavorite;


    public MyViewHolder(View itemView) 
        super(itemView);
        //int position =  (int) itemView.getTag();
        ivImage = (SimpleDraweeView) itemView.findViewById(R.id.iv_image);
        tvTitulo = (TextView) itemView.findViewById(R.id.tv_titulo);
        ivContextMenuWhats = (TextView) itemView.findViewById(R.id.iv_buttom_whats);
        ivContextMenuFace = (TextView) itemView.findViewById(R.id.iv_buttom_face);
        ivContextMenuShare = (TextView) itemView.findViewById(R.id.iv_buttom_share);
        ivMenuFavorite = (ImageView) itemView.findViewById(R.id.iv_buttom_favorite);


        if( ivContextMenuWhats != null )
            ivContextMenuWhats.setOnClickListener(this);
        
        if( ivContextMenuFace != null )
            ivContextMenuFace.setOnClickListener(this);
        
        if( ivContextMenuShare != null )
            ivContextMenuShare.setOnClickListener(this);
        
        if( ivMenuFavorite != null )
            ivMenuFavorite.setOnClickListener(this);
        

    


    public void onClick(View v) 

        Image image = mList.get(getAdapterPosition());
        String tag = ivMenuFavorite.getTag().toString();

        switch (v.getId()) 
            case R.id.iv_buttom_whats: 
                FirebaseCrash.log("ImageAdapter:onClick:iv_buttom_whats");
                if (isPackageInstalled("com.whatsapp")) 
                    new MyAsyncTask(mContext, mList.get(getAdapterPosition()).getFoto(), mList.get(getAdapterPosition()).getPhoto(), "com.whatsapp").execute();
                 else 
                    Toast.makeText(mContext, mContext.getString(R.string.txt_whatsapp_not_faund), Toast.LENGTH_SHORT).show();
                
                break;
            

            case R.id.iv_buttom_face: 
                FirebaseCrash.log("ImageAdapter:onClick:iv_buttom_face");
                if (isPackageInstalled("com.facebook.katana")) 
                    new MyAsyncTask(mContext, mList.get(getAdapterPosition()).getFoto(), mList.get(getAdapterPosition()).getPhoto(), "com.facebook.katana").execute();
                 else 
                    Toast.makeText(mContext, mContext.getString(R.string.txt_facebook_not_faund), Toast.LENGTH_SHORT).show();
                
                break;
            

            case R.id.iv_buttom_share: 
                FirebaseCrash.log("ImageAdapter:onClick:iv_buttom_share");
                new MyAsyncTask(mContext, mList.get(getAdapterPosition()).getFoto(), mList.get(getAdapterPosition()).getPhoto(), "share").execute();
                break;
            
            case R.id.iv_buttom_favorite: 
                FirebaseCrash.log("ImageAdapter:onClick:iv_buttom_favorite");
            if (tag.equalsIgnoreCase("grey")) 
                sharedPreference.addFavorite(mContext, image);
                Toast.makeText(mContext,
                mContext.getResources().getString(R.string.txt_favorite_add),
                Toast.LENGTH_SHORT).show();
                //Toast.makeText(mContext, image.toString(), Toast.LENGTH_LONG).show();
                //Log.e("Cor red", tag);
                ivMenuFavorite.setTag("red");
                ivMenuFavorite.setImageResource(R.drawable.ico_heart_on);
             else 
                sharedPreference.removeFavorite(mContext, image);
                ivMenuFavorite.setTag("grey");
                ivMenuFavorite.setImageResource(R.drawable.ico_heart_off);
                // Toast.makeText(mContext, "remove" + qid ,Toast.LENGTH_SHORT).show();
                Toast.makeText(mContext,
                mContext.getResources().getString(R.string.txt_remove_favorite),
                Toast.LENGTH_SHORT).show();
                //Log.e("Cor grey", tag);
            

                break;
            
        
    


    public boolean isPackageInstalled(String packagename) 
        PackageManager pm = mContext.getPackageManager();
        try 
            pm.getPackageInfo(packagename, PackageManager.GET_ACTIVITIES);
            return true;
         catch (PackageManager.NameNotFoundException e) 
            return false;
        
    


public boolean checkFavoriteItem(Image checkImage) 
    boolean check = false;
    List<Image> myList = sharedPreference.getFavorites(mContext);
    if (myList != null) 
        for (Image image : myList) 
            if (image.equals(checkImage)) 
                check = true;

                break;
            
                    
    
    return check;

代码 xml:

<RelativeLayout
    android:layout_
    android:layout_
    android:background="#f8f8f8">

    <com.facebook.drawee.view.SimpleDraweeView
        android:id="@+id/iv_image"
        android:layout_
        android:layout_
        android:layout_alignParentTop="true"
        fresco:actualImageScaleType="center"
        fresco:failureImage="@drawable/error_img"
        fresco:placeholderImage="@drawable/loading_img"
        fresco:retryImage="@drawable/retry_img" />

    <TextView
        android:id="@+id/tv_titulo"
        android:layout_
        android:layout_
        android:background="#f8f8f8"
        android:layout_below="@+id/iv_image"
        android:padding="8dp"
        android:textColor="@color/colorPrimarytext"
        android:textSize="14sp" />

    <TextView
        android:textSize="12sp"
        android:textStyle="bold"
        android:textColor="#fff"
        android:ellipsize="marquee"
        android:gravity="top|bottom|center_vertical|fill_vertical|center|fill"
        android:id="@+id/iv_buttom_whats"
        android:background="@drawable/border_corner"
        android:paddingBottom="8dp"
        android:padding="8dp"
        android:layout_
        android:layout_
        android:text="@string/compartilhar_no_whatsapp"
        android:singleLine="true"
        android:drawableLeft="@drawable/ic_whatsapp_white"
        android:drawablePadding="8dp"
        android:marqueeRepeatLimit="marquee_forever"
        android:layout_below="@+id/tv_titulo"
        android:layout_marginLeft="8dp"
        android:layout_marginStart="8dp" />

    <TextView
        android:textSize="12sp"
        android:textStyle="bold"
        android:textColor="#fff"
        android:ellipsize="marquee"
        android:gravity="top|bottom|center_vertical|fill_vertical|center|fill"
        android:id="@+id/iv_buttom_face"
        android:background="@drawable/border_corner_face"
        android:padding="8dp"
        android:layout_
        android:layout_
        android:singleLine="true"
        android:drawableLeft="@drawable/ic_facebook_white"
        android:drawablePadding="8dp"
        android:marqueeRepeatLimit="marquee_forever"
        android:layout_below="@+id/tv_titulo"
        android:layout_toRightOf="@+id/iv_buttom_whats"
        android:layout_marginLeft="8dp"
        android:layout_marginStart="8dp" />

    <TextView
        android:textSize="12sp"
        android:textStyle="bold"
        android:textColor="#fff"
        android:ellipsize="marquee"
        android:gravity="top|bottom|center_vertical|fill_vertical|center|fill"
        android:id="@+id/iv_buttom_share"
        android:background="@drawable/border_corner_share"
        android:padding="8dp"
        android:layout_
        android:layout_
        android:singleLine="true"
        android:drawableLeft="@drawable/ic_share_white"
        android:drawablePadding="8dp"
        android:marqueeRepeatLimit="marquee_forever"
        android:layout_below="@+id/tv_titulo"
        android:layout_toRightOf="@+id/iv_buttom_face"
        android:layout_toEndOf="@+id/iv_buttom_face"
        android:layout_marginLeft="8dp"
        android:layout_marginStart="8dp" />

    <ImageView
        android:textSize="12sp"
        android:textStyle="bold"
        android:textColor="#fff"
        android:ellipsize="marquee"
        android:gravity="top|bottom|center_vertical|fill_vertical|center|fill"
        android:id="@+id/iv_buttom_favorite"
        android:padding="8dp"
        android:layout_
        android:layout_
        android:singleLine="true"
        android:background="@null"
        android:drawablePadding="8dp"
        android:marqueeRepeatLimit="marquee_forever"
        android:layout_below="@+id/tv_titulo"
        android:layout_toRightOf="@+id/iv_buttom_share"
        android:layout_toEndOf="@+id/iv_buttom_share"
        android:layout_marginLeft="8dp"
        android:layout_marginStart="8dp"
        android:contentDescription="@string/favorites"
        android:clickable="true" />

    <Space
        android:layout_
        android:layout_
        android:layout_below="@+id/iv_buttom_whats"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

感谢任何可以帮助我的人

【问题讨论】:

【参考方案1】:

您必须致电Fresco.initialize(...)。 仅在您的自定义 Application 类中执行此操作一次。 请参阅http://frescolib.org/docs/index.html 了解更多信息。

如果这不能解决问题,则可能是 ProGuard 设置不正确。如何使用 Fresco 设置 ProGuard:http://frescolib.org/docs/proguard.html

【讨论】:

【参考方案2】:

您需要在设置上下文视图之前初始化 com.facebook.drawee.view.SimpleDraweeView。你可以通过写作来实现 Fresco.initialize(this)

【讨论】:

【参考方案3】:

如果你初始化 Fresco 后仍然出现异常。

检查您的build.gradle 是否支持矢量

vectorDrawables.useSupportLibrary = true
// Remove this line and use .png(s) :(

这发生在我身上,我注意到在我支持矢量图像后出现了崩溃。

2019 年 5 月 16 日更新

请忽略我的旧答案,这是因为我在 SplashActivity 上初始化 Fresco,最好在 Application 类中初始化 Fresco。

【讨论】:

以上是关于Android - 膨胀类 com.facebook.drawee.view.SimpleDraweeView 的异常错误的主要内容,如果未能解决你的问题,请参考以下文章

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

Android 应用程序停止错误:膨胀类 android.widget.ListView

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

Xamarin Forms Android 错误膨胀类 MaterialCalendarGridView

Android错误膨胀类android.support.constraint.ConstraintLayout

在Android中膨胀类按钮时出错