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?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
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 usingfindViewById(R.id.<view_name>)
with resource name as your xml layout defined name.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 dosetContentView()
that's why when you callfindViewById()
withoutsetContentView()
then your application can not find views from view hierarchy and it will throwNullPointerException
.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 callfindViewById()
likepseudo code only for your understanding,
And yes,
setContentView()
uses the sameinflater.inflate()
method too.And when
setContentView()
andinflate()
not required?If you are creating dynamically views, in java code then you don't have to required call either
setContentView()
orinflate()
.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.
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 callingfindViewById()
, because otherwise there are no widgets to find.setContentView()
will use aLayoutInflater
andinflate()
under the covers, if you pass a layout resource ID into thesetContentView()
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.!