我想使用ExpandableListActivity
类,但不想从该类扩展我的类。但是,我想从ActionBarActivity
扩展我的课程并使用ExpandableListActivity
。当我从ExpandableListActivity
扩展课程时,一切都很有效。
我试过这个:
public class GestureAnyWhere extends ActionBarActivity {
ExpandableListActivity ex = new ExpandableListActivity ();
ExpandableListView.OnChildClickListener liste_schritt1_Listener = new ExpandableListView.OnChildClickListener () {
@Override
public boolean onChildClick ( ExpandableListView parent, View v, int groupPosition, int childPosition, long id ) {
Toast.makeText ( getApplicationContext (), " " + childPosition, Toast.LENGTH_SHORT ).show ();
return false;
}
};
...
layoutElementeAnordnen ();
...
private void layoutElementeAnordnen () {
TextView schritt1 = ( TextView ) findViewById ( R.id.schritt1 );
schritt1.setTextSize ( 14 );
schritt1.setTextColor ( getResources ().getColor ( R.color.rot ) );
// Ausklappbares ListView
// ExpandableListView liste_schritt1 = (ExpandableListView)findViewById ( R.id.list);
// funktioniert nicht, da die ExpandableListView keine neue ID bekommen darf
ExpandableListView liste_schritt1 = ex.getExpandableListView ();
但是在ExpandableListView liste_schritt1 = ex.getExpandableListView ();
行上有一个NullpointerException
:
Caused by: java.lang.NullPointerException
at android.app.Activity.setContentView(Activity.java:1939)
at android.app.ExpandableListActivity.ensureList(ExpandableListActivity.java:273)
at android.app.ExpandableListActivity.getExpandableListView(ExpandableListActivity.java:257)
at de.gestureanywhere.GestureAnyWhere.layoutElementeAnordnen(GestureAnyWhere.java:77)
at de.gestureanywhere.GestureAnyWhere.onCreate(GestureAnyWhere.java:61)
布局文件的相应部分:
<ExpandableListView
android:id="@id/android:list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:groupIndicator="@null"
android:transcriptMode="alwaysScroll">
</ExpandableListView>
我认为这是因为我创建了一个新的ExpandableListActivity
对象:ExpandableListActivity ex = new ExpandableListActivity ();
。然后ExpandableListView liste_schritt1 = ex.getExpandableListView ();
为空。
正如我所提到的,当我从ExpandableListActivity
延伸时,一切都很有效。因为我现在从ActionBarActivity
延伸,所以我只是修改了我的代码:
ExpandableListActivity ex = new ExpandableListActivity ();
为新行ExpandableListView liste_schritt1 = ex.getExpandableListView ();
(之前:ExpandableListView liste_schritt1 = getExpandableListView ();
非常感谢您的帮助
答案 0 :(得分:1)
为什么使用ExpandableListActivity
作为课程字段?您只需从主要活动中调用(ExpandableListView) findViewById(android.R.id.list)
即可获取列表,而不是getExpandableListView()
。
答案 1 :(得分:0)
如果是ExpandableListActivity,我们必须有3个布局:
ExpandableListView
视图。你可能错过了上面的一个