Skip to content Skip to sidebar Skip to footer

Android - GetListView And SetListAdapter Error With NullPointerException When Using Custom Layout

I have a layout like this Copy

so the users variable is most likely null.


Solution 3:

Set a breakpoint right after the line ListView lv = getListView();

Chances are this is giving you a null value.

Honestly I would try to go with a regular activity and just configure the ListView manually. It isn't hard and it seems like a better way to support having two lists in one Activity.


Solution 4:

I changed the listview to explicitly give it an id like this

 <ListView android:id="@+id/list_view"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:layout_weight="1"
     android:drawSelectorOnTop="false"/>

And then I changed my code to this

private void buildListView()
{
    ListView lv = (ListView)findViewById(R.id.list_view);

    lv.setTextFilterEnabled(true);

    lv.clearChoices();

    lv.setAdapter(new UserListAdapter(SuspendedUsersActivity.this, R.layout.useritem, users));

    lv.setOnItemClickListener(clickListener);
}

And now it works. For some reason that default ID for the listview doesn't work.


Solution 5:

your must extends MainActivity to ListActivity.

public class MainActivity extends ListActivity {
}

Post a Comment for "Android - GetListView And SetListAdapter Error With NullPointerException When Using Custom Layout"