Why i cauth an exception on listView?

EdelarkEdelark ESMember
edited August 2015 in Xamarin.Android

**Hi! i have 2 listView in diferents layouts, but when i run my app ocurre it: **

Java.Lang.RuntimeException: java.lang.reflect.InvocationTargetException
at --- End of managed exception stack trace ---
at java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at at dalvik.system.NativeStart.main(Native Method)
at Caused by: java.lang.reflect.InvocationTargetException
at at java.lang.reflect.Method.invokeNative(Native Method)
at at java.lang.reflect.Method.invoke(Method.java:511)
at at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at ... 2 more
at Caused by: md52ce486a14f4bcd95899665e9d932190b.JavaProxyThrowable: System.NullReferenceException: Object reference not set to an instance of an object
at ILoveCanariasExperience.MainActivity.OnCreate (Android.OS.Bundle) [0x00298] in c:\Users\acobos\Documents\Projects\ILoveCanariasExperience\ILoveCanariasExperience\MainActivity.cs:62
at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (intptr,intptr,intptr) [0x00011] in /Users/builder/data/lanes/1879/5f55a9ef/source/monodroid/src/Mono.Android/platforms/android-19/src/generated/Android.App.Activity.cs:2475
at at (wrapper dynamic-method) object.aeed765e-97c4-4ee0-9a74-8714b4c24128 (intptr,intptr,intptr)
at at md51f824fc9a0ec24140417a1c6ab96caf9.MainActivity.n_onCreate(Native Method)
at at md51f824fc9a0ec24140417a1c6ab96caf9.MainActivity.onCreate(MainActivity.java:28)
at at android.app.Activity.performCreate(Activity.java:5008)
at at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
at at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at at android.app.ActivityThread.access$600(ActivityThread.java:130)
at at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at at android.os.Handler.dispatchMessage(Handler.java:99)
at at android.os.Looper.loop(Looper.java:137)
at at android.app.ActivityThread.main(ActivityThread.java:4745)
at ... 5 more

Xamarin studio remarked Line : mListView.Adapter = adapter;

this is my MainActivity.cs

using System;

using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using System.Collections.Generic;



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

        private List<BotonMenu> mItems;
        private ListView mListView;

        private List<DiveSite> mItems2;
        private ListView mListView2;

        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);


            // Set our view from the "main" layout resource
            SetContentView (Resource.Layout.dive_sites);
            mListView = FindViewById<ListView>(Resource.Id.List23);
            mListView2 = FindViewById<ListView>(Resource.Id.List_dive_sites);

            mItems = new List<BotonMenu>();
            mItems2 = new List<DiveSite>();

            mItems.Add(new BotonMenu(){ Nombre = "DIVE SITES" });
            mItems.Add(new BotonMenu(){ Nombre = "LOCAL GALLERY" });
            mItems.Add(new BotonMenu(){ Nombre = "ABOUT CANARY ISLANDS" });
            mItems.Add(new BotonMenu(){ Nombre = "EMERGENCY PLAN" });
            mItems.Add(new BotonMenu(){ Nombre = "INTERESTING INFO" });
            mItems.Add(new BotonMenu(){ Nombre = "ANIMAL LIFE" });
            mItems.Add(new BotonMenu(){ Nombre = "ABOUT US" });

            mItems2.Add(new DiveSite(){Nombre="LA PALMA", Src="@drawable/ic_lapalma" });
            mItems2.Add(new DiveSite(){Nombre="EL HIERRO", Src="@drawable/ic_elhierro" });
            mItems2.Add(new DiveSite(){Nombre="LA GOMERA", Src="@drawable/ic_lagomera" });
            mItems2.Add(new DiveSite(){Nombre="TENERIFE", Src="@drawable/ic_tenerife" });
            mItems2.Add(new DiveSite(){Nombre="GRAN CANARIA", Src="@drawable/ic_grancanaria" });
            mItems2.Add(new DiveSite(){Nombre="FUERTEVENTURA", Src="@drawable/ic_fuerteventura" });
            mItems2.Add(new DiveSite(){Nombre="LANZAROTE", Src="@drawable/ic_lanzarote" });



            // Get our button from the layout resource,
            // and attach an event to it
            AdapterClass adapter = new AdapterClass(this,mItems);

            AdapterClass2 adapter2 = new AdapterClass2(this,mItems2);

            mListView.Adapter = adapter;

            mListView2.Adapter = adapter2;
        }

    }
}

**What wrong?

my Adapter class: **

using System;
using System.Linq;
using System.Text;
using System.Collections.Generic;

using Android.App;
using Android.Widget;
using Android.Content;
using Android.Views;
using Android.OS;
using Android.Runtime;

namespace ILoveCanariasExperience
{
    public class AdapterClass : BaseAdapter<BotonMenu>
    {
        public List<BotonMenu> mItems;
        private Context mContext;


        public AdapterClass(Context context, List<BotonMenu> items)
        {
            mItems = items;
            mContext = context;
        }
        #region implemented abstract members of BaseAdapter

        public override long GetItemId(int position)
        {
            return position;
        }

        public override Android.Views.View GetView(int position, View convertView, ViewGroup parent)
        {
            View row = convertView;

            if(row == null)
            {
                row = LayoutInflater.From(mContext).Inflate(Resource.Layout.molde_list_view, null, false);
            }
            TextView txtName = row.FindViewById<TextView>(Resource.Id.txtName);
            txtName.Text = mItems[position].Nombre;

            return row;
        }

        public override int Count
        {
            get
            {
                return mItems.Count;
            }
        }

        #endregion

        #region implemented abstract members of BaseAdapter

        public override BotonMenu this[int index]
        {
            get
            {
                return mItems[index];
            }
        }

        #endregion
    }
}

Answers

  • EdelarkEdelark ESMember
    edited August 2015

    I can't find the error! :sweat:

  • ArturMalendowiczArturMalendowicz USMember ✭✭
    edited August 2015

    Are you sure that mListView = FindViewById<ListView>(Resource.Id.List23); found the view? Looks like mListView is null.

  • EdelarkEdelark ESMember

    I have mListView and mListView2 but mListView2 work correctly... listView and listView2 isn't the same layout... can be it the error?

  • ArturMalendowiczArturMalendowicz USMember ✭✭

    Is mListView != null ?

  • EdelarkEdelark ESMember

    I put this code in another activity and work!

Sign In or Register to comment.