LayoutInflater.createViewFromTag() 时,name = null 异常

当LayoutInflater.inflate(context,resId,attachedRoot)中会调用createViewFromTag(View parent, String name, Context context, AttributeSet attrs,
boolean ignoreThemeAttr)

 /**
     * Creates a view from a tag name using the supplied attribute set.
     * 

* Note: Default visibility so the BridgeInflater can * override it. * * @param parent the parent view, used to inflate layout params * @param name the name of the XML tag used to define the view * @param context the inflation context for the view, typically the * {@code parent} or base layout inflater context * @param attrs the attribute set for the XML tag used to define the view * @param ignoreThemeAttr {@code true} to ignore the {@code android:theme} * attribute (if set) for the view being inflated, * {@code false} otherwise */ View createViewFromTag(View parent, String name, Context context, AttributeSet attrs, boolean ignoreThemeAttr) { if (name.equals("view")) { // tag.1 name = attrs.getAttributeValue(null, "class"); // tag.2 } // Apply a theme wrapper, if allowed and one is specified. if (!ignoreThemeAttr) { final TypedArray ta = context.obtainStyledAttributes(attrs, ATTRS_THEME); final int themeResId = ta.getResourceId(0, 0); if (themeResId != 0) { context = new ContextThemeWrapper(context, themeResId); } ta.recycle(); } if (name.equals(TAG_1995)) { //throw exception // Let's party like it's 1995! return new BlinkLayout(context, attrs); } .... }

name.equals(TAG_1995) 这里name = null。
从代码里面的线索是:
tag.1 这里返回的是 true ,即name = “view”
tag.2 这里就返回了null

那么 name 为什么等于“view”?
看了一下代码,才发现自己的错误:


那这里的view代表什么呢?
对于一个自定义View来说,在XML里面有两种写法:

  // Android Studio 3.0提示没有class 这个属性名,应该是不建议用了吧

也就是说对于一个自定义Widget组件,name = 最开头写的字段。自己手误把View 写成 view 没想到还有这个问题,记录一下。

你可能感兴趣的:(LayoutInflater.createViewFromTag() 时,name = null 异常)