I'm trying to display a list of log items in an android app, however whenever I call convertView.FindViewByID, I get a NullReferenceException. The layout inflates properly with the correct amount of child views according to the debugger, but I cannot get any of the child views using FindViewById.
My solution is spread over a couple of projects, but both the adapter (and containing view) and the layout are in the same project. This project is not the startup project, but this hasn't been a problem so far.
I've made sure that I'm referencing the correct layout file, I've checked that the layout isn't improperly formed to the best of my knowledge, and I've double-checked the IDs and types of the views I'm referencing. It all looks correct.
I've also checked about 3-4 pages of search results on Google, full of answers to similar questions, but none of them working for me. I do not have access to an OnFinishInflate method, and I've made absolutely sure that it's the correct layout and that the IDs are correct. I've also checked to make sure that no IDs are duplicates. The layout doesn't exist anywhere else in the solution, and none of the IDs in the layout file exist anywhere else either. Essentially, from what I've found, the other solutions are either things I'm already doing, or not applicable because I'm doing this in an adapter's GetView method, not in a fragment or activity.
The list of items to display populates properly, and has the expected number and type of items.
Here's the GetView method:
public override View GetView(int position, View convertView, ViewGroup parent)
{
if (convertView == null)
{
convertView = _activity.LayoutInflater.Inflate(Resource.Layout.startLogListItem, null, false);
}
var log = _logsEntries[position];
var image = convertView.FindViewById<ImageView>(Resource.Id.startLogListItem_imageView1); //returns null
image.SetImageResource(Resource.Drawable.icon_info); //throws NullReferenceException
convertView.FindViewById<TextView>(Resource.Id.startLogListItem_txtMessage).Text = log.Name;
convertView.FindViewById<TextView>(Resource.Id.startLogListItem_txtDateTime).Text = log.Date.ToShortTimeString();
convertView.FindViewById<TextView>(Resource.Id.startLogListItem_txtTimespan).Text = log.Duration.ToReadableShortString();
return convertView;
}
And here's the contents of the layout file
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:src="@android:drawable/ic_menu_gallery"
android:layout_width="32dp"
android:layout_height="32dp"
android:paddingLeft="2dp"
android:layout_gravity="center_vertical"
android:id="@+id/startLogListItem.imageView1" />
<LinearLayout
android:orientation="vertical"
android:minWidth="25px"
android:minHeight="25px"
android:paddingLeft="4dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/linearLayout2">
<LinearLayout
android:orientation="vertical"
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/linearLayout3">
<TextView
android:text="Label Label Label"
android:layout_width="wrap_content"
android:textColor="#000"
android:layout_height="wrap_content"
android:id="@+id/startLogListItem.txtMessage" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/linearLayout4">
<TextView
android:text="17.35"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#393939"
android:textSize="12sp"
android:fontFamily="sans-serif-light"
android:layout_weight="1"
android:id="@+id/startLogListItem.txtDateTime"
android:paddingRight="20dp" />
<TextView
android:text="17.35"
android:layout_width="wrap_content"
android:textColor="#393939"
android:textSize="12sp"
android:fontFamily="sans-serif-light"
android:layout_height="wrap_content"
android:layout_weight="2"
android:id="@+id/startLogListItem.txtTimespan" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Whenever I call convertView.FindViewById, it returns null and any attempted assignment to one of the subviews throws a NullReferenceException.
I finally managed to find the cause of the problem and solve it.
The problem was in the ID, but not in a way that I expected, or even knew could be an issue. The problem was that the ID started with a lower case letter, not an upper case letter. Changing from a lower case to an upper case letter fixed the issue. For one reason or another, a lower case "s" doesn't work, but a lower case "a" is fine. I initially suspected this might be related to the limit on IDs, but we're nowhere near the roughly 65000 limit on resources for IDs.
But in any case, changing the first letter to a capital S fixed the issue for us.