您的位置:首页 > 移动开发 > Android开发

使用include findViewById 返回null

2015-10-09 19:26 447 查看

使用include findViewById 返回null

layout 文件如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_chat_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background">

<include
android:id="@+id/head_include"
layout="@layout/layout_activity_head" />

</RelativeLayout>


layout_activity_head 布局如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/head_ly"
android:layout_width="match_parent"
android:layout_height="44dip"
android:background="#ff5cc1cb">
</RelativeLayout>


findViewById(R.id.head_ly)
返回值为null

原因:include 设置了id,会覆盖掉layout_activity_head布局文件中根layout 的 id

解决办法:
findViewById(R.id.layout_activity_head)
或者
去掉 include 的id
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android include