Oliver G Hjermitslev
Oliver G Hjermitslev

Reputation: 129

DialogBuilder FATAL EXCEPTION on builder.show()

LogCat is telling me that my Application is unable to display a window, since "token null is not for an application". This is the code it's having problems with:

                AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
            builder.setMessage("Er du sikker?\n" + taskList.get(position) +"\nvil starte med det samme hvis du trykker 'Ja'");
                    .setPositiveButton("Ja", dialogClickListener);
                    .setNegativeButton("Nej", dialogClickListener);
                    .show();

Ignore the Danish, that's just for our users. It's on .show() it starts acting up and throws FATAL. I looked this same issue up all over StackOverflow and found that it's likely a problem in getApplicationContext(), but I've been unable to figure out how to get the context of use otherwise.

I'll leave some of the code it's supposed to be used on here in case you need it.

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { //give our listView an onItemClickListener
        @Override
        public void onItemClick(AdapterView<?> parent, View view, final int position, long id) { //when you click on an item
            DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() { //it should open a dialog but doesn't
                @Override
                public void onClick(DialogInterface dialog, int which) { //if you click
                    switch (which){ //"ja"
                        case DialogInterface.BUTTON_POSITIVE:

                            Intent i = new Intent(getApplicationContext(), PlayScreen.class); //start a new intent to open PlayScreen
                            i.putExtra("task", taskList.get(position)); //with the task out of the ArrayList that was clicked
                            startActivity(i);

                            break;

                        case DialogInterface.BUTTON_NEGATIVE: //nothing

                            break;
                    }
                }
            };

Basically I think the problem is I don't know how to provide builder with the context it needs. It may have something to do with me being new to Android development.

Upvotes: 0

Views: 58

Answers (2)

Oliver G Hjermitslev
Oliver G Hjermitslev

Reputation: 129

I am an idiot.

Basically I forgot I was using it in the context of a ListView, meaning I was attempting to use it with an activity. I got the context from the listview..

lv.getContext()

Sorry for wasting your time, if only I had spent 5 more minutes figuring out my position in the code...

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1006664

Replace getApplicationContext() with this, assuming that this is an Activity. Activity inherits from Context, and you need to use an Activity to show a Dialog.

Upvotes: 1

Related Questions