我遇到类似这个问题的问题findViewById returns null on a LinearLayout inside an <include>d view
我有类似的问题:
调用include块的xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
...>
<include
android:id="@+id/popupMenu"
layout="@layout/mainmenu_popup" />
</RelativeLayout>
和mainmenu_popup.xml包含所包含的代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:visibility="gone"
android:layout_marginTop="@dimen/action_bar_height"
android:layout_width="0dp"
android:layout_height="0dp">
<fragment
android:id="@+id/mainmenupopup_fragment"
class="RetailMobile.MainMenuFragment" />
<TextView
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_below="@id/mainmenupopup_fragment"
android:background="@color/dark_blue"
android:id="@+id/blueLine" />
<RelativeLayout
...
</RelativeLayout>
</RelativeLayout>
我遵循Luksprog的回答
RelativeLayout popupMenu = this.Activity.FindViewById<RelativeLayout>(Resource.Id.popupMenu);
但popupMenu == null
我还检查了这个帖子findViewById not working for an include?但没有给出正面结果。
答案 0 :(得分:2)
将android:id
添加到mainmenu_popup.xml中的RelativeLayout,然后按ID定义的视图查找
答案 1 :(得分:0)
我找到了解决方案 mainmenu_popup.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/popup_mainmenu">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/popup_mainmenu_inner"> ...
然后我引用popup_mainmenu_inner:
this.Activity.FindViewById<RelativeLayout>(Resource.Id.popup_mainmenu_inner);