避免将null作为视图根传递(需要解析膨胀布局的根元素上的布局参数)

has*_*ash 218 android warnings android-layout layout-inflater

为root studio传递null给了我这个警告:

避免将null作为视图根传递(需要解析膨胀布局的根元素上的布局参数)

它显示为空值getGroupView.请帮忙.

public class ExpandableListAdapter extends BaseExpandableListAdapter {

    private Context _context;
    private List<String> _listDataHeader; // header titles
    // child data in format of header title, child title
    private HashMap<String, List<String>> _listDataChild;

    public ExpandableListAdapter(Context context, List<String> listDataHeader,
                                 HashMap<String, List<String>> listChildData) {
        super();
        this._context = context;
        this._listDataHeader = listDataHeader;
        this._listDataChild = listChildData;
    }

    @Override
    public Object getChild(int groupPosition, int childPosititon) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .get(childPosititon);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public View getChildView(int groupPosition, final int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {

        final String childText = (String) getChild(groupPosition, childPosition);

        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_item, null);


        }

        TextView txtListChild = (TextView) convertView
                .findViewById(R.id.lblListItem);

        txtListChild.setText(childText);
        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return this._listDataHeader.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return this._listDataHeader.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        String headerTitle = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_group, null);
        }

        TextView lblListHeader = (TextView) convertView
                .findViewById(R.id.lblListHeader);
        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(headerTitle);

        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

}
Run Code Online (Sandbox Code Playgroud)

Coe*_*ect 346

而不是做

convertView = infalInflater.inflate(R.layout.list_item, null);
Run Code Online (Sandbox Code Playgroud)

convertView = infalInflater.inflate(R.layout.list_item, parent, false);
Run Code Online (Sandbox Code Playgroud)

它将使用给定的父级对其进行膨胀,但不会将其附加到父级.

  • @Coeffect但是在活动中膨胀时我应该使用什么?我应该使用什么而不是父母? (24认同)
  • 警报对话框的自定义视图怎么样,父应该是什么? (6认同)
  • @Coeffect如果你没有父母怎么办?例如,在TabHost.TabContentFactory中 (4认同)
  • @AlexanderKuznetsov我想,取决于你想做什么.如果您正在尝试设置活动的内容,则应该使用`setContentView(layoutId)`.如果您尝试向现有ViewGroup添加新视图,则应该只传递父视图并让inflater附加新视图. (3认同)
  • 为`InputMethodService`实现`onCreateInputView()`时会出现同样的问题(没有父). (3认同)

Mak*_*iev 51

我发现了一篇很好的文章LayoutInflater.作者解释了该inflate方法的所有版本如何工作并给出了ListView和的例子AlertDialog

http://www.doubleencore.com/2013/05/layout-inflation-as-intended/

更新#1.

这个答案最近对我有帮助. /sf/answers/351954501/


Thu*_*ick 31

在这里,由于某种原因,使用View.inflate而不是从layoutinflater进行膨胀会使lint错误消失.以为我会在这里发布这个帖子,因为这个帖子位于Google搜索的顶部...

view = View.inflate(context,R.layout.custom_layout,null);
Run Code Online (Sandbox Code Playgroud)

  • 这消除了 Lint 警告,但没有消除问题本身,因为 View.inflate() 在幕后调用 LayoutInflater。您应该解决原因,而不仅仅是删除消息。 (6认同)

Mou*_*usa 28

如果你真的没有parent(例如创建视图AlertDialog),除了传递之外别无他法null.所以这样做是为了避免警告:

final ViewGroup nullParent = null;
convertView = layoutInflater.inflate(R.layout.list_item, nullParent);
Run Code Online (Sandbox Code Playgroud)

  • 做抑制比哄骗工作更好. (53认同)
  • 要为此抑制Lint,请在方法上方添加`@SuppressLint("InflateParams"). (9认同)
  • 我不确定这个答案是否正确。我成功地使用了`ViewGroup root = (ViewGroup) myActivity.findViewById(R.id.my_main_content);`,其中`my_main_content`是我的活动布局文件中最外层容器的ID。 (4认同)

Mos*_*war 8

对于AlertDialog波纹管代码可以使用

convertView = layoutInflater.inflate(R.layout.list_item, findViewById(android.R.id.content), false);
Run Code Online (Sandbox Code Playgroud)


ojo*_*ifu 7

我在寻找解决此问题的方法时发现了一个很好的信息。根据戴夫史密斯的说法,

布局膨胀是在 Android 上下文中使用的术语,用于指示何时解析 XML 布局资源并将其转换为 View 对象的层次结构。

这里ViewGroup被要求作为 inflate 方法的一部分参数用于继承更高级别的样式。虽然传递 null 看起来无害,但它实际上可能会在以后为您的应用程序带来严重问题。在此处阅读更多相关信息 。