Robert
Robert

Reputation: 4830

Listview and dialog in android

I have a small custom dialog in wich I have a ListView with id:list. I want to populate it with strings I have in my resources (R.array.tones), but I have really big problems making this work, have tried many different solutions this is the latest which I think would work but it throws an null pointer exception on the toneList.

    Dialog dialog = new Dialog(this);

    dialog.setContentView(R.layout.tone_dialog);
    dialog.setTitle(R.string.tonePromptTitle);
    ListView toneList = (ListView)findViewById(R.id.list);

    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.tones, android.R.layout.simple_list_item_1);

    toneList.setAdapter(adapter);

    dialog.show();

My class is just extending Activity not ListActivity, and I would like to have it that way otherwise I must create a new class just for the listview. I apologize for the long code, I'm new here and hasn't really figured out all functionality yet.

Upvotes: 0

Views: 1990

Answers (2)

DecodeGnome
DecodeGnome

Reputation: 1809

You should look for the View inside of the dialog:

 ListView toneList = (ListView)dialog.findViewById(R.id.list);

:)

Upvotes: 5

Mark Allison
Mark Allison

Reputation: 21909

You are not searching your dialog layout for toneList. Try the following instead:

ListView toneList = (ListView)dialog.findViewById(R.id.list);

Upvotes: 0

Related Questions