如何在另一个自定义视图中添加自定义视图?

Eve*_*ett 5 xml android android-layout

我正在尝试将CustomViewBeta(一个扩展的RelativeLayout)添加到CustomViewAlpha(扩展的LinearLayout) - 这个想法是CustomViewAlpha将一堆CustomViewBetas保存为ListView.无论我尝试什么,它都行不通.我没有看到任何东西 - 没有CustomViewBetas,或者当我在CustomViewBeta中的一个TextViews上尝试setText时它给了我一个NPE

CustomViewAlpha工作正常,因为它在Fragment的XML中是硬编码的:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
   ...>
    <com.path.CustomViewAlpha
        android:id="@+id/customviewalpha"
        android:orientation="vertical"
        android:layout_gravity="center_vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)

代码是:

public class CustomViewAlpha extends LinearLayout {
private Context mContext;

public CustomViewAlpha (Context context) {
    super(context);
    this.mContext = context;
}

public CustomViewAlpha (Context context, AttributeSet attrs) {
    super(context, attrs);
    this.mContext = context;
}

public CustomViewAlpha (Context context, AttributeSet attrs, int defStyle){
    super(context, attrs,defStyle);
    this.mContext = context;
}

public void addCustomViewBeta(String someString){
    CustomViewBeta customViewBeta = new CustomViewBeta(mContext);
    addView(customViewBeta);
}
Run Code Online (Sandbox Code Playgroud)

CustomViewBeta在Fragment的XML中没有硬编码,并且以编程方式添加:

公共类CustomViewBeta扩展RelativeLayout {private Context mContext;

private TextView textView;

public CustomViewBeta (Context context) {
    super(context);
    this.mContext = context;
    init();
}

public CustomViewBeta (Context context, AttributeSet attrs) {
    super(context, attrs);
    this.mContext = context;
    init();
}

public CustomViewBeta (Context context, AttributeSet attrs, int defStyle){
    super(context, attrs,defStyle);
    this.mContext = context;
    init();
}


private void init() {
    LayoutInflater.from(mContext).inflate(R.layout.customviewbeta, null, false);
    textView = (TextView) findViewById(R.id.tripleexpandablelistview_category_name);
    textView.setText("ASDASDASDAD");
}
Run Code Online (Sandbox Code Playgroud)

这个XML是:

<?xml version="1.0" encoding="utf-8"?>
<com.path.CustomViewBeta
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

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

</com.path.CustomViewBeta>
Run Code Online (Sandbox Code Playgroud)

我经常在"textView.setText("ASDASDASDAD")上遇到NPE;" line,因为TextView为null.有什么我想念的吗?我是否应该尝试为CustomViewBeta扩充XML并以编程方式执行(以编程方式逐个添加文本视图?)?谢谢.

Bla*_*elt 3

你的初始化是错误的

private void init() {
    LayoutInflater.from(mContext).inflate(R.layout.customviewbeta, null, false);
    textView = (TextView) findViewById(R.id.tripleexpandablelistview_category_name);
    textView.setText("ASDASDASDAD");
}
Run Code Online (Sandbox Code Playgroud)

最后一个参数必须是true添加TextView到RelativeLayout。另外ViewGroup,有一个static inflate方法,所以你可以避免 LayoutInflater.from(mContext),你可以直接调用inflate(mContext, R.layout.customviewbeta, this);