0

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Karol Gasienica
  • 2,825
  • 24
  • 36
  • 1
    Have you debugged it? Is `imgThunbailUsername` null? – SushiHangover Apr 27 '17 at 07:51
  • Well... I wont even comment it... Didn;t check it. It seems like compiler doesn't see any problem with it, but in real it doesn't find that element. Thank you. If you post your answer I'd accept it. – Karol Gasienica Apr 27 '17 at 08:32

1 Answers1

1

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.

Karol Gasienica
  • 2,825
  • 24
  • 36