Android findViewWithTag返回null

Android findViewWithTag返回null,android,xml,Android,Xml,我使用的是PagerAdapter,希望使用findViewWithTag方法,但它总是返回null 在实例化项中,我像这样实例化我的页面视图 public Object instantiateItem(View view, int position) { View pageView = getPage(position); ((ViewPager) view).addView(pageView); return pageView; } 在getPage方法中,我执行

我使用的是PagerAdapter,希望使用findViewWithTag方法,但它总是返回null

在实例化项中,我像这样实例化我的页面视图

public Object instantiateItem(View view, int position) {
    View pageView = getPage(position);
    ((ViewPager) view).addView(pageView);
    return pageView;
} 
在getPage方法中,我执行以下操作:

 LinearLayout pageView = (LinearLayout)mInflater.inflate(R.layout.pageritem_news,null,false);
在for循环中,我试图通过标记获取pageView中的视图,但它总是返回null,有人知道吗

这个问题可能是由pageritem_news.xml引起的,因为我正在使用include将视图包含到布局中,并在xml中的
中定义android:tag

更新 pageritem_news.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:baselineAligned="false"
    android:orientation="vertical" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:baselineAligned="false"
    android:weightSum="3" >

    <include
        android:id="@+id/newspage_item_row_0_left"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:tag="0"
        layout="@layout/listitem_news"
        android:visibility="invisible" />

    <include
        android:id="@+id/newspage_item_row_0_middle"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:tag="5"
        layout="@layout/listitem_news"
        android:visibility="invisible" />

    <include
        android:id="@+id/newspage_item_row_0_right"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:tag="10"
        layout="@layout/listitem_news"
        android:visibility="invisible" />
</LinearLayout>

<View
    android:layout_width="match_parent"
    android:layout_height="@dimen/list_divider_height"
    android:background="@drawable/horizontal_divider" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:baselineAligned="false"
    android:weightSum="3" >

    <include
        android:id="@+id/newspage_item_row_1_left"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:tag="1"
        layout="@layout/listitem_news"
        android:visibility="invisible" />

    <include
        android:id="@+id/newspage_item_row_1_middle"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:tag="6"
        layout="@layout/listitem_news"
        android:visibility="invisible" />

    <include
        android:id="@+id/newspage_item_row_1_right"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:tag="11"
        layout="@layout/listitem_news"
        android:visibility="invisible" />
</LinearLayout>

<View
    android:layout_width="match_parent"
    android:layout_height="@dimen/list_divider_height"
    android:background="@drawable/horizontal_divider" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:baselineAligned="false"
    android:weightSum="3" >

    <include
        android:id="@+id/newspage_item_row_2_left"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:tag="2"
        layout="@layout/listitem_news"
        android:visibility="invisible" />

    <include
        android:id="@+id/newspage_item_row_2_middle"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:tag="7"
        layout="@layout/listitem_news"
        android:visibility="invisible" />

    <include
        android:id="@+id/newspage_item_row_2_right"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:tag="12"
        layout="@layout/listitem_news"
        android:visibility="invisible" />
</LinearLayout>

<View
    android:layout_width="match_parent"
    android:layout_height="@dimen/list_divider_height"
    android:background="@drawable/horizontal_divider" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:baselineAligned="false"
    android:weightSum="3" >

    <include
        android:id="@+id/newspage_item_row_3_left"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:tag="3"
        layout="@layout/listitem_news"
        android:visibility="invisible" />

    <include
        android:id="@+id/newspage_item_row_3_middle"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:tag="8"
        layout="@layout/listitem_news"
        android:visibility="invisible" />

    <include
        android:id="@+id/newspage_item_row_3_right"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:tag="13"
        layout="@layout/listitem_news"
        android:visibility="invisible" />
</LinearLayout>

<View
    android:layout_width="match_parent"
    android:layout_height="@dimen/list_divider_height"
    android:background="@drawable/horizontal_divider" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:baselineAligned="false"
    android:weightSum="3" >

    <include
        android:id="@+id/newspage_item_row_4_left"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        layout="@layout/listitem_news"
        android:tag="4"
        android:visibility="invisible" />

    <include
        android:id="@+id/newspage_item_row_4_middle"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        layout="@layout/listitem_news"
        android:tag="9"
        android:visibility="invisible" />

    <include
        android:id="@+id/newspage_item_row_4_right"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        layout="@layout/listitem_news"
        android:tag="14"
        android:visibility="invisible" />
</LinearLayout>

</LinearLayout>

PagerAdapter的相关代码部分:

@Override
public Object instantiateItem(View view, int position) {
    View pageView = getPage(position);
    ((ViewPager) view).addView(pageView);
    return pageView;
}


private View getPage(int position) {
    LinearLayout pageView = (LinearLayout)mInflater.inflate(R.layout.pageritem_news,null ,false);

    int index=0;
    View viewToBuild = null;
    while (index < mMovers.size()) {
        viewToBuild = pageView.findViewWithTag(String.valueOf(index));
        System.out.println("View to build: " + viewToBuild);
        if (viewToBuild != null) {
            buildNewsView(viewToBuild, mNews.get(index));
        }
        index++;
    }

    return pageView;
}
@覆盖
公共对象实例化项(视图,int位置){
查看页面视图=获取页面(位置);
((ViewPager)视图)。添加视图(pageView);
返回页面视图;
}
私有视图getPage(内部位置){
LinearLayout页面视图=(LinearLayout)mInflater.flate(R.layout.pageritem_news,null,false);
int指数=0;
视图viewToBuild=null;
而(索引
根据,您只能在使用
时覆盖
布局*
属性(基本上,任何将进入相应的
布局参数
对象的内容)

此功能有一个增强请求

我建议您找到一种方法来完成您正在尝试使用ID进行的操作。例如,您可以拥有一组可以迭代的ID。您可以在每个配置的基础上更改此数组(这样它就不在您的代码中,并且您拥有用XML定义标记的所有好处)。

根据,在使用
时,您只能覆盖
布局*
属性(基本上,任何将进入相应
布局参数
对象的内容)

此功能有一个增强请求


我建议您找到一种方法来完成您正在尝试使用ID进行的操作。例如,您可以拥有一组可以迭代的ID。您可以在每个配置的基础上更改此数组(这样它就不在您的代码中,并且您拥有用XML定义标记的所有好处)。

向我们展示您的XML(包括的布局和包含它的位置)而实际调用
findViewWithTag
我已经更新了我的帖子你在
pageritem_news.xml
中有你的标签,但是在你提供的代码中,你可以在
pageritem_movers.xml
中查找标签,你也可以显示这个文件吗?向我们展示你的xml(包括的布局和包含它的位置)而实际调用的
findViewWithTag
我已经更新了我的帖子您的标签在
pageritem\u news.xml
中,但是在您提供的代码中,您可以在
pageritem\u movers.xml
中查找标签,您也可以显示此文件吗?谢谢您的帮助。我从没想过问题会出在使用include上。我的另一个想法是id列表,但我不知道为什么“标签方式”听起来更酷:)谢谢你的帮助。我从没想过问题会出在使用include上。我的另一个想法是id列表,但我不知道为什么“标签方式”听起来更酷:)