findViewById() returns null for custom component in layout XML, not for other components

前端 未结 18 1530
情歌与酒
情歌与酒 2020-11-28 10:36

I have a res/layout/main.xml including these elements and others:



        
18条回答
  •  盖世英雄少女心
    2020-11-28 11:33

    I had the same problem. My mistake was that: I wrote

            LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View layout=inflater.inflate(R.layout.dlg_show_info, null);
            alertDlgShowInfo.setView(layout);
            TableRow trowDesc=(TableRow)findViewById(R.id.trowDesc);
    

    and as I used an inflater to "load" the view from an XML file, the last line was wrong. To solve it, I had to write:

    TableRow trowDesc=(TableRow)layout.findViewById(R.id.trowDesc);
    

    I wrote my solution, in case someone have the same problem.

提交回复
热议问题