I am creating an app for Android in Xamarin Forms (latest) on Visual Studio. On my Android 12.1 emulator, it works fine, whether I use Target Framework as 12, 11, or 10.
But on my actual smartphone (API level 10), and on another smartphone (API level 7), it seems to crash as soon as I run this function -

        void Browsing(object sender)  
            Browser.Children.Clear();  
            FocusedItem = null;  
            CurrentFolder.Insert(0, ((sender as Frame).BindingContext as FolderFunctionality.Item).Name);  
            FolderFunctionality.Item Folder = FolderFunctionality.Get(CurrentFolder.GetRange(1, CurrentFolder.Count - 1), CurrentFolder[0]);  
            foreach (FolderFunctionality.Item a in Folder)  
                Frame button = new Frame  
                    Padding = 10,  
                    CornerRadius = 15,  
                    HasShadow = false,  
                    BackgroundColor = Color.LightSkyBlue,  
                    Content = new StackLayout  
                        HorizontalOptions = LayoutOptions.FillAndExpand,  
                        Orientation = StackOrientation.Horizontal,  
                        Padding = 0,  
                        Spacing = 10,  
                        Children =  
                            new Image { BackgroundColor = Color.Transparent, VerticalOptions = LayoutOptions.Center, HorizontalOptions = LayoutOptions.Start, HeightRequest = 50, WidthRequest = 50, Source = ImageSource.FromResource("App.Resources.Devices." + a.Type + ".png", typeof(App)) },  
                            new Label { BackgroundColor = Color.Transparent, VerticalOptions = LayoutOptions.Center, HorizontalOptions = LayoutOptions.FillAndExpand, LineBreakMode = LineBreakMode.TailTruncation, TextColor = Color.Black, Padding = 0, FontSize = 20, Text = a.Name },  
                TapGestureRecognizer tap = new TapGestureRecognizer();  
                button.BindingContext = a;  
                if (a.Type == "F") tap.Tapped += (snd, arg) =>  
                    PreviouslyFocusedItem.BackgroundColor = Color.LightSkyBlue;  
                    ((PreviouslyFocusedItem.Content as StackLayout).Children[1] as Label).LineBreakMode = LineBreakMode.TailTruncation;  
                    PreviouslyFocusedItem = snd as Frame;  
                    bool UnFocus = !(snd as Frame).HasShadow;  
                    Delays.Cancel();  
                    Delays.Dispose();  
                    Delays = new CancellationTokenSource();  
                    PreviouslyFocusedItem.BackgroundColor = Color.DeepSkyBlue;  
                    ((PreviouslyFocusedItem.Content as StackLayout).Children[1] as Label).LineBreakMode = LineBreakMode.WordWrap;  
                    if (UnFocus)  
                        FocusedItem = ((PreviouslyFocusedItem.Content as StackLayout).Children[1] as Label).Text;  
                        Task.Run(async () =>  
                            try { await Task.Delay(500, Delays.Token); } catch (OperationCanceledException) { }  
                            PreviouslyFocusedItem.HasShadow = false;  
                        (snd as Frame).HasShadow = true;  
                    else Browsing(snd);  
                else tap.Tapped += (snd, arg) =>  
                    PreviouslyFocusedItem.BackgroundColor = Color.LightSkyBlue;  
                    ((PreviouslyFocusedItem.Content as StackLayout).Children[1] as Label).LineBreakMode = LineBreakMode.TailTruncation;  
                    PreviouslyFocusedItem = snd as Frame;  
                    bool UnFocus = !(snd as Frame).HasShadow;  
                    Delays.Cancel();  
                    Delays.Dispose();  
                    Delays = new CancellationTokenSource();  
                    PreviouslyFocusedItem.BackgroundColor = Color.DeepSkyBlue;  
                    ((PreviouslyFocusedItem.Content as StackLayout).Children[1] as Label).LineBreakMode = LineBreakMode.WordWrap;  
                    if (UnFocus)  
                        FocusedItem = ((PreviouslyFocusedItem.Content as StackLayout).Children[1] as Label).Text;  
                        Task.Run(async () =>  
                            try { await Task.Delay(500, Delays.Token); } catch (OperationCanceledException) { }  
                            PreviouslyFocusedItem.HasShadow = false;  
                        (snd as Frame).HasShadow = true;  
                    else DeviceController(snd);  
                button.GestureRecognizers.Add(tap);  
                Browser.Children.Add(button);  
            if (Browser.Children.Count < 1) Browser.Children.Add(new Label { Text = "No Items Here", BindingContext = new FolderFunctionality.Item() });  
            maincontent.Content = new ScrollView { Content = Browser, HorizontalScrollBarVisibility = ScrollBarVisibility.Never };  
            topbar.Content = new StackLayout  
                Padding = new Thickness(8, 4),  
                Spacing = 0,  
                BackgroundColor = Color.Transparent,  
                Orientation = StackOrientation.Horizontal,  
                Children =  
                    backbutton,  
                    AppName,  
                    new Frame { Padding = 0, WidthRequest = 62, HeightRequest = 62, BackgroundColor = Color.Transparent }  
            MainPage = new ContentPage                                                              // Main Page  
                Opacity = 0,  
                Content = new StackLayout  
                    Spacing = 0,  
                    Children =  
                        topbar,  
                        maincontent,  
                        ChangeOptions  
            MainPage.FadeTo(1);  

Where FolderFunctionality and FolderFunctionality.Item is a class created by me (Very less chances that it causes problems as another function using it in the same way runs as expected).

The console shows this message last -
[libc] Fatal signal 11 (SIGSEGV), code 2 (SEGV_ACCERR), fault addr 0x7ffb9b4f40 in tid 32564 (companyname.app), pid 32564 (companyname.app)

Any ideas about the solution will be highly appreciated.

Android seems to have problems with Arm64 based architecture. Which give you Execute-only memory violation for some libraries. You can refer to this documentation: Diagnosing Native Crashes.

You can try the following steps for finding out more details about which library that's giving the issue.

  • On the Android device navigate to Settings > Developer options > enable USB debugging.
  • Connect the Android device to computer with USB.
  • Download Android SDK Platform Tools in your PC.
  • Run the App and crash the app
  • From the terminal run: ./adb logcat -v threadtime -d > /dump_crash/test.txt
  • A more detailed error should be in the .test.txt. That could be helpful for us to find the real reason.

    Libc means the error occurs due to native .so file invoking.

    Fatal signal 11 (SIGSEGV), code 2 (SEGV_ACCERR) means that there is an illegal address access.

    We need more detailed error log information to resolve your issue.:)

    I think the problem is -
    'Browsing()' is called as a part of a button click event which is a child of Browser'. And, Browser's children are being cleared in 'Browsing()' itself.
    So, I guess the button object is destroyed before 'Browsing' finishes, and the nulls it out.
    Here is how Browsing() is called...

    tap.Tapped += (snd, arg) =>  
                        PreviouslyFocusedItem.BackgroundColor = Color.LightSkyBlue;  
                        ((PreviouslyFocusedItem.Content as StackLayout).Children[1] as Label).LineBreakMode = LineBreakMode.TailTruncation;  
                        PreviouslyFocusedItem = snd as Frame;  
                        bool UnFocus = !(snd as Frame).HasShadow;  
                        Delays.Cancel();  
                        Delays.Dispose();  
                        Delays = new CancellationTokenSource();  
                        PreviouslyFocusedItem.BackgroundColor = Color.DeepSkyBlue;  
                        ((PreviouslyFocusedItem.Content as StackLayout).Children[1] as Label).LineBreakMode = LineBreakMode.WordWrap;  
                        if (UnFocus)  
                            FocusedItem = ((PreviouslyFocusedItem.Content as StackLayout).Children[1] as Label).Text;  
                            Task.Run(async () =>  
                                try { await Task.Delay(500, Delays.Token); } catch (OperationCanceledException) { }  
                                PreviouslyFocusedItem.HasShadow = false;  
                            (snd as Frame).HasShadow = true;  
                        else Browsing(snd);  
    

    Am I correct, and if so, can you tell me the possible solution?

    Could you please post the log of stack trace? As you said, it works fine in Android Emulator.

    Without the log information, we could hardly make code test for reproducing and find the real cause.