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

大多数都建议使用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应该正常工作。令人惊讶的是:它并没有解决任何问题。

我到底做错了什么?

欢迎任何意见。


当前回答

FWIW,我没有看到任何人解决这个问题在完全相同的方式,我需要。在编译时没有抱怨,但我在运行时得到一个空视图,并以适当的顺序调用东西。也就是说, findViewById () 后 setContentView()。 问题是,我的视图是在content_main.xml中定义的,但在我的activity_main.xml中,我缺少这一条语句:

<include layout="@layout/content_main" />

当我将其添加到activity_main.xml时,不再有NullPointer。

其他回答

在我的情况下,我已经膨胀布局,但子视图返回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……

在我的特殊情况下,我试图添加一个页脚到一个ListView。onCreate()中的下面调用返回null。

TextView footerView = (TextView) placesListView.findViewById(R.id.footer);

将此更改为膨胀页脚视图,而不是通过ID查找它解决了这个问题。

View footerView = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer_view, null, false);

在我的例子中,我的项目中有两个活动,main.xml和main2.xml。从一开始,main2是main的副本,一切都工作得很好,直到我添加了新的TextView到main2,所以R.id。textview1成为可用的应用程序的其余部分。然后我试图通过标准调用获取它:

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

它总是空的。事实证明,在onCreate构造函数中,我实例化的不是main2,而是另一个。我有:

setContentView(R.layout.main);

而不是

setContentView(R.layout.main2);

我到这里后,在工地上注意到了这一点。

可能,你在调用setContentView之前调用了findViewById ? 如果是这种情况,尝试在调用setContentView之后调用findViewById

当我试图为ListView做一个自定义视图时,我也面临着类似的问题。

我的解决方法很简单:

public View getView(int i, View view, ViewGroup viewGroup) {

    // Gets the inflater
    LayoutInflater inflater = LayoutInflater.from(this.contexto);

    // Inflates the layout
    ConstraintLayout cl2 = (ConstraintLayout) 
    inflater.inflate(R.layout.custom_list_view, viewGroup, false);

    //Insted of calling just findViewById, I call de cl2.findViewById method. cl2 is the layout I have just inflated. 
     TextView tv1 = (TextView)cl2.findViewById(cl2);