Java,从多个 类 调用 AlertDialog

Java, Calling an AlertDialog from multiple classes

我想在多个 class 中使用同一个 AlertDialog。这是我用来显示 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();
    }

当我在同一个 class 中调用它时,它在初始化时工作。但是当我从另一个 class 调用它时,我得到错误:

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

如何才能成功调用AlertDialog?请记住,我无法扩展包含 AlertDialog 代码的 class,因为我已经扩展了一个不同的 class.

您应该通过参数获取上下文:

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();
}

那么您就可以从 activity 调用的任何地方获得上下文。 ;)

修改方法声明如下

public static void incorrectFields(Context context)

将方法中的行更改为

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

另外,我建议将该方法移动到一些实用程序中 class

activity中的调用方法

SomeUtilClass.incorrectFields(this)

片段中调用方法

SomeUtilClass.incorrectFields(getContext())

这是我在实践中经常做的事情。可能不是最好的方法,但我还没有发现它在做一些荒谬的事情。如果我这样做,我会更新这个。 :)

/**
 * 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();
}

即使您在某些 activity 中有以前的功能并且您发现自己经常使用它们,将它们更改为 static 然后将 activity 作为 WeakReference(非常重要)到任何你需要调用它们的地方,然后访问它们。

或者,您可以创建一个包含这些方法的 static utility class,然后调用它们。