我想在生成listview之后在侧边栏菜单中插入expandableListView我试图将它插入expandableListView实例中但是它说nullpointer这里是代码的某些部分
Activity_main_drawer文件的结尾
<ExpandableListView
android:id="@+id/expandablelisto"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
主要活动
ListView modeList = new ListView(this);
String[] stringArray = new String[] { "Bright Mode", "Normal Mode" };
ArrayAdapter<String> modeAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, stringArray);
modeList.setAdapter(modeAdapter);
//here the problem
ExpandableListView expandableListView = (ExpandableListView) findViewById(R.id.expandablelisto);
expandableListView.addView(modeList);
//end
错误
java.lang.NullPointerException
答案 0 :(得分:0)
您必须SimpleExpandableListAdapter
使用ExpandableListView
,例如:
SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(
this,
groupData,
android.R.layout.simple_expandable_list_item_1,
groupFrom,
groupTo,
childData,
android.R.layout.simple_list_item_1,
childFrom,
childTo);
ExpandableListView expandableListView = (ExpandableListView) findViewById(R.id.expandablelisto);
expandableListView.setAdapter(adapter);