android - 避免将 null 作为 View 根传递(需要在 inflated layout 的根元素上解析布局参数)

标签 android android-layout warnings layout-inflater

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

Avoid passing null as the view root (need to resolve layout parameters on the inflated layout's root element)

它在 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;
    }

}

最佳答案

而不是做

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

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

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

关于android - 避免将 null 作为 View 根传递(需要在 inflated layout 的根元素上解析布局参数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24832497/

相关文章:

java - 如何获取当前时间和日期并设置在我的应用程序中执行操作的特定时间

android - XML 中的“图像上缺少 contentDescription 属性”

cucumber - 从 "rake cucumber"中删除关于 ansicon 的警告

android - Admob 状态恢复为非 Activity 状态

Android SQLiteDiskIOException(代码 522 SQLITE_IOERR_SHORT_READ)

android - 为什么 RecyclerView 中的交错网格布局元素在加载图像时会自动交换其位置?

android - Android 中 Activity 与其布局的关系

java - EditText 按需小部件

r - 在 R 中抑制但捕获警告

Android:如何使 swipeRefreshLayout 更难向下滑动