Xamarin Android Multiple Activity error: System.MemberAccessException

Frans Pinos 21 Reputation points
2020-06-17T07:48:58.873+00:00

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 = "@ hide /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 = "Activity3")]
public class Activity3 : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);

        // Create your application here  
    }  
}  

}

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: 'Cannot create an instance of Android.Widget.ArrayAdapter`1[T] because Type.ContainsGenericParameters is true.'

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.

Not Monitored
Not Monitored
Tag not monitored by Microsoft.
36,235 questions
0 comments No comments
{count} votes

Accepted answer
  1. Richard Zhang-MSFT 6,936 Reputation points
    2020-06-17T08:21:57.317+00:00

    Hello,

    Welcome to Microsoft Q&A!

    Q&A will add more product support in the future, you can view the current product support list here: Supported products

    Your question about Xamarin.Android is not supported right now. But there is a better place for asking questions about this, you could ask about this question in Xamarin.Android forums.

    Thank you.

    0 comments No comments

0 additional answers

Sort by: Most helpful