locked
Multiple Activity , spinner and: Cannot create an instance of Android.Widget.ArrayAdapter`1[T] RRS feed

  • Question

  • User395244 posted

    After experience spinner problems at the time to add more than 4 Activity in my main App (Xamarin Android) I builded one very simple App with the minimum minimum in order to play with spinners. This test App is like the following:

    MainActivity has only one Button that transfers to Activity1. In MainActivity there's no code, only the button:

    namespace App2
    {
        [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
        public class MainActivity : AppCompatActivity
        {
            protected override void OnCreate(Bundle savedInstanceState)
            {
                base.OnCreate(savedInstanceState);
                Xamarin.Essentials.Platform.Init(this, savedInstanceState);
                // Set our view from the "main" layout resource
                SetContentView(Resource.Layout.activity_main);
    
                Button Btn_1 = FindViewById<Button>(Resource.Id.button1);
                Btn_1.Click += delegate
                {
                    StartActivity(typeof(Activity1));
                };
           }
        }
    }
    

    Activity1 has only the code corresponding to the Spinner. Nothing else.

    namespace App2
    {
        [Activity(Label = "Activity1")]
        public class Activity1 : Activity
        {
            private static Spinner N_Filter;
            protected override void OnCreate(Bundle savedInstanceState)
            {
                base.OnCreate(savedInstanceState);
    
                SetContentView(Resource.Layout.layout1);
                // Create your application here
                N_Filter = (Spinner)FindViewById(Resource.Id.nfiltros);
    
                //- Definición Spinner Número de Filtros
                N_Filter.ItemSelected += new EventHandler<AdapterView.ItemSelectedEventArgs>(N_Filter_ItemSelected);
                var adapter = ArrayAdapter.CreateFromResource(this, Resource.Array.Num_1a24, Android.Resource.Layout.SimpleSpinnerItem);
                adapter.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);
                N_Filter.Adapter = adapter;
            }
            private void N_Filter_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e)
            {
                int Xi = e.Position;
    
                Xi = Xi * 2;
            }
        }
    }
    

    All the rest of the Activity2...4 are only empty activities add automatically Like:

    namespace App2
    {
        [Activity(Label = "Activity2")]
        public class Activity2 : Activity
        {
            //        protected override void OnCreate(Bundle savedInstanceState)
            protected override void OnDestroy()
            {
                base.OnDestroy();
            }
        }
    }
    

    At the time of add Activity 4 is when the following errors occurs: In line: var adapter = ArrayAdapter.CreateFromResource(this, Resource.Array.Num_1a24, Android.Resource.Layout.SimpleSpinnerItem);

    Exception error: System.MemberAccessException: &#39;Cannot create an instance of Android.Widget.ArrayAdapter`1[T] because Type.ContainsGenericParameters is true.&#39;

    And IF I REMOVE Activity4 the App WORKS again WITH NO ISSUE. If I add it again it fails...

    Note: Num_1a24 is one string-array on strings.xml.

    Wednesday, June 17, 2020 8:49 AM

All replies

  • User379860 posted

    All the rest of the Activity2...4 are only empty activities add automatically Like:

    The rest of Activity2...4 just were created. Are not used in the this application?

    I create a same application, I test it, it worked as normal. Here is running GIF.

    Here is my demo, you can download and test it, you can updata your demo as well.

    Wednesday, June 17, 2020 10:56 AM
  • User124665 posted

    @Frans_Pinos same exception message here, but with an interesting thing: the app runs perfectly using Visual Studio for Mac. The problem only occurs for me when I use Rider.

    Wednesday, June 17, 2020 2:35 PM
  • User124665 posted

    @Frans_Pinos try to create your adapter this way:

    csharp var adapter = new ArrayAdapter<string>( this, Android.Resource.Layout.SimpleSpinnerItem, Resource.Array.Num_1a24 );

    I don't know yet why this occurs, but this way to create the adapter solved the problem for me.

    Wednesday, June 17, 2020 5:56 PM
  • User395244 posted

    @LeonLu , Just I tested your solution and curiously the error appears at the time of add Activity5. If I remove it workks fine. By the contrary in my solution is with Activity4 !!! I have no idea if it is becasue my VS configuration or why...

    Friday, June 19, 2020 2:20 PM
  • User395244 posted

    @ionixjunior Thx, I will test your suggestion during the weekend and will keep you informed. Anyway, in my opinion there is something wrong with the spinners treatment on xamarin android. Or may be on my VS ;)

    Friday, June 19, 2020 2:24 PM
  • User395244 posted

    @LeonLu Sorry about forgetting question about if used or not Activity 2 to 4. As I mentioned it was only a test try to reproduce the error on the simplest mode as possible. Many thanks to share with me your work.

    Friday, June 19, 2020 2:33 PM
  • User395244 posted

    @LeonLu Sorry about forgetting question about if used or not Activity 2 to 4. As I mentioned it was only a test try to reproduce the error on the simplest mode as possible. Many thanks to share with me your work.

    Friday, June 19, 2020 2:33 PM
  • User395244 posted

    @LeonLu Sorry about forgetting question about if used or not Activity 2 to 4. As I mentioned it was only a test try to reproduce the error on the simplest mode as possible. Many thanks to share with me your work.

    Friday, June 19, 2020 2:33 PM
  • User395244 posted

    @LeonLu Sorry about forgetting question about if used or not Activity 2 to 4. As I mentioned it was only a test try to reproduce the error on the simplest mode as possible. Many thanks to share with me your work.

    Friday, June 19, 2020 2:33 PM
  • User379860 posted

    @Frans_Pinos said: @LeonLu , Just I tested your solution and curiously the error appears at the time of add Activity5. If I remove it workks fine. By the contrary in my solution is with Activity4 !!! I have no idea if it is becasue my VS configuration or why...

    First of all, please update your VS 2019 to the latest version, then make a test, if this issue is still existed, you can upload your demo to here, I can help you to test it and check the code completely.

    Monday, June 22, 2020 6:57 AM