1

I am developing a Xamarin (PCL) cross platform app that i am currently testing on my Android Physical devices and Emulators. The app runs perfectly on the high-end phones, but "crashes unexpectedly" on the Samsung S4 and such low-end phones. It crashes only after i perform multiple activities and tasks.

I am assuming that has something to do with the task-thread-capacity of these phones. If i am right in that regard, how am i supposed to make my app such that it works smoothly and error free on all phones?

Edit - The error says "The app has closed unexpectedly". This error doesn't appear on a specific line because it has nothing to do with the code. It breaks only on certain "older" phones when many activities are performed. Example of activities include, Add data to database, updating/deleting them, moving from one activity to another, showing calendars and pie-charts from user entered data.

Edit 2 -Some of the Code that deals with images:

Xaml:

<ScrollView HorizontalOptions="Fill" Orientation="Horizontal">
                                <StackLayout Orientation="Vertical" BackgroundColor="GhostWhite" >
                                    <StackLayout Orientation="Horizontal" BackgroundColor="GhostWhite" >
                                        <Button HeightRequest="50" BackgroundColor="GhostWhite"  WidthRequest="50" Command="{Binding Button_Clicked_Food}" CommandParameter="dairy" Image="{Binding Dairy_Image}"/>
                                        <Button HeightRequest="50" BackgroundColor="GhostWhite"  WidthRequest="50" Command="{Binding Button_Clicked_Food}" CommandParameter="alcohol" Image="{Binding Alcohol_Image}"/>
                                        <Button HeightRequest="50" BackgroundColor="GhostWhite"  WidthRequest="50" Command="{Binding Button_Clicked_Food}" CommandParameter="eggs" Image="{Binding Egg_Image}"/>
                                        <Button HeightRequest="50" BackgroundColor="GhostWhite"  WidthRequest="50" Command="{Binding Button_Clicked_Food}" CommandParameter="fastfood" Image="{Binding Fastfood_Image}"/>
                                        <Button HeightRequest="50" BackgroundColor="GhostWhite"  WidthRequest="50" Command="{Binding Button_Clicked_Food}" CommandParameter="fish" Image="{Binding Fish_Image}"/>
                                        <Button HeightRequest="50" BackgroundColor="GhostWhite"  WidthRequest="50" Command="{Binding Button_Clicked_Food}" CommandParameter="fruit" Image="{Binding Fruit_Image}"/>
                                        <Button HeightRequest="50" BackgroundColor="GhostWhite"  WidthRequest="50" Command="{Binding Button_Clicked_Food}" CommandParameter="grain" Image="{Binding Grain_Image}"/>
                                        <Button HeightRequest="50" BackgroundColor="GhostWhite"  WidthRequest="50" Command="{Binding Button_Clicked_Food}" CommandParameter="legume" Image="{Binding Legume_Image}"/>
                                    </StackLayout>
                                    <StackLayout Orientation="Horizontal" BackgroundColor="GhostWhite" >
                                        <Button HeightRequest="50" BackgroundColor="GhostWhite"  WidthRequest="50" Command="{Binding Button_Clicked_Food}" CommandParameter="meat" Image="{Binding Meat_Image}"/>
                                        <Button HeightRequest="50" BackgroundColor="GhostWhite"  WidthRequest="50" Command="{Binding Button_Clicked_Food}" CommandParameter="munchies" Image="{Binding Munchies_Image}"/>
                                        <Button HeightRequest="50" BackgroundColor="GhostWhite"  WidthRequest="50" Command="{Binding Button_Clicked_Food}" CommandParameter="nuts" Image="{Binding Nut_Image}"/>
                                        <Button HeightRequest="50" BackgroundColor="GhostWhite"  WidthRequest="50" Command="{Binding Button_Clicked_Food}" CommandParameter="potato" Image="{Binding Potato_Image}"/>
                                        <Button HeightRequest="50" BackgroundColor="GhostWhite"  WidthRequest="50" Command="{Binding Button_Clicked_Food}" CommandParameter="soda" Image="{Binding Soda_Image}"/>
                                        <Button HeightRequest="50" BackgroundColor="GhostWhite"  WidthRequest="50" Command="{Binding Button_Clicked_Food}" CommandParameter="sweets" Image="{Binding Sweet_Image}"/>
                                        <Button HeightRequest="50" BackgroundColor="GhostWhite"  WidthRequest="50" Command="{Binding Button_Clicked_Food}" CommandParameter="vegetables" Image="{Binding Vegetable_Image}"/>
                                    </StackLayout>
                                </StackLayout>

