I know that we need to place setContentView()
in the onCreate()
method before initializing any view otherwise it will throw a null pointer exception.
But what is the reason for it?Is the setContentView()
similar to the inflate()
method?
before initializing any view
I do not know for certain what you mean by "initializing any view". Given the rest of your question, I am going to interpret this as meaning "call findViewById()
on the activity".
You need to call setContentView()
before calling findViewById()
, because otherwise there are no widgets to find.
Is the setContentView() similar to the inflate() method?
setContentView()
will use a LayoutInflater
and inflate()
under the covers, if you pass a layout resource ID into the setContentView()
method.
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.!
Ok. Agree with @CommonsWare. In some details, let say if you have some views defined in your xml layout file and you want to use those views in you activity, so in this case you have to call setContentView(<R.layout.xml_layout_name>)
and after that to inititlalize view using findViewById(R.id.<view_name>)
with resource name as your xml layout defined name.
Ok but why we have to call
setContentView()
?
So when you call setContentView()
application activity means android nutshell will render views and prepare view hierarchy for your activity from layout file. Just remember you have defined views in layout using xml file and you are using those views in Java code, so for that all works to preparing views for you will do setContentView()
that's why when you call findViewById()
without setContentView()
then your application can not find views from view hierarchy and it will throw NullPointerException
.
setContentView()
similar to theinflate()
method ?
Some how, because both are doing the same thing, rendering views from given xml layout files but scope of works is different. setContentView() provides views throughout your activity scope while inflate() will only gives you a view from the layout file, that's why whenever you have used inflate() you have to always use reference of return view
to call findViewById()
like
pseudo code only for your understanding,
View view = infalter.inflate(<R.layout.<file_name>>);
TextView mextView = view.findViewById(R.id.textView);
And yes, setContentView()
uses the same inflater.inflate()
method too.
And when setContentView()
and inflate()
not required?
If you are creating dynamically views, in java code then you don't have to required call either setContentView()
or inflate()
.
Note: In old android version when you create a dynamically views using java code and you pass it to some ListView's header or footer it won't work. For this views must be inflated before set to the ListView.
来源:https://stackoverflow.com/questions/30822210/reason-for-nullpointerexception-if-setcontentview-is-not-used