Android 如何在使用特定的XML布局时扩展视图?

Android 如何在使用特定的XML布局时扩展视图?,android,layout,view,Android,Layout,View,我想扩展视图类,以便在其中存储自定义对象。 我的自定义视图应该是膨胀特定布局的结果 现在我正在使用这个代码 public EpisodeView(Context context, final ViewGroup rootView, final CustomObject object) { super(activity); this.object = object; this.context = context; View.i

我想扩展视图类,以便在其中存储自定义对象。 我的自定义视图应该是膨胀特定布局的结果

现在我正在使用这个代码

    public EpisodeView(Context context, final ViewGroup rootView, final CustomObject object) {
        super(activity);
        this.object = object;
        this.context = context;
        View.inflate(context, R.layout.episode_view, rootView);
        initControls();
        setDisplay();       
    }

    protected void initControls() {
        this.textView = (TextView)findViewById(R.id.customTextView);
...
}
我的问题是findViewById始终返回null。问题不是来自我的布局xml。 更重要的是,我不知道如何使用设置自定义视图。您有以下错误:

this.textView = (TextView)findViewById(R.id.customTextView);
您应该将其更改为:

this.textView = (TextView)view.findViewById(R.id.customTextView);

希望这有帮助

我的代码有一个错误,我刚刚更新了它。当然我可以添加视图成员,但我不明白为什么这个.findViewById(…)不起作用