findViewById not working for an include?

后端 未结 7 1266
终归单人心
终归单人心 2020-11-27 06:05

I have an include like:


The include layout looks like (te

7条回答
  •  遥遥无期
    2020-11-27 06:27

    If you have given the id to the include element and you have only single view in the included layout (not in any ViewGroup), in my case AdView, then use the id of include element instead of included view.

    
    
    
    
    

    activity_main.xml

    
    

    In activity

    AdView nullAdView=findViewById(R.id.adView); //this will give null
    AdView adView = findViewById(R.id.adsLayout);  //this will be AdView
    

    If you do not give id to include element then it is ok to use view id. Also if your view is inside any ViewGroup like FrameLayout then it is ok to use view id.

提交回复
热议问题