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.