Programing

널을 뷰 루트로 전달하지 마십시오 (팽창 된 레이아웃의 루트 요소에서 레이아웃 매개 변수를 해결해야 함)

lottogame 2020. 4. 28. 08:16
반응형

널을 뷰 루트로 전달하지 마십시오 (팽창 된 레이아웃의 루트 요소에서 레이아웃 매개 변수를 해결해야 함)


루트 스튜디오에 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;
    }

}

대신에

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

하다

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

주어진 부모와 함께 팽창 시키지만 부모에게 첨부하지는 않습니다.


에 대한 좋은 기사를 찾았습니다 LayoutInflater. 저자는 방법의 모든 버전이 어떻게 inflate작동 하는지 설명 ListView하고AlertDialog

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

업데이트 # 1.

이 답변은 최근에 나에게도 도움이되었습니다. https://stackoverflow.com/a/5027921/1065835


Here ya go, for some reason using View.inflate instead of inflating from a layoutinflater makes the lint error disappear. Thought I'd post this here since this thread is at the top of the Google Search...

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

When you really don't have any parent (for example creating view for AlertDialog), you have no other way than passing null. So do this to avoid warning:

final ViewGroup nullParent = null;
convertView = layoutInflater.inflate(R.layout.list_item, nullParent);

A nice information I found while looking for a way to solve this.According to Dave Smith,

Layout inflation is the term used within the context of Android to indicate when an XML layout resource is parsed and converted into a hierarchy of View objects.

The ViewGroup been asked for here as part of the inflate method parameters is used to inherit higher level styling.While passing null can seem harmless, it can actually cause serious problems for your app later on. Read more about this here.


For AlertDialog bellow code can be used

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

참고URL : https://stackoverflow.com/questions/24832497/avoid-passing-null-as-the-view-root-need-to-resolve-layout-parameters-on-the-in

반응형