扩展LinearLayout空引用

时间:2012-07-02 15:27:35

标签: android xamarin.android

我希望能够基于xml中定义的布局来实例化一个新的自定义LinearLayout类。无论我做什么,在运行时,LinearLayout和我的所有TextView都抛出一个空引用异常。

transferListRow.axml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:padding="5dp">
<TextView
    android:id="@+id/transferSerial"
    android:layout_width="0dp"
    android:layout_weight=".30"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:padding="5dp"
    android:gravity="center" />
<TextView
    android:id="@+id/transferModel"
    android:layout_width="0dp"
    android:layout_weight=".30"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:padding="5dp"
    android:gravity="center" />
<TextView
    android:id="@+id/transferSite"
    android:layout_width="0dp"
    android:layout_weight=".30"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:padding="5dp"
    android:gravity="center" />
<Button
    android:id="@+id/transferDelete"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:padding="5dp"
    android:gravity="center"
    android:src="@drawable/delete" />

TransferListRow.cs尝试1:

 sealed class TransferListRow : LinearLayout
 {
    private readonly Context _context;

    public TransferListRow(Context context, string serial, string model, string site)
        :base(context)
    {
        _context = context;

        LayoutInflator inflator = (LayoutInflator) _context.GetSystemService(Context.LayoutInflaterService);
        inflator.Inflate(Resource.Layout.transferListRow, null);

        TextView s = (TextView) FindViewById(Resource.Id.transferSerial);
        s.Text = serial;

        TextView m = (TextView) FindViewById(Resource.Id.transferModel);
        m.Text = model;

        TextView st = (TextView) FindViewById(Resource.Id.transferSite);
        st.Text = site;
    }
}

TransferListRow.cs尝试2:

sealed class TransferListRow : LinearLayout
{
    private readonly Context _context;

    public TransferListRow(Context context, string serial, string model, string site)
        :base(context)
    {
        _context = context;

        LinearLayout layout = (LinearLayout) FindViewById(Resource.Layout.transferListRow);

        TextView s = (TextView) layout.FindViewById(Resource.Id.transferSerial);
        s.Text = serial;

        TextView m = (TextView) layout.FindViewById(Resource.Id.transferModel);
        m.Text = model;

        TextView st = (TextView) layout.FindViewById(Resource.Id.transferSite);
        st.Text = site;

        AddView(s);
        AddView(m);
        AddView(st);
    }
}

最终目标是来自我的主要活动,可以在按钮点击事件中执行类似的操作:

 mainLayout.AddView(new TransferListRow(this, "serial", "model", "site"));

1 个答案:

答案 0 :(得分:4)

1)因为您正在扩展LinearLayout,所以在XML中定义的布局不需要具有LinearLayout包装器。而只使用<merge> ... </merge>

2)在你的构造函数中,你必须调用inflator.Inflate(Resource.Layout.transferListRow, this);。请注意参数。这会将XML中定义的所有元素作为子元素扩展到LinearLayout。

3)当完成充气并且onFinishInflate()可以找到视图时,框架会在findViewById()通知您。因此,在onFinishInflate()中,您可以将TextView分配给成员变量。

就是这样:))