Search code examples
c#androidxamarinxamarin.androidffimageloading

Xamarin Android FFImageLoading ImageService.Instance.LoadUrl() System.NullReferenceException


I've got a strange problem with FFImageLoading. Compiler don't show any error, but while application is starting on device, there is thrown an exception: System.NullReferenceException.


Code:

C#

private void setUserImage()
{
    ImageService.Instance.Initialize();
    imgThunbailUsername = FindViewById<ImageViewAsync>(Resource.Id.imgDisplay);
    string url = @"https://lh4.googleusercontent.com/-Lfgi-xEMwuk/VNWoy8EDWcI/AAAAAAAAACk/rwsluNfhSZY/w1486-h832-no/photo.jpg";

    ImageService.Instance.LoadUrl(url)
        .Retry(3, 200)
        .DownSample(60, 60)
        .Into(imgThunbailUsername); // compiler points here with an exception
}

XML

<FFImageLoading.Views.ImageViewAsync
  android:id="@+id/imgDisplay"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:scaleType="fitCenter" />

I'd be thankful for any tip, idea, or even another good tool for images in Xamarin.


Solution

  • Okay, I found out what's wrong. XML below is not directly under ViewModel.

    <FFImageLoading.Views.ImageViewAsync
      android:id="@+id/imgDisplay"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:scaleType="fitCenter" />
    

    XML over there is called inside

    <android.support.design.widget.NavigationView
      android:layout_width="wrap_content"
      android:layout_height="match_parent"
      android:layout_gravity="start"
      android:background="@android:color/white"
      android:id="@+id/nav_view"
      app:headerLayout="@layout/drawer_header" <!-- here -->
      app:menu="@menu/navmenu" />
    </android.support.v4.widget.DrawerLayout>
    

    So in this case I have to call properly view:

    View headerLayout = navigationView.InflateHeaderView(Resource.Layout.drawer_header);
    

    and so on use FindViewByIdlike that:

    imgThunbailUsername = headerLayout.FindViewById<ImageViewAsync>(Resource.Id.imgDisplay);
    

    So all I need to do is just get a View headerlayout with InflateHeaderView.