在我正在进行的项目中,我最初是从 ListActivity 继承的,以便在其中使用 Listviews 进行活动。但是,由于我有一个基类来制作设置菜单和其他更简单的东西,我想看看我是否可以将它切换到一个正常的活动,以便只有一个基类。但是我的问题是,对于其中一项活动,将 Listactivity.Listadapter 与我构建的适配器一起使用可以正常工作,但是当我尝试对 ListView.Adapter 使用相同的适配器时,它给了我一个 nullreferenceexeption。查看文档,看起来两者都在调用相同的对象类型(IListAdapter),而我终其一生都无法弄清楚原因。
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;
}
我确定我遗漏了一些明显的东西,但我终其一生都无法弄清楚。
编辑:
这是日志猫
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>
和异常“System.NullReferenceException:对象引用未设置为对象的实例”