XmlPullParserException Binary XML file line #17 tag requires viewportWidth > 0

前端 未结 7 1810
轻奢々
轻奢々 2021-02-18 15:31

This is a follow up question to this question:

Update Android Support Library to 23.2.0 cause error: XmlPullParserException Binary XML file line #17 tag re

7条回答
  •  名媛妹妹
    2021-02-18 15:47

    Event after trying the already provided answers, the app was crashing on some devices (mainly Samsung). So along with that, I tried loading vector drawables like this

    Drawable drawable = AppCompatDrawableManager.get().getDrawable(context, R.drawable.resource_id);
    

    This AppCompatDrawableManager internally tries to fetch drawable with various methods:

    Drawable getDrawable(@NonNull Context context, @DrawableRes int resId,
                boolean failIfNotKnown) {
            checkVectorDrawableSetup(context);
    
            Drawable drawable = loadDrawableFromDelegates(context, resId);
            if (drawable == null) {
                drawable = createDrawableIfNeeded(context, resId);
            }
            if (drawable == null) {
                drawable = ContextCompat.getDrawable(context, resId);
            }
    
            if (drawable != null) {
                // Tint it if needed
                drawable = tintDrawable(context, resId, failIfNotKnown, drawable);
            }
            if (drawable != null) {
                // See if we need to 'fix' the drawable
                DrawableUtils.fixDrawable(drawable);
            }
            return drawable;
        }
    

    Thus it works on all android versions and all devices (hopefully).

    Note: Don't try to use Picasso(2.5.2)'s or Glide(3.7.0)'s method to load vector drawables. Because they internally use deprecated getDrawable(@DrawableRes int id) method. Which will result in Resources.NotFoundException on some devices.

提交回复
热议问题