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!
I have met with such a bug.