Reputation: 135
I want to open a dialog for user to choose from multiple items but I get an error when setAdapter()
was going to execute...
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Dialog cd = new Dialog(Main1.this);
String[] mobileArray = {"Android","IPhone","WindowsMobile","Blackberry","WebOS","Ubuntu","Windows7","Max OS X"};
ArrayAdapter adapter = new ArrayAdapter<String>(cd.getContext(), R.layout.lvlayout, mobileArray);
ListView listView = (ListView) findViewById(R.id.listviewID);
listView.setAdapter(adapter);
cd.setContentView(R.layout.dialogLayout);
cd.setTitle("MEOW");
cd.show();
}
});
ListView
is in Dialog layout. whats Wrong here?
Upvotes: 0
Views: 63
Reputation: 24211
You have some basic mistakes here.
The major mistake is calling the cd.setContentView(R.layout.dialogLayout);
at the end of your code. You need to call before you want to find something in that content layout.
And the other mistake is trying to find the list. You need to get a view to find the list in that view. This might be something like this.
ListView listView = (ListView) cd.getView().findViewById(R.id.listviewID);
However, I do prefer the following solution for your problem. This might be an easier way for what you're trying to achieve.
Here's a nice example of implementing a custom dialogue. You can have a look here. https://github.com/afollestad/material-dialogs
So, now as you're getting NullPointerException
, findViewById
is not returning any reference of your ListView
I guess. So here by using the library mentioned above you can achieve this quite easily.
// Initialize your dialogue
MaterialDialog dialog = new MaterialDialog.Builder(getActivity())
.title("MEOW")
.customView(R.layout.dialogLayout, true)
.positiveText(R.string.ok)
.negativeText(R.string.cancel)
.onNegative(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
dialog.dismiss();
}
})
.show();
// Now get the view of that dialogue.
View view = dialog.getCustomView();
// Initialize the array adapter
String[] mobileArray = {"Android","IPhone","WindowsMobile","Blackberry","WebOS","Ubuntu","Windows7","Max OS X"};
ArrayAdapter adapter = new ArrayAdapter<String>(cd.getContext(), R.layout.lvlayout, mobileArray);
// Get the list view and set the adapter
ListView listView = (ListView) view.findViewById(R.id.listviewID);
listView.setAdapter(adapter);
Upvotes: 0
Reputation: 5011
Just initialize your listview outside of onClick listener. Initializing it in onCreate() is more preferable.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
// initialize
ListView listView = (ListView) findViewById(R.id.listviewID);
}
Upvotes: 0
Reputation: 634
The problem is that your variable listView
null is when you call setAdapter()
on it. So the call to findViewById()
returns null. Maybe you just used a wrong ID.
Upvotes: 0
Reputation: 8562
Simply there are two mistakes in your code, you are calling this line without prefixing it with cd.
so that you have to do it like this,
ListView listView = (ListView) cd.findViewById(R.id.listviewID);
Another mistake you are doing is calling findViewById
before calling setContentView()
, This may also raise NPE.
So I suggest you to move this up and re-arrange like this
cd.setContentView(R.layout.dialogLayout);
ListView listView = (ListView) cd.findViewById(R.id.listviewID);
Upvotes: 1