javiersantos / MaterialStyledDialogs

A library that shows a beautiful and customizable Material-based dialog with header. API 14+ required.
Apache License 2.0
1.17k stars 155 forks source link

[Help] [Question] How to Reuse #39

Closed CelticParser closed 7 years ago

CelticParser commented 7 years ago

How can I reuse the dialog's in a separate class?

With my attempt, I get a NullPointerException:

Attempt to invoke virtual method 'android.content.res.Resources$Theme android.content.Context.getTheme()' on a null object reference." at FailDialogs.java:24

I just started diving back into Android/Java after a 5yr layoff so your help will be greatly appreciaded.

public class FailDialogs {

    static Context context;

    public static void invalidPasswordEmail() {

        new MaterialStyledDialog.Builder(context) // <- This would be line 24
                .setHeaderDrawable(R.drawable.shot_down)
                .withDialogAnimation(true)
                .setTitle(context.getString(R.string.dialog_warning))
                .setDescription(context.getString(R.string.login_description_failed))
                .setPositiveText(context.getString(R.string.login_positive_text_failed))
                .onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        /**
                         * Send the user to the Signup Activity If they
                         * realize they had not signed up yet or needs to
                         * create a new account via the action button.
                         */
                        context.startActivity(new Intent(context, RegisterActivity.class));
                    }
                })
                .setNegativeText(R.string.login_negative_text_failed)
                .show();

    }
}

I'm accessing it with checkIfEmailVerified();

CelticParser commented 7 years ago

Sorry I almost forgot to comeback with my findings.

The following works:

public class FailDialogs {

public static void invalidPasswordEmail(final Context context) {

   . . .

}