Cian
Cian

Reputation: 105

Java, Calling an AlertDialog from multiple classes

I want to use the same AlertDialog in multiple classes. Here is the function I made to display a AlertDialog:

public void incorrectFields()
    {
        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(mActivity);
        String emptyFields = "Empty Field(s)";
        String emptyFieldsMessage = "Please ensure all fields are contain data";

        dialogBuilder.setTitle(emptyFields);
        dialogBuilder.setMessage(emptyFieldsMessage);
        dialogBuilder.setNegativeButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        dialogBuilder.setPositiveButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                // continue with delete
            }
        });

        dialogBuilder.create();
        dialogBuilder.show();
    }

It works when I am calling it in the same class it's initialized in. But when I call it from another class I get the error:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference

How can I call the AlertDialog successfully? Keep in mind I cannot extend the class which contains the code for the AlertDialog because I am already extending a different class.

Upvotes: 0

Views: 575

Answers (3)

Debosmit Ray
Debosmit Ray

Reputation: 5403

This is something I frequently do in practice. Probably not the very best way, but I haven't found it to be doing something ludicrous as yet. I'll update this if I do. :)

/**
 * Creates a dialog and shows it
 *
 * @param exception
 *            The exception to show in the dialog
 * @param title
 *            The dialog title
 */
private void createAndShowDialogFromTask(final Exception exception, String title) {
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            createAndShowDialog(exception, "Error");
        }
    });
}


/**
 * Creates a dialog and shows it
 *
 * @param exception
 *            The exception to show in the dialog
 * @param title
 *            The dialog title
 */
private void createAndShowDialog(Exception exception, String title) {
    Throwable ex = exception;
    if (exception.getCause() != null) {
        ex = exception.getCause();
    }
    createAndShowDialog(ex.getMessage(), title);
}

/**
 * Creates a dialog and shows it
 *
 * @param message
 *            The dialog message
 * @param title
 *            The dialog title
 */
private void createAndShowDialog(final String message, final String title) {
    final AlertDialog.Builder builder = new AlertDialog.Builder(this);

    builder.setMessage(message);
    builder.setTitle(title);
    builder.create().show();
}

Even if you have the previous functions in some activity and you find yourself to be using them frequently, change them to be static and then pass the activity as a WeakReference(very important) to wherever you need to call them from, and then access them.

Alternatively, you can make a static utility class containing these methods, and then invoke them.

Upvotes: 0

Kevin Crain
Kevin Crain

Reputation: 1935

Change method declaration as follows

public static void incorrectFields(Context context)

Change line in method to

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);

Also I would suggest moving the method into some util class

Calling method in activity

SomeUtilClass.incorrectFields(this)

Calling method in fragment

SomeUtilClass.incorrectFields(getContext())

Upvotes: 1

Cas
Cas

Reputation: 356

You should get the context by argument:

public void incorrectFields(Context context)

{
    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
    String emptyFields = "Empty Field(s)";
    String emptyFieldsMessage = "Please ensure all fields are contain data";

    dialogBuilder.setTitle(emptyFields);
    dialogBuilder.setMessage(emptyFieldsMessage);
    dialogBuilder.setNegativeButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

        }
    });
    dialogBuilder.setPositiveButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            // continue with delete
        }
    });

    dialogBuilder.create();
    dialogBuilder.show();
}

Then you have the context from whichever activity you are calling from. ;)

Upvotes: 1

Related Questions