.CS:

 public UserMealINC_vm(User_Profiles up, DateTime day)
        {
            try
            {
                Day = day;
                User_pro = up;
                Bool_Food_Type = false;
                Food_Name = "";
                Type = "";
                Food_Weight = "0";
                Selected_Food = new List<string>();

                //All meal item are first initialized with the non-coloured images
                Dairy_Image = "drawable/dairy.png";
                Alcohol_Image = "drawable/alcohol.png";
                Egg_Image = "drawable/eggs.png";
                Fastfood_Image = "drawable/fastfood.png";
                Fish_Image = "drawable/fish.png";
                Fruit_Image = "drawable/fruit.png";
                Grain_Image = "drawable/grain.png";
                Legume_Image = "drawable/legume.png";
                Meat_Image = "drawable/meat.png";
                Munchies_Image = "drawable/munchies.png";
                Nut_Image = "drawable/nuts.png";
                Potato_Image = "drawable/potato.png";
                Soda_Image = "drawable/soda.png";
                Sweet_Image = "drawable/sweets.png";
                Vegetable_Image = "drawable/vegetables.png";

                this.Button_Clicked_Food = new Command<string>((key) =>
                {
                    //Change the item selected from color to non-color, or vice-versa

                    if (Selected_Food.Contains(key))
                    {
                        if (key == "dairy")
                        {
                            Dairy_Image = "drawable/dairy.png";
                        }
                        else if (key == "alcohol")
                        {
                            Alcohol_Image = "drawable/alcohol.png";
                        }
                        else if (key == "eggs")
                        {
                            Egg_Image = "drawable/eggs.png";
                        }
                        else if (key == "fastfood")
                        {
                            Fastfood_Image = "drawable/fastfood.png";
                        }
                        else if (key == "fish")
                        {
                            Fish_Image = "drawable/fish.png";
                        }
                        else if (key == "fruit")
                        {
                            Fruit_Image = "drawable/fruit.png";
                        }
                        else if (key == "grain")
                        {
                            Grain_Image = "drawable/grain.png";
                        }
                        else if (key == "legume")
                        {
                            Legume_Image = "drawable/legume.png";
                        }
                        else if (key == "meat")
                        {
                            Meat_Image = "drawable/meat.png";
                        }
                        else if (key == "munchies")
                        {
                            Munchies_Image = "drawable/munchies.png";
                        }
                        else if (key == "nuts")
                        {
                            Nut_Image = "drawable/nuts.png";
                        }
                        else if (key == "potato")
                        {
                            Potato_Image = "drawable/potato.png";
                        }
                        else if (key == "soda")
                        {
                            Soda_Image = "drawable/soda.png";
                        }
                        else if (key == "sweets")
                        {
                            Sweet_Image = "drawable/sweets.png";
                        }
                        else if (key == "vegetables")
                        {
                            Vegetable_Image = "drawable/vegetables.png";
                        }
                        else
                        {
                            //Key out of bounds???
                        }

                        Selected_Food.Remove(key);
                    }
                    else
                    {
                        if (key == "dairy")
                        {
                            Dairy_Image = "drawable/dairy_color.png";
                        }
                        else if (key == "alcohol")
                        {
                            Alcohol_Image = "drawable/alcohol_color.png";
                        }
                        else if (key == "eggs")
                        {
                            Egg_Image = "drawable/eggs_color.png";
                        }
                        else if (key == "fastfood")
                        {
                            Fastfood_Image = "drawable/fastfood_color.png";
                        }
                        else if (key == "fish")
                        {
                            Fish_Image = "drawable/fish_color.png";
                        }
                        else if (key == "fruit")
                        {
                            Fruit_Image = "drawable/fruit_color.png";
                        }
                        else if (key == "grain")
                        {
                            Grain_Image = "drawable/grain_color.png";
                        }
                        else if (key == "legume")
                        {
                            Legume_Image = "drawable/legume_color.png";
                        }
                        else if (key == "meat")
                        {
                            Meat_Image = "drawable/meat_color.png";
                        }
                        else if (key == "munchies")
                        {
                            Munchies_Image = "drawable/munchies_color.png";
                        }
                        else if (key == "nuts")
                        {
                            Nut_Image = "drawable/nuts_color.png";
                        }
                        else if (key == "potato")
                        {
                            Potato_Image = "drawable/potato_color.png";
                        }
                        else if (key == "soda")
                        {
                            Soda_Image = "drawable/soda_color.png";
                        }
                        else if (key == "sweets")
                        {
                            Sweet_Image = "drawable/sweets_color.png";
                        }
                        else if (key == "vegetables")
                        {
                            Vegetable_Image = "drawable/vegetables_color.png";
                        }
                        else
                        {
                            //Key out of bounds???
                        }
                        Selected_Food.Add(key);
                    }
                });                
            }
            catch (Exception ex)
            {
                App.Current.MainPage.DisplayAlert("UserMealINC_vm 1!", ex.Message, "OK");
            }
        }
