Java 在Android中膨胀视图后,自定义视图子项为空

Java 在Android中膨胀视图后,自定义视图子项为空,java,android,android-custom-view,layout-inflater,Java,Android,Android Custom View,Layout Inflater,我正在开发一款Android应用程序。我有一个自定义视图和布局,如下所示: <com.hello.view.card.inner.SimpleCardView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/card_simple_linear_layout" android:layout_width="match_parent" andr

我正在开发一款Android应用程序。我有一个自定义视图和布局,如下所示:

<com.hello.view.card.inner.SimpleCardView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/card_simple_linear_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/simple_label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

</com.hello.view.card.inner.SimpleCardView>
这就是我如何夸大视图(我尝试了以下两个调用):


我遇到的问题是,当调用view.setCard(card)时,我看到textView为null,尽管我希望在init(..)方法中设置它。有人能告诉我什么地方设置不正确吗?提前感谢。

而不是使用根元素

com.hello.view.card.inner.SimpleCardView
试用

<merge xmlns:android="http://schemas.android.com/apk/res/android">
    <TextView
        android:id="@+id/simple_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</merge>
当您在其他布局中使用该视图时,您将希望将其放置在其中

<com.hello.view.card.inner.SimpleCardView />


以及它需要的任何属性、id、宽度/高度等。

谢谢您的回答。事实证明,不应该在构造函数中调用init(上下文)。调用它的正确位置是onFinishInflate()。以下更改有助于修复此问题:

public class SimpleCardView extends LinearLayout {
    protected SimpleCard card = null;
    protected TextView textView;

    public SimpleCardView(Context context) {
        super(context);     
    }

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

    public SimpleCardView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        init(getContext());
    }

    protected void init(Context context) {
        textView = (TextView)findViewById(R.id.simple_label);
    }

    public void setCard(SimpleCard card) {
        this.card = card;
        textView.setText(card.getMessage());        
    }
}

请显示活动的代码这正是我想要的。需要在谷歌搜索结果中排名靠前。
LayoutInflater.from(context).inflate(R.layout.card_simple, this);
setOrientation(LinearLayout.VERTICAL);
textView = (TextView)findViewById(R.id.simple_label);
<com.hello.view.card.inner.SimpleCardView />
public class SimpleCardView extends LinearLayout {
    protected SimpleCard card = null;
    protected TextView textView;

    public SimpleCardView(Context context) {
        super(context);     
    }

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

    public SimpleCardView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        init(getContext());
    }

    protected void init(Context context) {
        textView = (TextView)findViewById(R.id.simple_label);
    }

    public void setCard(SimpleCard card) {
        this.card = card;
        textView.setText(card.getMessage());        
    }
}