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

275

如果将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;
    }

}
7个回答

409

不要做...

convertView = infalInflater.inflate(R.layout.list_item, null);

convertView = infalInflater.inflate(R.layout.list_item, parent, false);

它将使用给定的父级对其进行充气,但不会将其附加到该父级。


39
在从Activity中膨胀视图时,您应该使用什么?在parent的位置上,应该使用什么? - Alexander Kuznetsov
3
如果你尝试设置活动的内容,应该使用 setContentView(layoutId)。如果你想向现有的 ViewGroup 添加一个新视图,你应该只需传递父级并让 inflater 自动附加新视图。 - Coeffect
5
еҪ“е®һзҺ°InputMethodServiceзҡ„onCreateInputView()ж—¶пјҢдјҡйҒҮеҲ°дёҺд№ӢеүҚзӣёеҗҢзҡ„й—®йўҳпјҲжІЎжңүзҲ¶зә§пјүгҖӮ - Ted Hopp
1
糟糕,这是一个更好的答案... https://dev59.com/_18d5IYBdhLWcg3w1E50 - Siddharth
6
自定义AlertDialog的视图时,父视图应该是什么? - user25
显示剩余6条评论

62

56

因为一些原因,使用View.inflate而不是从layoutinflater中膨胀会使lint错误消失。我想在这里发布这个帖子,因为这个线程在Google搜索的最上面...

view = View.inflate(context,R.layout.custom_layout,null);

5
这样可以消除Lint警告,但并不能解决问题本身,因为View.inflate()在幕后调用LayoutInflater。你应该修复原因,而不仅仅是删除消息。 - lsrom

36

编辑:

在我回答这个问题的时候,我不知道有 Lint 抑制(suppressions)。最好是使用抑制 Lint 而不是欺骗 IDE。将以下代码添加到代码行或方法之前:


``` //noinspection Lint ```
@SuppressLint("InflateParams")
当您确实没有任何父级(例如为AlertDialog创建视图),则除了传递null之外,没有其他办法。因此,请执行以下操作以避免警告:
final ViewGroup nullParent = null;
convertView = layoutInflater.inflate(R.layout.list_item, nullParent);

75
压制比欺骗Lint让它工作更好。 - StarWind0
4
我不确定这个答案是否正确。我成功地使用ViewGroup root = (ViewGroup) myActivity.findViewById(R.id.my_main_content);,其中my_main_content是我活动布局文件中最外层容器的ID。 - ban-geoengineering
13
为了禁止此处的Lint提示,您需要在方法上方添加@SuppressLint("InflateParams") - ban-geoengineering
我发现@ban-geoengineering的回复最有帮助,它不仅消除了消息,还解决了问题。这是适用于我的Kotlin解决方案。val rootView = findViewById<ViewGroup>(R.id.container) .... imageHolder = vInflater.inflate(R.layout.recycler_view_target, rootView,false) 这使我能够在RecyclerView Holder中放置不同的视图。 - BroPage

12

使用下面的代码可以创建 AlertDialog

convertView = layoutInflater.inflate(R.layout.list_item, findViewById(android.R.id.content), false);

7
我在寻找解决方法时发现了一条很好的信息。根据Dave Smith的说法,布局膨胀是Android上下文中使用的术语,用于指示何时解析XML布局资源并将其转换为View对象层次结构。 这里要求的ViewGroup作为inflate方法参数的一部分被用于继承更高级别的样式。虽然传递null可能看起来无害,但实际上会在以后导致应用程序出现严重问题。可以在此处阅读更多相关信息 here

2

这里有一张参考图片:

enter image description here

return inflater.inflate(R.layout.some_layout, container, true); 
// for the last parameter use true, false or simply don't declare

顺便说一句...导入ANDROIDX.fragment.app.Fragment; - Aaron Peisakhovich

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接