TigerLionCheetah
  • 178
  • 3
  • 19
  • It depends on what kind of crashes you are experiencing. Care to elaborate a bit more? – Cheesebaron Aug 10 '17 at 13:54
  • What is the error? What does the code look like? What line does it crash on? – hvaughan3 Aug 10 '17 at 13:54
  • @hvaughan3 i have edited my question. – TigerLionCheetah Aug 10 '17 at 14:28
  • @Cheesebaron I have edited my question – TigerLionCheetah Aug 10 '17 at 14:28
  • Usually you would get a stack trace or a sigsegv or something indicating where it went wrong. Check your output window or check logcat on the device. – Cheesebaron Aug 10 '17 at 14:30
  • I have created a log from the Android Device Log and pasted it in this text file - https://docs.google.com/document/d/1RAE_KKSY2esRQhQaKjwUooyNCsg0PgLIgOfMs46lVYo/edit?usp=drivesdk – TigerLionCheetah Aug 10 '17 at 14:38
  • 1
    Pages 156 to 160 have what seems like is the stack trace, though it does not look like it is specifying a specific line in your code. It is indeed running out of memory due to an image being loaded into a `Button`. Do you have any images being loaded into `Button`s? Perhaps the image is way too big or you have too many of the `Button`s on the screen at once. – hvaughan3 Aug 10 '17 at 14:46
  • @hvaughan3 yes i have many buttons with images on them, they are pretty small tho, like 50x50 and 40x40. What could be a roundabout for these? Should i change the button to images and have a on-click function attached to them? – TigerLionCheetah Aug 10 '17 at 14:50
  • 1
    Perhaps you could try compressing the images or caching them? It is hard to say without seeing any code. [Xamarin](https://developer.xamarin.com/recipes/android/resources/general/load_large_bitmaps_efficiently/) has a post dedicated to image loading in Android since this is a common problem, so you might start there. If you are using Xamarin Forms, you might also check out [FFImageLoading](https://github.com/luberda-molinet/FFImageLoading) plugin and use that to load the images since it will cache images and can be configured to compress them to the size of the control. – hvaughan3 Aug 10 '17 at 15:06
  • 1
    I can try caching, but i am still confused on the memory because the size of these images are around 1.6KB. Ad there are like only 20 of them. Maybe it's how i am manipulating these images is whats inefficient. I will look into the links you have attached. By the way, thanks a lot :) – TigerLionCheetah Aug 10 '17 at 15:13
  • Happy to help out a little bit at least. I do not have enough experience with the problem to know much more. Hopefully someone else will be able to help you more with this. Although without seeing code, they may not be able to do much. – hvaughan3 Aug 10 '17 at 15:42
  • 1
    @hvaughan3 "Happy to help out a little bit at least"...Yeah, you solved it for me lol. I was able to use this link (https://stackoverflow.com/questions/41184166/java-lang-outofmemoryerror-failed-to-allocate-a-allocation-until-oom) and add a few lines in the manifest lines and the app doesn't crash anymore. Appreciate your help – TigerLionCheetah Aug 10 '17 at 15:47
  • Haha awesome!! I would suggest answering your own question and marking it as such for anyone else who comes along with a similar issue. – hvaughan3 Aug 10 '17 at 18:37

1 Answers1

1

It turns out that the device memory was getting overflowed by all the multiple images i had in my App. (As suggested by @hvaughan3)

This link has the answer to it.

You basically just have to add these two lines in you Android Manifest (Under Application) -

android:hardwareAccelerated="false"
android:largeHeap="true"

TigerLionCheetah
  • 178
  • 3
  • 19