Null pointer Exception - findViewById()

后端 未结 10 2688
情歌与酒
情歌与酒 2020-11-21 04:16

Can anyone help me to find out what can be the issue with this program. In the onCreate() method the findViewById() returns null for all ids and th

10条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-21 05:00

    findViewById() returns a View if it exists in the layout you provided in setContentView(), otherwise it returns null and that's what happening to you.

    Example if you setContentView(R.layout.activity_first); and then call findViewById(R.id.first_View); it will return a View which is your layout.

    But if you call findViewById(R.id.second_View); it will return null since there is not a view in your activity_first.xml layout called @+id/second_View.

提交回复
热议问题