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

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

我到底做错了什么?

欢迎任何意见。


当前回答

如果在自定义视图中调用了错误的超级构造函数,FindViewById可以为空。ID标记是attrs的一部分,因此如果忽略attrs,则删除ID。

这是错误的

public CameraSurfaceView(Context context, AttributeSet attrs) {
        super(context);
}

这是正确的

public CameraSurfaceView(Context context, AttributeSet attrs) {
        super(context,attrs);
}

其他回答

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

它崩溃了,因为我的活动id中的一个字段与其他活动的id匹配。我通过给出一个唯一的id来修复它。

在我的loginActivity.xml密码字段id是“password”。在我的注册活动中,我只是通过给出id r_password来修复它,然后它返回非空对象:

password = (EditText)findViewById(R.id.r_password);

对我来说,同一个活动有两个xml布局——一个竖屏模式,一个横屏模式。当然,我已经在landscape xml中更改了一个对象的id,但忘记在portrait版本中做同样的更改。如果您更改了一个xml,请确保对另一个xml也执行相同的操作,否则在运行/调试它之前不会得到错误,并且它无法找到未更改的id。哦,愚蠢的错误,你为什么要这样惩罚我?

对于那些使用ExpandableListView并根据它的标题遇到这个问题的人来说,这是一个答案。

我有这个错误试图与TextViews在我的孩子和组视图作为一个expandabelistview实现的一部分。

您可以在getChildView()和getGroupView()方法的实现中使用如下内容。

        if (convertView == null) {
            LayoutInflater inflater =  (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.child_layout, null);
        }

我在这里找到了这个。

返回null

可能是因为你说得太早了。等待到onfinishinflation()。下面是一个示例项目,演示了一个自定义视图访问其内容。