0

I have this code separate class which makes a Snackbar to be displayed within my application, But with my current implementation I am getting a 'java.lang.NullPointerException'. How do I implement it in my main class properly?

here is my snack bar class:

public class SnackBarUtils
{
private static SnackBarUtils mInstance = null;
private  Snackbar mSnackBar;

private SnackBarUtils()
{

}

public static SnackBarUtils getInstance()
{
    if (mInstance == null)
    {
        mInstance = new SnackBarUtils();
    }
    return mInstance;
}

public void hideSnackBar()
{
    if (mSnackBar != null)
    {
        mSnackBar.dismiss();
    }
}

public void showProblemSnackBar(final Activity activity, final String message)
{
    mSnackBar = Snackbar.make(activity.findViewById(android.R.id.content), message, 
    Snackbar.LENGTH_INDEFINITE);
    // Changing action button text color
    View sbView = mSnackBar.getView();
    TextView textView = sbView.findViewById(com.google.android.material.R.id.snackbar_text);
    mSnackBar.setAction("x", new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            //Call your action method here
            mSnackBar.dismiss();
        }
    });
    textView.setTextColor(Color.WHITE);
    sbView.setBackgroundColor(Color.RED);
    textView.setMaxLines(3);
    mSnackBar.show();
}
}

This is my current implementation within main activity, I have already Initialized the snackbar class like this:

SnackBarUtils snackBarUtils;

and then called it like this:

snackBarUtils.showProblemSnackBar(MainActivity.this, mPlainTextResponse);

what am I doing wrong? Or what is the correct way to do this?

Reaper
  • 35
  • 5

1 Answers1

0

First of all, you would share the stacktrace of the NPE for more context.

For the snackbar utility:
If you are using callbacks, then you can use the utility for displaying a snackbar with that callback as parameter:

interface onProblemSnackbarClickedListener {
  void onActionClicked(View view);
}
...
/* inside SnackBarUtils.java */
...
public static void showProblemSnackbar(View view, @StringRes int message, onProblemSnackbarClickedListener listener){
   Snackbar mSnackBar = Snackbar.make(view,message,Snackbar.LENGTH_INDEFINITE)
    .setAction("x", new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        listener.onActionClicked(v);
        mSnackBar.dismiss();
      }
  })
  mSnackbar.show();
}

The callback could work for the need to listen to it in the activity/fragment.

For the styling of the Snackbar, you can see this related question: Style SnackBar in theme app.

Keep in mind the migration from "Support design" to MDC (Material design components), that facilitates the global styling of the snackbar with theme attributes.

Marlon López
  • 460
  • 8
  • 21
  • Thank you for the answer but how do I call it in my Main Activity to show a snackbar? – Reaper Sep 29 '20 at 08:23
  • @Reaper .... since you have a SnackBarUtils class, you can call it as before... in your activity it would be this way: /* inside your activity */ SnackBarUtils.getInstance().showProblemSnackbar(rootView, R.string.snack_message, new onProblemSnackbarClickedListener { (at)Override public void onActionClicked(View v){ /* callback logic here. */ } }) – Marlon López Sep 29 '20 at 14:17