Code before setContentView trouble

后端 未结 2 572
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-21 04:14

My question is if it is possible to write code before setContentView() in the onCreate() method of the main Activity. In the code below I

相关标签:
2条回答
  • 2021-01-21 04:55

    The setContentView() method sets the content of your XML file as the View, which is shown by the Activity.

    You're calling setVariables() before you've specified any View to be shown.

    That's why the error raises. The compiler doesn't know where that View belongs to. If you want to use a ResourceView, you have to set it first.

    0 讨论(0)
  • 2021-01-21 05:08

    You can execute any code you want before the setContentView() method as long as it doesn't refer to (parts of) the View, which isn't set yet.

    Since your setVariables() method refers to the contents of the View, it can't be executed.

    0 讨论(0)
提交回复
热议问题