Vik0809
Vik0809

Reputation: 379

Nullpointerexception using ExpandableListActivity

I want to use the ExpandableListActivity class but don't want to extend my class from that class. However I want to extend my class from ActionBarActivity and use ExpandableListActivity. When I extend my class from ExpandableListActivityeverything works great.

I tried it with this:

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 ();

But on the line ExpandableListView liste_schritt1 = ex.getExpandableListView (); there is an 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)

The corresponding part of the layout file:

<ExpandableListView
    android:id="@id/android:list"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:groupIndicator="@null"
    android:transcriptMode="alwaysScroll">
</ExpandableListView>

I think it's because I create a new ExpandableListActivity object with this: ExpandableListActivity ex = new ExpandableListActivity ();. And then ExpandableListView liste_schritt1 = ex.getExpandableListView ();is null.

As I mentioned everything works great when I extend from ExpandableListActivity. Because I extend now from ActionBarActivity I just modified my code with these points:

Thanks a lot for helping

Upvotes: 0

Views: 41

Answers (2)

Ircover
Ircover

Reputation: 2446

Why are you using ExpandableListActivity as class field? You can just call (ExpandableListView) findViewById(android.R.id.list) from your main Activity to get list instead of getExpandableListView().

Upvotes: 1

Sai Phani
Sai Phani

Reputation: 462

In case of ExpandableListActivity we must have 3 layouts:

  • First one containing the main ExpandableListView view.
  • Second one for the groups.
  • Third one for the children (detail rows).

you possibly missing one of the above

Upvotes: 0

Related Questions