findViewById() not working in a not MainActivity class

后端 未结 4 1455
耶瑟儿~
耶瑟儿~ 2020-12-13 18:46

I have a text view in my Lyout and I would like to set some text to this textview. This should be made in a class which is not a MainActivity class.

The problem is t

4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-13 19:18

    When you call findViewById matters. The layout must have already happened. You must already have set a content view, etc.

    There are ways to work around this, as shown in other answers, but they work fundamentally differently from findViewById, and should only be used as a substitute if you understand exactly how they work. Most often it's far more efficient to just wait until after the initial layout has already occurred.

提交回复
热议问题