5

I'm getting a null pointer exception in my custom view (which is derived from a LinearLayout) because it can't find its child views. Here is the code:

public class MyView extends LinearLayout
{
    public MyView(Context context, AttributeSet attrs)
    {
        this(context, attrs, 0);
    }

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

    private TextView mText;

    @Override
    protected void onFinishInflate()
    {
        super.onFinishInflate();
        mText = (TextView) findViewById(R.id.text);

        if (isInEditMode())
        {
            mText.setText("Some example text.");
        }
    }
}

Here is the layout (my_view.xml):

<?xml version="1.0" encoding="utf-8"?>
<com.example.views.MyView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/text"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:ellipsize="end"
        android:maxLines="4"
        android:paddingLeft="8dp"
        android:paddingRight="8dp"
        android:text="Some text" />

</com.example.views.MyView>

And here is how I put it in the XML file:

    <com.example.views.MyView
        android:id="@+id/my_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

But when I try to preview it in the layout editor I get an NPE on mText.setText(...) because getViewById() returns null.

What's going on?

Clarification

The reason I expect this to work, is if I do

MyView v = (MyView)inflater.inflate(R.layout.my_view);
((TextView)v.findViewById(R.id.text)).setText("Foo");

everything works fine. Is that not what the layout inflater does when it goes through a layout file? In any case, how can I handle both situations correctly (without getting pointless nested views)?

Timmmm
  • 88,195
  • 71
  • 364
  • 509

1 Answers1

4

In you XML file you are trying to use a custom view class (com.example.views.MyView) and in the same time trying to add a TextView inside. It's not possible.

Here is what you need to change:

You must inflate XML file in the code:

public MyView(Context context, AttributeSet attrs, int defStyle)
{
    super(context, attrs, defStyle);
    LayoutInflater.from(context).inflate(R.layout.<your_layout>.xml, this);
}

And modify the XML layout file like this:

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

<TextView
    android:id="@+id/text"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:gravity="center"
    android:ellipsize="end"
    android:maxLines="4"
    android:paddingLeft="8dp"
    android:paddingRight="8dp"
    android:text="Some text" />

</LinearLayout>
sdabet
  • 18,360
  • 11
  • 89
  • 158
  • But if I do `myview = (MyView)inflater.inflate(R.layout.my_view, null, false)` in some other code it works fine. Maybe it is a bug in the layout editor (wouldn't be the first!). Plus you should use a `` if you are going to do it the way you have written. – Timmmm Oct 29 '12 at 15:10
  • Of course it works. It's just that whatever you put inside the `` XML element will be ignored. That's why the TextView cannot be found. – sdabet Oct 29 '12 at 15:13
  • No I mean if I then do `myview.findViewById(R.id.text)` that finds the view fine. It seems like the layout inflater behaves differently if it is inflating the custom component directly to when it is inflating the custom component *inside* another layout. I'll update the question to clarify. – Timmmm Oct 29 '12 at 15:18
  • Are you sure you're supposed to have the `.xml`? – Nic Dec 25 '17 at 06:25