首先,是的,我读了关于这个话题的所有其他帖子。不仅仅是这个网站的人…(你看,我有点沮丧)

大多数都建议使用android:id,而不是XML文件中的id。我做到了。

我从别人那里了解到,这种观点。findViewById的工作方式与Activity.findViewById不同。我也处理了。

在我的location_layout.xml中,我使用:

<FrameLayout .... >
    <some.package.MyCustomView ... />

    <LinearLayout ... >
        <TextView ...
            android:id="@+id/txtLat" />
        ...
    </LinearLayout>
</FrameLayout>

在我的活动中,我做:

...
setContentView( R.layout.location_layout );

在我的自定义视图类中:

... 
TextView tv = (TextView) findViewById( R.id.txtLat );

返回null。这样做,我的Activity工作得很好。所以这可能是因为活动。findViewById和View。findViewById差异。所以我存储上下文传递给本地的海关视图构造函数,并尝试:

...
TextView tv = (TextView) ((Activity) context).findViewById( R.id.txtLat );

它也返回null。

然后,我改变了我的自定义视图扩展ViewGroup而不是视图,并改变了location_layout.xml,让TextView是我的自定义视图的直接子,以便视图。findViewById应该正常工作。令人惊讶的是:它并没有解决任何问题。

我到底做错了什么?

欢迎任何意见。


当前回答

在我的情况下,我已经膨胀布局,但子视图返回null。最初我是这么想的:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_history);

    footerView = ((LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.listview_footer, null, false);
    pbSpinner = (ProgressBar) findViewById(R.id.pbListviewFooter);
    tvText = (TextView) findViewById(R.id.tvListviewFooter);
    ...
}

然而,当我把它改成下面的时候,它工作了:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_history);

    footerView = ((LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.listview_footer, null, false);
    pbSpinner = (ProgressBar) footerView.findViewById(R.id.pbListviewFooter);
    tvText = (TextView) footerView.findViewById(R.id.tvListviewFooter);
    ...
}

关键是要特别引用已经膨胀的布局,以便获得子视图。即添加footerView:

footerView.findViewById……

其他回答

如果你在Fragment中,findViewById也可以返回null。如上所述:在Fragment中找到viewbyid

你应该调用getView()来返回Fragment中最顶层的View。然后你可以找到布局项(按钮,文本视图等)

从布局资源中设置活动内容。 即,setContentView (R.layout.basicXml);

膨胀布局!!(包含id)

在我的例子中,findViewById()返回null,因为元素被写入的布局没有被膨胀…

如。 fragment_layout.xml

<ListView
android:id="@+id/listview">

findViewById(R.id.listview)返回null,因为我没有做 inflater.inflate (R.layout.fragment_layout……,……); 在这之前。

希望这个答案能帮助到你们。

在我的情况下,我已经膨胀布局,但子视图返回null。最初我是这么想的:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_history);

    footerView = ((LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.listview_footer, null, false);
    pbSpinner = (ProgressBar) findViewById(R.id.pbListviewFooter);
    tvText = (TextView) findViewById(R.id.tvListviewFooter);
    ...
}

然而,当我把它改成下面的时候,它工作了:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_history);

    footerView = ((LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.listview_footer, null, false);
    pbSpinner = (ProgressBar) footerView.findViewById(R.id.pbListviewFooter);
    tvText = (TextView) footerView.findViewById(R.id.tvListviewFooter);
    ...
}

关键是要特别引用已经膨胀的布局,以便获得子视图。即添加footerView:

footerView.findViewById……

对我来说,它返回null,因为给定的控件是(以编程方式)隐藏的。当我只在控件可见时调用findViewByID(id)时,它又开始工作了。