0

I am trying to show a snackbar:

Snackbar s = Snackbar
                .Make(Window.DecorView.RootView, text, Snackbar.LengthLong)
                .SetAction("Retry", view =>
                {
                    /* TODO */
                });
s.Show();

I am getting a NullReferenceException, when calling the method:

Object reference not set to an instance of an object.

What Am I doing wrong?

hans meyer
  • 91
  • 2
  • 9
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Camilo Terevinto Oct 25 '16 at 13:17
  • What is `text` containing upon the call? Why is your `view =>` for `.SetAction` not filled out at all? There's missing information, and is probably why your code sees *null references* (nothing). – gravity Oct 25 '16 at 13:21
  • Ah okay, just debugged it again and noticed, that the view is null. I am calling the method via DependencyInjection. What is the correct view to pass the android view as a parameter OR how can I get the current view? – hans meyer Oct 25 '16 at 13:34

1 Answers1

2

We were having a similar problem. Although we followed the documentation, we kept getting a NullReferenceException which turned out to be the parent layout view.

In my case, it worked after I cleaned and build my Visual Studio Project.

Please see my code below for reference:

I have the following parent linear layout with an id reference loginView:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:id="@+id/loginView"
    android:orientation="vertical" >
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:orientation="vertical">

        <!-- omitted --> 

    </LinearLayout>
</LinearLayout>

Next, I have a utility class with the following method:

/// <summary>
/// Show a snackbar notification on screen
/// </summary>
/// <param name="view">this is the parent container</param>
/// <param name="msg">message to show in the snackbar</param>
public static void ShowSnack(View view, string msg)
{            
    if(view != null)
    {
        try
        {
            Snackbar snackBar = Snackbar.Make(view, msg, Snackbar.LengthLong);
            snackBar.SetAction("Ok", (v) =>
            {
                Log.Info(TAG, "Action in view clicked");
            });
            snackBar.Show();
        }
        catch(Exception ex)
        {
            Log.Error(TAG, "Error occured when showing snackbar: " + ex.Message); 
        }                
    }
}

Finally, I can display the SnackBar from my activity with the following:

var view = FindViewById(Resource.Id.loginView);
AndroidUtil.ShowSnack(view, "Hey there it works!");

More info:

We're currently using Visual Studio 2017 RC. One thing we noticed was that we had to clean the project a lot as this was a usual occurrence.

keshav.bahadoor
  • 1,816
  • 1
  • 12
  • 19
  • Thank you! Due to some internal design changes we stopped using the snackbar, but I'll still accept your answer tho! Thank you :) – hans meyer Feb 09 '17 at 09:06