Yaobin Then
Yaobin Then

Reputation: 2832

findViewById returns null in custom view

Ok, I've read around that a custom view must have a constructor with AttributeSet in order for findViewById() to work. I've not seen any actual implementation yet.

Below are my codes: My custom view that extends HorizontalScrollView:

public AnimatedMenuBar(Context context) {
        // TODO Auto-generated constructor stub
        this(context, null);
    }

    public AnimatedMenuBar(Context context, AttributeSet attrs) {
        // TODO Auto-generated constructor stub
        this(context, attrs, 0);
    }

    public AnimatedMenuBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
        this.context = context;
        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.main, this);
        this.setHorizontalScrollBarEnabled(false);
    }

From my main activity:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.testing);
        AnimatedMenuBar animatedMenuBar = (AnimatedMenuBar)findViewById(R.id.animatedMenuBar);
}

testing.xml

<?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="match_parent">
    <com.pt.task.custommenubar.AnimatedMenuBar
        android:id="@+id/animatedMenuBar" android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

main.xml

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="wrap_content" >
    <LinearLayout android:id="@+id/horizontalLayout" android:orientation="horizontal"
        android:layout_width="fill_parent" android:layout_height="wrap_content">
        <TextView android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:id="@+id/dummyTextView" android:padding="5dip"/>
    </LinearLayout>
</merge>

animatedMenuBar is always null, is there any thing that I missed?

Thank you very much!

Upvotes: 0

Views: 3842

Answers (2)

Tek Yin
Tek Yin

Reputation: 3050

Btw, this may not be suitable to ask here, could you explain to me how the view is able to be drawn as I simply called inflate() inside the constructor, without returning anything. – Alvin Then

When you call this inflater.inflate(R.layout.main, this), The inflater will inflate main.xml and put the inflated View into the ViewGroup in your custom view.

It has same effect with setContentView(R.layout.main) in Activity

Upvotes: 2

Divers
Divers

Reputation: 9569

I have met with such a bug.

  1. Try Project-clean. Sometimes it helps.
  2. If AnimatedMenuBar insied custommenubar, then write com.pt.task.custommenubar$AnimatedMenuBar
  3. Try to rename AnimatedMenuBar everywhere in code.

Upvotes: 3

Related Questions