android.view.InflateException: Binary XML file line #12: Error inflating class

后端 未结 30 1981
伪装坚强ぢ
伪装坚强ぢ 2020-11-22 13:48

I am receiving many errors of kind displayed in the subj. These errors seems to be occasional and I cannot reproduce them. From stack I can learn that such error may occurs

相关标签:
30条回答
  • 2020-11-22 14:44

    The inflate exception is not actually the problem, but really comes from another deeper issue in your layout that is then wrapped in an InflateException. A common issue is an out of memory exception when trying to inflate an imageview loading a drawable resource. If one of this resources has a high pixel resolution it would take a lot of memory causing then an inflate exception.

    So basically verify that the pixel resolution in your drawables images are just the minimum necessary for your layout.

    0 讨论(0)
  • 2020-11-22 14:45

    I had the same error when creating custom view with only one constructor, try to define all constructor for your custom views.

       public CustomWebView(Context context) {
            super(context);
            init();
        }
    
        public CustomWebView(Context context, AttributeSet attrs) {
            super(context, attrs);
            init();
        }
    
        public CustomWebView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            init();
        }
    
    0 讨论(0)
  • 2020-11-22 14:46

    This can also happen if you use a VectorDrawable using support library and forgot to use app:srcCompat instead of android:src

    Exact error is: Binary XML file line #XX: Error inflating class ImageView

    See that link

    0 讨论(0)
  • 2020-11-22 14:46

    I encountered the same bug and found the root reason is:

    Use Application Context to inflate view.

    Inflating with Activity Context fixed the bug.

    0 讨论(0)
  • 2020-11-22 14:46

    In case someone gets similar issues, I had such an issue while inflating the view:

    View.inflate(getApplicationContext(), R.layout.my_layout, null)

    fixed by replacing getApplicationContext() with this

    0 讨论(0)
  • 2020-11-22 14:47

    I had the same error on Kitkat which was because i had android:tint on one of the imageViews. It would work fine on Lollipop but crash on Kikkat with Error Inflating class .

    Fixed it by using app:tint on the AppCompatImageView which i was dealing with.

    0 讨论(0)
提交回复
热议问题