Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8173017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:59:01+00:00 2026-06-06T21:59:01+00:00

I want to be able to instantiate a new custom LinearLayout class based on

  • 0

I want to be able to instantiate a new custom LinearLayout class based on a layout defined in xml. No matter what I do, at runtime, the LinearLayout and all of my TextViews throw a null reference exception.

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 Attempt 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 Attempt 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);
    }
}

End goal would be from my main activity be able to do something like this in a button click event:

 mainLayout.AddView(new TransferListRow(this, "serial", "model", "site"));
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T21:59:02+00:00Added an answer on June 6, 2026 at 9:59 pm

    1) Because you are extending LinearLayout, your layout definded in XML does not need to have a LinearLayout wrapper. Instead use just <merge> ... </merge>.

    2) In your constructor you have to call inflator.Inflate(Resource.Layout.transferListRow, this);. Please note the this parameter. This will inflate all elements defined in your XML into the LinearLayout as children.

    3) The framework will notify you in onFinishInflate() when inflating is done and the views can be found with findViewById(). So, in onFinishInflate() you can assign your TextViews to your member variables.

    That’s it 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an EJB which I don't want anyone to be able to instantiate
I want to be able to instantiate any object in my application with this
I have a custom ViewController class and many instances of it, and I want
I want to be able to call Interfaces in my class, but have spring
I am very new to Scala. I want to implement a generic matrix class
This seems like a silly question but I want to be able to instantiate
I want to be able to instantiate a JavaScript object as follows: var c
I have an model with a relation, and I want to instantiate a new
I'm using LaTeX and BibTeX for an article, and I want to able to
Want to be able to provide a search interface for a collection of objects

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.