1

I want to populate a listview with text taken from (string.xml) and images. I did so but I get NPE Can you help me? thanks

public class Menu_activity extends ListActivity {
int[] img = { R.drawable.01, R.drawable.1, R.drawable.2,
        R.drawable.3, R.drawable.4,
        R.drawable.5};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getListView().setDividerHeight(1);
    getListView().setAdapter(new BindDataAdapter(this, img, men));

}
private String[] men = getResources().getStringArray(R.array.men);

1 Answer 1

2

Change to

private String[] men;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    men = getResources().getStringArray(R.array.men); // inside onCreate
    getListView().setDividerHeight(1);
    getListView().setAdapter(new BindDataAdapter(this, img, men));     
}
Sign up to request clarification or add additional context in comments.

5 Comments

Ah, good catch! I didn't notice it was outside of onCreate(). Do you need to use setContentView() before calling getListView()? I know its an Android resource but I was thinking it still needed to be set first...
CONFIRM, IT WORKS WELL
@codeMagic the docs does not say anything about it developer.android.com/reference/android/app/ListActivity.html. So it should work fine
Ah, yes, "ListActivity has a default layout..." I knew it had a default ListView but I guess I didn't realize it was in a default layout. Just checking...thanks
@codeMagic yes if he had custom lauout then op should have a listview with id android:id="@android:id/list" and setContentView(R.layout.mylayout)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.