I have defined a simple custom View by extending LinearLayout. The crux is that it contains an ExpandableListView with a header view added:
public class MyView extends LinearLayout {
private View mHeaderView;
private ExpandableListView mListView;
@SuppressWarnings("UnusedDeclaration")
public MyView(Context context) {
super(context);
init(context);
}
@SuppressWarnings("UnusedDeclaration")
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
private void init(Context context) {
setOrientation(LinearLayout.VERTICAL);
setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
LayoutInflater inflater = LayoutInflater.from(context);
//Inflate custom view into LinearLayout
inflater.inflate(
R.layout.merge_myview, this);
//Inflate header with no parent so we can set it as the ListView header in onFinishInflate()
mHeaderView = inflater.inflate(R.layout.header_bundles_list, null);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
mListView = (ExpandableListView) (findViewById(R.id.lv_expandable));
Log.d("MyView", "Almost done...");
//Commenting out this line makes it work
mListView.addHeaderView(mHeaderView, null, false);
}
}
Here's merge_myview.xml:
<merge xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ExpandableListView
android:id="@+id/lv_expandable"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</merge>
Here's the header:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#EFEEFF"
android:clickable="true"
android:gravity="center"
android:minHeight="96dp"
android:onClick="openPane"
android:paddingBottom="24dp"
android:paddingTop="24dp"
android:text="HELLO!"
android:textColor="#333333"
android:textIsSelectable="true"
android:textSize="48sp" />
</RelativeLayout>
With a plain onCreate in my activity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_myview);
}
Where activity_myview.xml contains only my custom view:
<com.example.app.MyView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:orientation="vertical"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"/>
However, when I run this app, I get the following error:
java.lang.NullPointerException at
android.widget.ListView.clearRecycledState(ListView.java:507) at
android.widget.ListView.resetList(ListView.java:493) at
android.widget.ListView.layoutChildren(ListView.java:1431) at
android.widget.AbsListView.onLayout(AbsListView.java:1147) at
android.view.View.layout(View.java:7035) at
android.widget.LinearLayout.setChildFrame(LinearLayout.java:1252) at
android.widget.LinearLayout.layoutVertical(LinearLayout.java:1128) at
android.widget.LinearLayout.onLayout(LinearLayout.java:1045) at
android.view.View.layout(View.java:7035) at
android.widget.FrameLayout.onLayout(FrameLayout.java:333) at
android.view.View.layout(View.java:7035) at
android.widget.LinearLayout.setChildFrame(LinearLayout.java:1252) at
android.widget.LinearLayout.layoutVertical(LinearLayout.java:1128) at
android.widget.LinearLayout.onLayout(LinearLayout.java:1045) at
android.view.View.layout(View.java:7035) at
android.widget.LinearLayout.setChildFrame(LinearLayout.java:1252) at
android.widget.LinearLayout.layoutVertical(LinearLayout.java:1128) at
android.widget.LinearLayout.onLayout(LinearLayout.java:1045) at
android.view.View.layout(View.java:7035)
I suppose this is a bug that was fixed somewhere between Android 2.3.7 and 4.1.1, because I only get the error when testing with the former. I can't find it in the bug tracker, though. So why does this error occur? And is there any workaround?
EDIT
I think this is the bug, fixed in June 2013.