Reason for NullPointerException if setContentView() is not used

前端 未结 3 1948
执笔经年
执笔经年 2020-12-12 06:57

I know that we need to place setContentView() in the onCreate() method before initializing any view otherwise it will throw a null pointer exceptio

3条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-12 07:05

    If no content View is set then from where you will reference the views like EditText,TextView,ListVIew and all other components which you have used in your layout.

    It is like you have items in your bucket and its cover is locked for safety, you came in house without bucket and forgot it in the car and your mom asked you to put items 1 by 1 on Kitchen counter , but you don't have bucket?? so first you will get bucket then you will take out items from it.

    Simply first you have to have a Container in your activity so that you can reference its items by using their ID which are assigned in layout xml. Hope it is clear to you.!

提交回复
热议问题