Android 扩展LinearLayout中具有标头的ExpandableListView:NullPointerException

Android 扩展LinearLayout中具有标头的ExpandableListView:NullPointerException,android,nullpointerexception,expandablelistview,Android,Nullpointerexception,Expandablelistview,我通过扩展LinearLayout定义了一个简单的自定义视图。关键在于它包含一个ExpandableListView,并添加了一个标题视图: public class MyView extends LinearLayout { private View mHeaderView; private ExpandableListView mListView; @SuppressWarnings("UnusedDeclaration") public MyView(Context

我通过扩展
LinearLayout
定义了一个简单的自定义
视图。关键在于它包含一个
ExpandableListView
,并添加了一个标题视图:

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);
}
}
下面是
merge\u 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>
其中
activity\u myview.xml
仅包含我的自定义视图:

<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"/>
我想这是一个在Android2.3.7和4.1.1之间修复的bug,因为我只在使用前者进行测试时才得到错误。不过,我在图书馆里找不到。那么为什么会出现这种错误呢?还有什么解决办法吗


编辑 我想这是2013年6月修复的错误。

替换

mListView = (ExpandableListView) (findViewById(R.id.lv_expandable));

尝试不使用合并

private void init(Context context) {
    setOrientation(LinearLayout.VERTICAL);
    setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
    LayoutInflater inflater = LayoutInflater.from(context);

    inflater.inflate(R.layout.expandablelistview, this, true);
    inflater.inflate(R.layout.textview, this, true);
    mHeaderView = inflater.inflate(R.layout.header_bundles_list, null);
}
expandablelistview.xml

<ExpandableListView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/lv_expandable"
    android:layout_width="match_parent"
    android:background="#ffffd467"
    android:layout_weight="2"
    android:layout_height="0dp"/>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/textview"
    android:text="HELLO"
    android:background="#AA4242"
    android:layout_width="match_parent"
    android:layout_weight="1"
    android:layout_height="0dp"/>
示例
onCreate()

创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u myview);
MyView MyView=(MyView)findviewbyd(R.id.MyView);
//设置适配器
int-groupnum=2;
int childnum=3;
List groupData=new ArrayList(groupnum);
List childData=newarraylist(groupnum);

对于(int i=0;iUh),如果
mHeaderView
ExpandableListView
的标题,那么这就没有意义了。您确定吗?
mListView = (ExpandableListView) (mHeaderView.findViewById(R.id.lv_expandable));
private void init(Context context) {
    setOrientation(LinearLayout.VERTICAL);
    setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
    LayoutInflater inflater = LayoutInflater.from(context);

    inflater.inflate(R.layout.expandablelistview, this, true);
    inflater.inflate(R.layout.textview, this, true);
    mHeaderView = inflater.inflate(R.layout.header_bundles_list, null);
}
<ExpandableListView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/lv_expandable"
    android:layout_width="match_parent"
    android:background="#ffffd467"
    android:layout_weight="2"
    android:layout_height="0dp"/>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/textview"
    android:text="HELLO"
    android:background="#AA4242"
    android:layout_width="match_parent"
    android:layout_weight="1"
    android:layout_height="0dp"/>
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_myview);
    MyView myView = (MyView) findViewById(R.id.myview);

    //Set up adapter
    int groupnum=2;
    int childnum=3;
    List<Map<String, String>> groupData=new ArrayList<Map<String, String>>(groupnum);
    List<List<Map<String,String>>> childData=new ArrayList<List<Map<String, String>>>(groupnum);
    for(int i=0;i<groupnum;i++){
        HashMap<String, String> map = new HashMap<String, String>(1);
        map.put("title","I'm a group");
        groupData.add(map);

        List<Map<String, String>> thisChildData = new ArrayList<Map<String, String>>(childnum);
        for(int j=0;j<childnum;j++){
        HashMap<String, String> childmap = new HashMap<String, String>(1);
            childmap .put("title","I'm a child");
            thisChildData.add(childmap );
        }
        childData.add(thisChildData);
    }

    ExpandableListAdapter adapter=new SimpleExpandableListAdapter(
            this,
            groupData,
            android.R.layout.simple_expandable_list_item_1,
            new String[] {"title"},
            new int[] {android.R.id.text1},
            childData,
            android.R.layout.simple_list_item_1,
            new String[] {"title"},
            new int[] {android.R.id.text1}
    );

    //Set adapter
    myView.mListView.setAdapter(adapter);
}