Search code examples
androidlistviewandroid-listviewxamarin.androidlistadapter

Monodroid Listview.Adapter versus ListActivity.ListAdapter


In the project I am working on, I was originally inheriting from ListActivity in order to make activities with Listviews in them. But, as I have a base class to make settings menus, and other things simpler, I wanted to see if I could switch it over to a normal activity, in order to only have one base class. But my problem is, with one of the activities, Using Listactivity.Listadapter with the adapter I built works fine, but when I try to use the same adapter for a ListView.Adapter, it gives me a nullreferenceexeption. Looking in the documentation, it looks like both are calling for the same object type (IListAdapter) and I cannot for the life of me figure out why.

    private ListView ticketView;
    List<TicketViewModel> tickets;
    List<String> ticketInfo;
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        ticketView = (ListView)FindViewById(Android.Resource.Id.List);
        //pulls data from a remote source
        //and puts it into a string array
        tickets = new List<Ticket>();
        tickets = DataDownload();
        ticketInfo = new List<string>();
        foreach (Ticket tick in tickets)
        {
            ticketInfo.Add(tick.Subject);
        }

        //adapter I am using
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
        Resource.Layout.itemList, ticketInfo);

        //this one works
        ListAdapter = adapter;

        //this one does not
        ticketView.Adapter = adapter;
    }

I'm sure I'm missing something obvious, but I can't for the life of me figure it out.

Edit:

Here's the logcat

07-30 08:59:53.498 I/ActivityManager(  162): Starting: Intent { cmp=KahunaAndroid.KahunaAndroid/kahunaandroid.TicketList } from pid 2101
07-30 08:59:59.534 D/ViewConsistency( 2101): AbsListView android.widget.ListView@40845958 enabled= true
07-30 08:59:59.594 D/dalvikvm( 2101): GC_EXTERNAL_ALLOC freed 9K, 47% free 3405K/6407K, external 575K/587K, paused 55ms
07-30 08:59:59.604 D/ViewConsistency( 2101): AbsListView android.widget.ListView@40845958 enabled= true
07-30 09:00:02.947 D/ViewConsistency( 2101): AbsListView android.widget.ListView@40867758 enabled= true
07-30 09:00:02.947 D/ViewConsistency( 2101): AbsListView android.widget.ListView@40867758 enabled= true
07-30 09:00:03.498 W/ActivityManager(  162): Launch timeout has expired, giving up wake lock!
07-30 09:00:03.538 W/ActivityManager(  162): Activity idle timeout for HistoryRecord{40a0aa88 KahunaAndroid.KahunaAndroid/kahunaandroid.TicketList}
07-30 09:00:13.958 I/MonoDroid( 2101): UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object
07-30 09:00:13.958 I/MonoDroid( 2101): at KahunaAndroid.TicketList.OnCreate (Android.OS.Bundle) [0x000aa] in C:\Users\david\Documents\Visual Studio 2010\Projects\AndroidApplication2\AndroidApplication2\Tickets\TicketList.cs:44
07-30 09:00:13.958 I/MonoDroid( 2101): at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (intptr,intptr,intptr) [0x00010] in /Users/builder/data/lanes/monodroid-mac-monodroid-4.2.4-branch/9f7cbd60/source/monodroid/src/Mono.Android/platforms/android-8/src/generated/Android.App.Activity.cs:1490
07-30 09:00:13.958 I/MonoDroid( 2101): at (wrapper dynamic-method) object.50e53c19-ce69-4f39-9dda-0aec4e7e7dcd (intptr,intptr,intptr) <IL 0x00012, 0x00033>

And the Exception "System.NullReferenceException: Object reference not set to an instance of an object"


Solution

  • Using the other page I had working with a similar list style, I got this one working after playing with it a bit. I think what was causing the error ( I made quite a few changes other than this one, so it is possible something else fixed it) was I was forgetting:

    SetContentView(Resource.Layout.itemList);
    

    before initializing the listview. After changing that, it works as expected. Since I wasn't using that view to create the ListView I was using (from android.resource) I originally didn't think that it was necessary to call it before getting the listview item.