Xamarin 中的列表视图错误

Listview Error in Xamarin

我正在尝试制作一个简单的列表视图,但出现错误,我不知道为什么!谁能告诉我我做错了什么?

代码:

namespace ListViewApp
{
    [Activity(Label = "ListViewApp", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {

        private List<string> nItem;
        private ListView lview;
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            lview = FindViewById<ListView>(Resource.Id.listView1);
            SetContentView(Resource.Layout.Main);

            nItem = new List<string>();
            nItem.Add("Bob");
            nItem.Add("XYZ");
            nItem.Add("AAA");
            nItem.Add("BBB");

            ArrayAdapter adapter = new ArrayAdapter<String>(this, Android.Resource.Layout.SimpleListItem1, nItem);

            lview.Adapter = adapter;

        }
    }
}

错误: 未处理的异常:

System.NullReferenceException: 对象引用未设置为对象的实例

发生未处理的异常。

In mgmain JNI_OnLoad
 D/dalvikvm( 3164): Late-enabling CheckJNI
 D/dalvikvm( 3164): Trying to load lib /data/app-lib/ListViewApp.ListViewApp-1/libmonodroid.so 0xa503e468
 D/dalvikvm( 3164): Added shared lib /data/app-lib/ListViewApp.ListViewApp-1/libmonodroid.so 0xa503e468
 W/monodroid-debug( 3164): Trying to initialize the debugger with options: --debugger-agent=transport=dt_socket,loglevel=0,address=127.0.0.1:8867,server=y,embedding=1
7 W/monodroid-debug( 3164): Accepted stdout connection: 42
 D/Mono    ( 3164): Image addref mscorlib[0xb8ef1fa8] -> mscorlib.dll[0xb8f0c290]: 1
 D/Mono    ( 3164): AOT module 'mscorlib.dll.so' not found: dlopen failed: library "/data/data/ListViewApp.ListViewApp/lib/mscorlib.dll.so" not found
 D/Mono    ( 3164): Assembly mscorlib[0xb8ef1fa8] added to domain RootDomain, ref_count=1
9 D/Mono    ( 3164): Assembly Loader probing location: '/storage/emulated/0/Android/data/ListViewApp.ListViewApp/files/.__override__/ListViewApp.dll'.
 D/Mono    ( 3164): Image addref ListViewApp[0xb8f3fe38] -> /storage/emulated/0/Android/data/ListViewApp.ListViewApp/files/.__override__/ListViewApp.dll[0xb8f3eff8]: 2
 D/Mono    ( 3164): Assembly ListViewApp[0xb8f3fe38] added to domain RootDomain, ref_count=1
3 D/Mono    ( 3164): AOT module '/storage/emulated/0/Android/data/ListViewApp.ListViewApp/files/.__override__/ListViewApp.dll.so' not found: dlopen failed: library "/data/data/ListViewApp.ListViewApp/lib//storage/emulated/0/Android/data/ListViewApp.ListViewApp/files/.__override__/ListViewApp.dll.so" not found
 D/Mono    ( 3164): Assembly Loader loaded assembly from location: '/storage/emulated/0/Android/data/ListViewApp.ListViewApp/files/.__override__/ListViewApp.dll'.
 D/Mono    ( 3164): Config attempting to parse: '/storage/emulated/0/Android/data/ListViewApp.ListViewApp/files/.__override__/ListViewApp.dll.config'.
 D/Mono    ( 3164): Config attempting to parse: '/Users/builder/data/lanes/monodroid-mlion-monodroid-4.20-series/ba9bbbdd/source/monodroid/builds/install/mono-x86/etc/mono/assemblies/ListViewApp/ListViewApp.config'.
 W/monodroid-gc( 3164): GREF GC Threshold: 46800
 D/Mono    ( 3164): Image addref Mono.Android[0xb8f40d78] -> Mono.Android.dll[0xb8f402a0]: 1
 D/Mono    ( 3164): Assembly Mono.Android[0xb8f40d78] added to domain RootDomain, ref_count=1
5 D/Mono    ( 3164): AOT module 'Mono.Android.dll.so' not found: dlopen failed: library "/data/data/ListViewApp.ListViewApp/lib/Mono.Android.dll.so" not found
 D/Mono    ( 3164): Assembly Ref addref Mono.Android[0xb8f40d78] -> mscorlib[0xb8ef1fa8]: 2
 W/monodroid( 3164): Xamarin/Android Trial Mode Active
 D/dalvikvm( 3164): GC_FOR_ALLOC freed 53K, 4% free 2752K/2864K, paused 188ms, total 195ms
 D/dalvikvm( 3164): GC_CONCURRENT freed <1K, 4% free 3183K/3296K, paused 25ms+84ms, total 138ms
 D/libEGL  ( 3164): loaded /system/lib/egl/libEGL_genymotion.so
 D/        ( 3164): HostConnection::get() New Host Connection established 0xb8f77590, tid 3164
 D/libEGL  ( 3164): loaded /system/lib/egl/libGLESv1_CM_genymotion.so
 D/libEGL  ( 3164): loaded /system/lib/egl/libGLESv2_genymotion.so
 W/EGL_genymotion( 3164): eglSurfaceAttrib not implemented
 E/OpenGLRenderer( 3164): Getting MAX_TEXTURE_SIZE from GradienCache
 E/OpenGLRenderer( 3164): MAX_TEXTURE_SIZE: 16384
 E/OpenGLRenderer( 3164): Getting MAX_TEXTURE_SIZE from Caches::initConstraints()
 E/OpenGLRenderer( 3164): MAX_TEXTURE_SIZE: 16384
 D/OpenGLRenderer( 3164): Enabling debug mode 0
 D/Mono    ( 3164): Image addref System.Core[0xb8fb1c50] -> System.Core.dll[0xb8fb1040]: 1
 D/Mono    ( 3164): Assembly System.Core[0xb8fb1c50] added to domain RootDomain, ref_count=1
1 D/Mono    ( 3164): AOT module 'System.Core.dll.so' not found: dlopen failed: library "/data/data/ListViewApp.ListViewApp/lib/System.Core.dll.so" not found
 D/Mono    ( 3164): Assembly Ref addref Mono.Android[0xb8f40d78] -> System.Core[0xb8fb1c50]: 2
 D/Mono    ( 3164): Assembly Ref addref System.Core[0xb8fb1c50] -> mscorlib[0xb8ef1fa8]: 3
The program 'Mono' has exited with code 0 (0x0).

您应该首先致电:

SetContentView(Resource.Layout.Main);

然后是

lview = FindViewById<ListView>(Resource.Id.listView1);