1. Are you ready for the Galaxy S20? Here is everything we know so far!

Getting OutOfMemoryError when dealing with images

Discussion in 'Android Development' started by robrod14, Jun 7, 2017.

  1. robrod14

    robrod14 Newbie
    Thread Starter

    I am new to Android Studio and this is my first attempt at making an app. I'm making a card game, and the app initially loads fine. As i tap the deck a card is delt. If i tap the deck again a new card is delt and placed on top of the card shown before.

    Here is the code within main that listens for a click and then calls another method to get the image.

    Code (Java):
    1. player1Deck.setOnClickListener(new View.OnClickListener(){
    2.  
    3.             public void onClick(View view){
    4.                 int deal = 0;
    5.                 PlayingCards c = null;
    6.                 String name;
    7.  
    8.                 deal = getRandom();
    9.                 c = b.get(deal);
    10.                 name = c.getName2();
    11.                 pc.assignImages(name, player1Empty);
    12.  
    13.                 //flip player 1 cards
    14.                 /*pc.assignImages(pc.cards.get(countPlayer1), player1Empty);*/
    15.  
    16.                 player1Empty.setVisibility(View.VISIBLE);
    17.                 countPlayer1++;
    18.             }
    19.         });
    This is the code from assignImages():
    Code (Java):
    1. public void assignImages (String card, ImageView image) {
    2.         switch(card){
    3.             //Clubs
    4.             case "clubs2":
    5.                 image.setImageResource(R.drawable.c2clubs);
    6.                 break;
    7.             case "clubs3":
    8.                 image.setImageResource(R.drawable.c3clubs);
    9.                 break;
    10.             case "clubs4":
    11.                 image.setImageResource(R.drawable.c4clubs);
    12.                 break;
    13.             case "clubs5":
    14.                 image.setImageResource(R.drawable.c5clubs);
    15.                 break;
    16.             case "clubs6":
    17.                 image.setImageResource(R.drawable.c6clubs);
    18.                 break;
    19.             case "clubs7":
    20.                 image.setImageResource(R.drawable.c7clubs);
    21.                 break;
    22.             case "clubs8":
    23.                 image.setImageResource(R.drawable.c8clubs);
    24.                 break;
    25.             case "clubs9":
    26.                 image.setImageResource(R.drawable.c9clubs);
    27.                 break;
    28.             case "clubs10":
    29.                 image.setImageResource(R.drawable.c10clubs);
    30.                 break;
    31.             case "clubs11":
    32.                 image.setImageResource(R.drawable.jackclubs);
    33.                 break;
    34.             case "clubs12":
    35.                 image.setImageResource(R.drawable.queenclubs);
    36.                 break;
    37.             case "clubs13":
    38.                 image.setImageResource(R.drawable.kingclubs);
    39.                 break;
    40.             case "clubs14":
    41.                 image.setImageResource(R.drawable.aceclub);
    42.                 break;
    43.           }
    44. }

    This is the error I am getting:

    Code (Text):
    1. java.lang.OutOfMemoryError: Failed to allocate a 245000012 byte allocation with 16777216 free bytes and 196MB until OOM
    2.                                                                                     at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
    3.                                                                                     at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
    4.                                                                                     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:700)
    5.                                                                                     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:535)
    6.                                                                                     at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1179)
    7.                                                                                     at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:770)
    8.                                                                                     at android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:621)
    9.                                                                                     at android.content.res.Resources.getDrawable(Resources.java:1642)
    10.                                                                                     at android.content.Context.getDrawable(Context.java:525)
    11.                                                                                     at android.support.v4.content.ContextCompatApi21.getDrawable(ContextCompatApi21.java:30)
    12.                                                                                     at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:372)
    13.                                                                                     at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:202)
    14.                                                                                     at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:190)
    15.                                                                                     at android.support.v7.content.res.AppCompatResources.getDrawable(AppCompatResources.java:100)
    16.                                                                                     at android.support.v7.widget.AppCompatImageHelper.setImageResource(AppCompatImageHelper.java:73)
    17.                                                                                     at android.support.v7.widget.AppCompatImageView.setImageResource(AppCompatImageView.java:81)
    18.                                                                                     at com.example.robertrodriquez.spit.PlayingCards.assignImages(PlayingCards.java:173)
    19.                                                                                     at com.example.robertrodriquez.spit.MainActivity$1.onClick(MainActivity.java:67)
    20.                                                                                     at android.view.View.performClick(View.java:6261)
    21.                                                                                     at android.view.View$PerformClick.run(View.java:23748)
    22.                                                                                     at android.os.Handler.handleCallback(Handler.java:751)
    23.                                                                                     at android.os.Handler.dispatchMessage(Handler.java:95)
    24.                                                                                     at android.os.Looper.loop(Looper.java:154)
    25.                                                                                     at android.app.ActivityThread.main(ActivityThread.java:6776)
    26.                                                                                     at java.lang.reflect.Method.invoke(Native Method)
    27.                                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
    28.                                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
    29.  

    I have searched the internet and they talk about using bitmap and trying to show preview of the images instead of the full image. I figured this would resolve my issue, I was trying to use the following code

    Code (Java):
    1. private static Bitmap decodeResource(Resources res, int id) {
    2.         Bitmap bitmap = null;
    3.         BitmapFactory.Options options = new BitmapFactory.Options();
    4.         for (options.inSampleSize = 1; options.inSampleSize <= 32; options.inSampleSize++) {
    5.             try {
    6.                 bitmap = BitmapFactory.decodeResource(res, id, options);
    7.                 Log.d("hereiam", "Decoded successfully for sampleSize " + options.inSampleSize);
    8.                 break;
    9.             } catch (OutOfMemoryError outOfMemoryError) {
    10.                 // If an OutOfMemoryError occurred, we continue with for loop and next inSampleSize value
    11.                 Log.e("heriam", "outOfMemoryError while reading file for sampleSize " + options.inSampleSize
    12.                         + " retrying with higher value");
    13.             }
    14.         }
    15.         return bitmap;
    But when i change my assignImages method from above to include :

    Code (Java):
    1. public void assignImages (String card, ImageView image) {
    2.         switch(card){
    3.             //Clubs
    4.             case "clubs1":
    5.                 image.setImageBitmap(decodeResource(getResources(), R.drawable.c2clubs));
    6.            
    I can't get "getResources()" to resolve. It looks like i have to create a getResources() method but i'm not sure what to put inside this method. I've searched and found out the getResources should return a resource which makes sense but i'm still not sure of the code that should go in this method. The decodeResource() method already has the id of the image i want with "R.drawable.c2clubs".

    If someone could help me figure out what i'm missing it would be appreciated.

    Thank You
     


    #1 robrod14, Jun 7, 2017
    Last edited: Jun 7, 2017
    sorinboss580 likes this.

    1. Download the Forums for Android™ app!


      Download

       
  2. Deleted User

    Deleted User Guest

    Hi and welcome to Android Forums.
    Please include your code, and the output from the Logcat, showing the stack trace.
     
    scary alien likes this.
  3. robrod14

    robrod14 Newbie
    Thread Starter

    Yea i submitted changes on accident before finalizing my post.
     
    scary alien likes this.
  4. wseng92

    wseng92 Android Enthusiast

    Use
    Code (Text):
    1. getApplicationContext().getResources()
     
  5. robrod14

    robrod14 Newbie
    Thread Starter

    That allowed getResources() to resolve but now "getApplicationContext()" will not resolve.

    This is what i tried based off of your suggestion.

    Code (Text):
    1.  
    2. public void assignImages (String card, ImageView image) {
    3.     switch(card){
    4.         //Clubs
    5.         case "clubs1":
    6.             image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.c1clubs));
    7.  
     
  6. wseng92

    wseng92 Android Enthusiast

    Your activity extends to AppCompatActivity or Fragment?
     
  7. robrod14

    robrod14 Newbie
    Thread Starter

    ok i made the change and did extends AppCompatActivity. It got rid of any compile errors. Now that i have added your information i'm getting

    java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference

    I will paste my code below
     
  8. robrod14

    robrod14 Newbie
    Thread Starter

    Code (Text):
    1.  
    2. package com.example.robertrodriquez.spit;
    3.  
    4. import android.content.res.Resources;
    5. import android.os.Bundle;
    6. import android.support.v4.app.Fragment;
    7. import android.support.v7.app.AppCompatActivity;
    8. import android.util.Log;
    9. import android.view.LayoutInflater;
    10. import android.view.View;
    11. import android.view.ViewGroup;
    12. import android.widget.ImageView;
    13.  
    14. import java.util.ArrayList;
    15. import java.util.Random;
    16. import android.graphics.Bitmap;
    17. import android.graphics.BitmapFactory;
    18.  
    19. import static android.support.v4.content.res.ResourcesCompat.getDrawable;
    20.  
    21. /**
    22. * Created by robertrodriquez on 6/5/17.
    23. */
    24.  
    25. public class PlayingCards extends AppCompatActivity {
    26.  
    27.     int suitLow = 1;
    28.     int suitHigh = 5;
    29.     int cardLow = 2;
    30.     int cardHigh = 15;
    31.     int suit;
    32.     int card;
    33.  
    34.  
    35.     private String suit2;
    36.     private int number2;
    37.     private String name2;
    38.  
    39.  
    40.     public PlayingCards(){
    41.  
    42.     }
    43.  
    44.     public PlayingCards(String name2, String suit2, int number2){
    45.  
    46.         this.name2 = name2;
    47.         this.suit2 = suit2;
    48.         this.number2 = number2;
    49.  
    50.     }
    51.  
    52.  
    53.     public String getSuit2(){
    54.  
    55.         return this.suit2;
    56.     }
    57.  
    58.     public int getNumber2(){
    59.  
    60.         return this.number2;
    61.     }
    62.  
    63.  
    64.     public String getName2(){
    65.  
    66.         return this.name2;
    67.     }
    68.  
    69.  
    70.     /*ArrayList<PlayingCards> cards = new ArrayList();*/
    71.  
    72.     public int[] createCard() {
    73.  
    74.         //creating a random suit
    75.         Random r = new Random();
    76.         suit = r.nextInt(suitHigh - suitLow) + suitLow;
    77.  
    78.         //creating a random card
    79.         Random r2 = new Random();
    80.         card = r2.nextInt(cardHigh - cardLow) + cardLow;
    81.         return new int[] {suit, card};
    82.     }
    83.  
    84.  
    85.     public void assignImages (String card, ImageView image) {
    86.         switch(card){
    87.             //Clubs
    88.             case "clubs2":
    89.                 image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.c2clubs));
    90.                 break;
    91.             case "clubs3":
    92.                 image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.c3clubs));
    93.                 break;
    94.             case "clubs4":
    95.                 image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.c4clubs));
    96.                 break;
    97.             case "clubs5":
    98.                 image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.c5clubs));
    99.                 break;
    100.             case "clubs6":
    101.                 image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.c6clubs));
    102.                 break;
    103.             case "clubs7":
    104.                 image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.c7clubs));
    105.                 break;
    106.             case "clubs8":
    107.                 image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.c8clubs));
    108.                 break;
    109.             case "clubs9":
    110.                 image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.c9clubs));
    111.                 break;
    112.             case "clubs10":
    113.                 image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.c10clubs));
    114.                 break;
    115.             case "clubs11":
    116.                 image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.jackclubs));
    117.                 break;
    118.             case "clubs12":
    119.                 image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.queenclubs));
    120.                 break;
    121.             case "clubs13":
    122.                 image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.kingclubs));
    123.                 break;
    124.             case "clubs14":
    125.                 image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.aceclub));
    126.                 break;
    127.  
    128.             //Diamonds
    129.             case "diamonds2":
    130.                 image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.c2diamond));
    131.                 break;
    132.             case "diamonds3":
    133.                 image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.c3diamond));
    134.                 break;
    135.             case "diamonds4":
    136.                 image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.c4diamond));
    137.                 break;
    138.             case "diamonds5":
    139.                 image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.c5diamond));
    140.                 break;
    141.             case "diamonds6":
    142.                 image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.c6diamond));
    143.                 break;
    144.             case "diamonds7":
    145.                 image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.c7diamond));
    146.                 break;
    147.             case "diamonds8":
    148.                 image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.c8diamond));
    149.                 break;
    150.             case "diamonds9":
    151.                 image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.c9diamond));
    152.                 break;
    153.             case "diamonds10":
    154.                 image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.c10diamond));
    155.                 break;
    156.             case "diamonds11":
    157.                 image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.jackdiamonds));
    158.                 break;
    159.             case "diamonds12":
    160.                 image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.queendiamonds));
    161.                 break;
    162.             case "diamonds13":
    163.                 image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.kingdiamonds));
    164.                 break;
    165.             case "diamonds14":
    166.                 image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.acediamond));
    167.                 break;
    168.  
    169.             //Hearts
    170.             case "hearts2":
    171.                 image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.c2hearts));
    172.                 break;
    173.             case "hearts3":
    174.                 image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.c3hearts));
    175.                 break;
    176.             case "hearts4":
    177.                 image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.c4hearts));
    178.                 break;
    179.             case "hearts5":
    180.                 image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.c5hearts));
    181.                 break;
    182.             case "hearts6":
    183.                 image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.c6hearts));
    184.                 break;
    185.             case "hearts7":
    186.                 image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.c7hearts));
    187.                 break;
    188.             case "hearts8":
    189.                 image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.c8hearts));
    190.                 break;
    191.             case "hearts9":
    192.                 image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.c9hearts));
    193.                 break;
    194.             case "hearts10":
    195.                 image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.c10hearts));
    196.                 break;
    197.             case "hearts11":
    198.                 image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.jackhearts));
    199.                 break;
    200.             case "hearts12":
    201.                 image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.queenhearts));
    202.                 break;
    203.             case "hearts13":
    204.                 image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.kinghearts));
    205.                 break;
    206.             case "hearts14":
    207.                 image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.acehearts));
    208.                 break;
    209.  
    210.             //Spades
    211.             case "spades2":
    212.                 image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.c2spade));
    213.                 break;
    214.             case "spades3":
    215.                 image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.c3spade));
    216.                 break;
    217.             case "spades4":
    218.                 image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.c4spade));
    219.                 break;
    220.             case "spades5":
    221.                 image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.c5spade));
    222.                 break;
    223.             case "spades6":
    224.                 image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.c6spade));
    225.                 break;
    226.             case "spades7":
    227.                 image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.c7spade));
    228.                 break;
    229.             case "spades8":
    230.                 image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.c8spade));
    231.                 break;
    232.             case "spades9":
    233.                 image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.c9spade));
    234.                 break;
    235.             case "spades10":
    236.                 image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.c10spade));
    237.                 break;
    238.             case "spades11":
    239.                 image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.jackspade));
    240.                 break;
    241.             case "spades12":
    242.                 image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.queenspade));
    243.                 break;
    244.             case "spades13":
    245.                 image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.kingspades));
    246.                 break;
    247.             case "spades14":
    248.                 image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.acespade));
    249.                 break;
    250.         }
    251.     }
    252.  
    253.  
    254.  
    255.     /*public void assignImages (String card, ImageView image) {
    256.         switch(card){
    257.             //Clubs
    258.    
    259.             case "clubs2":
    260.                 image.setImageResource(R.drawable.c2clubs);
    261.                 break;
    262.             case "clubs3":
    263.                 image.setImageResource(R.drawable.c3clubs);
    264.                 break;
    265.             case "clubs4":
    266.                 image.setImageResource(R.drawable.c4clubs);
    267.                 break;
    268.             case "clubs5":
    269.                 image.setImageResource(R.drawable.c5clubs);
    270.                 break;
    271.             case "clubs6":
    272.                 image.setImageResource(R.drawable.c6clubs);
    273.                 break;
    274.             case "clubs7":
    275.                 image.setImageResource(R.drawable.c7clubs);
    276.                 break;
    277.             case "clubs8":
    278.                 image.setImageResource(R.drawable.c8clubs);
    279.                 break;
    280.             case "clubs9":
    281.                 image.setImageResource(R.drawable.c9clubs);
    282.                 break;
    283.             case "clubs10":
    284.                 image.setImageResource(R.drawable.c10clubs);
    285.                 break;
    286.             case "clubs11":
    287.                 image.setImageResource(R.drawable.jackclubs);
    288.                 break;
    289.             case "clubs12":
    290.                 image.setImageResource(R.drawable.queenclubs);
    291.                 break;
    292.             case "clubs13":
    293.                 image.setImageResource(R.drawable.kingclubs);
    294.                 break;
    295.             case "clubs14":
    296.                 image.setImageResource(R.drawable.aceclub);
    297.                 break;
    298.  
    299.             //Diamonds
    300.             case "diamonds2":
    301.                 image.setImageResource(R.drawable.c2diamond);
    302.                 break;
    303.             case "diamonds3":
    304.                 image.setImageResource(R.drawable.c3diamond);
    305.                 break;
    306.             case "diamonds4":
    307.                 image.setImageResource(R.drawable.c4diamond);
    308.                 break;
    309.             case "diamonds5":
    310.                 image.setImageResource(R.drawable.c5diamond);
    311.                 break;
    312.             case "diamonds6":
    313.                 image.setImageResource(R.drawable.c6diamond);
    314.                 break;
    315.             case "diamonds7":
    316.                 image.setImageResource(R.drawable.c7diamond);
    317.                 break;
    318.             case "diamonds8":
    319.                 image.setImageResource(R.drawable.c8diamond);
    320.                 break;
    321.             case "diamonds9":
    322.                 image.setImageResource(R.drawable.c9diamond);
    323.                 break;
    324.             case "diamonds10":
    325.                 image.setImageResource(R.drawable.c10diamond);
    326.                 break;
    327.             case "diamonds11":
    328.                 image.setImageResource(R.drawable.jackdiamonds);
    329.                 break;
    330.             case "diamonds12":
    331.                 image.setImageResource(R.drawable.queendiamonds);
    332.                 break;
    333.             case "diamonds13":
    334.                 image.setImageResource(R.drawable.kingdiamonds);
    335.                 break;
    336.             case "diamonds14":
    337.                 image.setImageResource(R.drawable.acediamond);
    338.                 break;
    339.  
    340.             //Hearts
    341.             case "hearts2":
    342.                 image.setImageResource(R.drawable.c2hearts);
    343.                 break;
    344.             case "hearts3":
    345.                 image.setImageResource(R.drawable.c3hearts);
    346.                 break;
    347.             case "hearts4":
    348.                 image.setImageResource(R.drawable.c4hearts);
    349.                 break;
    350.             case "hearts5":
    351.                 image.setImageResource(R.drawable.c5hearts);
    352.                 break;
    353.             case "hearts6":
    354.                 image.setImageResource(R.drawable.c6hearts);
    355.                 break;
    356.             case "hearts7":
    357.                 image.setImageResource(R.drawable.c7hearts);
    358.                 break;
    359.             case "hearts8":
    360.                 image.setImageResource(R.drawable.c8hearts);
    361.                 break;
    362.             case "hearts9":
    363.                 image.setImageResource(R.drawable.c9hearts);
    364.                 break;
    365.             case "hearts10":
    366.                 image.setImageResource(R.drawable.c10hearts);
    367.                 break;
    368.             case "hearts11":
    369.                 image.setImageResource(R.drawable.jackhearts);
    370.                 break;
    371.             case "hearts12":
    372.                 image.setImageResource(R.drawable.queenhearts);
    373.                 break;
    374.             case "hearts13":
    375.                 image.setImageResource(R.drawable.kinghearts);
    376.                 break;
    377.             case "hearts14":
    378.                 image.setImageResource(R.drawable.acehearts);
    379.                 break;
    380.  
    381.             //Spades
    382.             case "spades2":
    383.                 image.setImageResource(R.drawable.c2spade);
    384.                 break;
    385.             case "spades3":
    386.                 image.setImageResource(R.drawable.c3spade);
    387.                 break;
    388.             case "spades4":
    389.                 image.setImageResource(R.drawable.c4spade);
    390.                 break;
    391.             case "spades5":
    392.                 image.setImageResource(R.drawable.c5spade);
    393.                 break;
    394.             case "spades6":
    395.                 image.setImageResource(R.drawable.c6spade);
    396.                 break;
    397.             case "spades7":
    398.                 image.setImageResource(R.drawable.c7spade);
    399.                 break;
    400.             case "spades8":
    401.                 image.setImageResource(R.drawable.c8spade);
    402.                 break;
    403.             case "spades9":
    404.                 image.setImageResource(R.drawable.c9spade);
    405.                 break;
    406.             case "spades10":
    407.                 image.setImageResource(R.drawable.c10spade);
    408.                 break;
    409.             case "spades11":
    410.                 image.setImageResource(R.drawable.jackspade);
    411.                 break;
    412.             case "spades12":
    413.                 image.setImageResource(R.drawable.queenspade);
    414.                 break;
    415.             case "spades13":
    416.                 image.setImageResource(R.drawable.kingspades);
    417.                 break;
    418.             case "spades14":
    419.                 image.setImageResource(R.drawable.acespade);
    420.                 break;
    421.         }
    422.     }*/
    423.  
    424.  
    425.  
    426.     /*public void assignImages (String card, ImageView image) {
    427.         switch(card){
    428.             //Clubs
    429.  
    430.             case "clubs2":
    431.                 image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c2clubs));
    432.                 break;
    433.             case "clubs3":
    434.                 image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c3clubs));
    435.                 break;
    436.             case "clubs4":
    437.                 image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c4clubs));
    438.                 break;
    439.             case "clubs5":
    440.                 image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c5clubs));
    441.                 break;
    442.             case "clubs6":
    443.                 image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c6clubs));
    444.                 break;
    445.             case "clubs7":
    446.                 image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c7clubs));
    447.                 break;
    448.             case "clubs8":
    449.                 image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c8clubs));
    450.                 break;
    451.             case "clubs9":
    452.                 image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c9clubs));
    453.                 break;
    454.             case "clubs10":
    455.                 image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c10clubs));
    456.                 break;
    457.             case "clubs11":
    458.                 image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.jackclubs));
    459.                 break;
    460.             case "clubs12":
    461.                 image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.queenclubs));
    462.                 break;
    463.             case "clubs13":
    464.                 image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.kingclubs));
    465.                 break;
    466.             case "clubs14":
    467.                 image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.aceclub));
    468.                 break;
    469.  
    470.             //Diamonds
    471.             case "diamonds2":
    472.                 image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c2diamond));
    473.                 break;
    474.             case "diamonds3":
    475.                 image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c3diamond));
    476.                 break;
    477.             case "diamonds4":
    478.                 image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c4diamond));
    479.                 break;
    480.             case "diamonds5":
    481.                 image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c5diamond));
    482.                 break;
    483.             case "diamonds6":
    484.                 image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c6diamond));
    485.                 break;
    486.             case "diamonds7":
    487.                 image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c7diamond));
    488.                 break;
    489.             case "diamonds8":
    490.                 image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c8diamond));
    491.                 break;
    492.             case "diamonds9":
    493.                 image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c9diamond));
    494.                 break;
    495.             case "diamonds10":
    496.                 image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c10diamond));
    497.                 break;
    498.             case "diamonds11":
    499.                 image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.jackdiamonds));
    500.                 break;
    501.             case "diamonds12":
    502.                 image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.queendiamonds));
    503.                 break;
    504.             case "diamonds13":
    505.                 image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.kingdiamonds));
    506.                 break;
    507.             case "diamonds14":
    508.                 image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.acediamond));
    509.                 break;
    510.  
    511.             //Hearts
    512.             case "hearts2":
    513.                 image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c2hearts));
    514.                 break;
    515.             case "hearts3":
    516.                 image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c3hearts));
    517.                 break;
    518.             case "hearts4":
    519.                 image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c4hearts));
    520.                 break;
    521.             case "hearts5":
    522.                 image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c5hearts));
    523.                 break;
    524.             case "hearts6":
    525.                 image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c6hearts));
    526.                 break;
    527.             case "hearts7":
    528.                 image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c7hearts));
    529.                 break;
    530.             case "hearts8":
    531.                 image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c8hearts));
    532.                 break;
    533.             case "hearts9":
    534.                 image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c9hearts));
    535.                 break;
    536.             case "hearts10":
    537.                 image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c10hearts));
    538.                 break;
    539.             case "hearts11":
    540.                 image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.jackhearts));
    541.                 break;
    542.             case "hearts12":
    543.                 image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.queenhearts));
    544.                 break;
    545.             case "hearts13":
    546.                 image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.kinghearts));
    547.                 break;
    548.             case "hearts14":
    549.                 image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.acehearts));
    550.                 break;
    551.  
    552.             //Spades
    553.             case "spades2":
    554.                 image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c2spade));
    555.                 break;
    556.             case "spades3":
    557.                 image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c3spade));
    558.                 break;
    559.             case "spades4":
    560.                 image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c4spade));
    561.                 break;
    562.             case "spades5":
    563.                 image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c5spade));
    564.                 break;
    565.             case "spades6":
    566.                 image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c6spade));
    567.                 break;
    568.             case "spades7":
    569.                 image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c7spade));
    570.                 break;
    571.             case "spades8":
    572.                 image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c8spade));
    573.                 break;
    574.             case "spades9":
    575.                 image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c9spade));
    576.                 break;
    577.             case "spades10":
    578.                 image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c10spade));
    579.                 break;
    580.             case "spades11":
    581.                 image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.jackspade));
    582.                 break;
    583.             case "spades12":
    584.                 image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.queenspade));
    585.                 break;
    586.             case "spades13":
    587.                 image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.kingspades));
    588.                 break;
    589.             case "spades14":
    590.                 image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.acespade));
    591.                 break;
    592.         }
    593.     }*/
    594.  
    595.  
    596.     private static Bitmap decodeResource(Resources res, int id) {
    597.         Bitmap bitmap = null;
    598.         BitmapFactory.Options options = new BitmapFactory.Options();
    599.         for (options.inSampleSize = 1; options.inSampleSize <= 32; options.inSampleSize++) {
    600.             try {
    601.                 bitmap = BitmapFactory.decodeResource(res, id, options);
    602.                 Log.d("hereiam", "Decoded successfully for sampleSize " + options.inSampleSize);
    603.                 break;
    604.             } catch (OutOfMemoryError outOfMemoryError) {
    605.                 // If an OutOfMemoryError occurred, we continue with for loop and next inSampleSize value
    606.                 Log.e("heriam", "outOfMemoryError while reading file for sampleSize " + options.inSampleSize
    607.                         + " retrying with higher value");
    608.             }
    609.         }
    610.         return bitmap;
    611.     }
    612. }
    613.  
    614.  
     
  9. robrod14

    robrod14 Newbie
    Thread Starter

    Code (Text):
    1.  
    2. package com.example.robertrodriquez.spit;
    3.  
    4. import android.support.v4.content.res.TypedArrayUtils;
    5. import android.support.v7.app.AppCompatActivity;
    6. import android.os.Bundle;
    7. import android.util.Log;
    8. import android.view.View;
    9. import android.widget.ImageView;
    10.  
    11.  
    12.  
    13. //import android.widget.TextView;
    14.  
    15. import java.lang.reflect.Array;
    16. import java.util.ArrayList;
    17. import java.util.Arrays;
    18. import java.util.Collections;
    19. import java.util.Random;
    20.  
    21. public class MainActivity extends AppCompatActivity {
    22.  
    23.     ImageView player1Deck, player2Deck, player1Empty, player2Empty;
    24.  
    25.     int countPlayer1 = 0;
    26.     int[] drawCard;
    27.     PlayingCards pc = new PlayingCards();
    28.     int[] deck;
    29.  
    30.     ArrayList<PlayingCards> a = new ArrayList<>();
    31.     ArrayList<PlayingCards> b = new ArrayList<PlayingCards>();
    32.     ArrayList<Integer> cardsCalled = new ArrayList<>();
    33.  
    34.     int randHigh = 52;
    35.     int randLow = 0;
    36.     int deckIndex = 0;
    37.  
    38.     @Override
    39.     protected void onCreate(Bundle savedInstanceState) {
    40.         super.onCreate(savedInstanceState);
    41.         setContentView(R.layout.activity_main);
    42.  
    43.     // Example of a call to a native method
    44.     //TextView tv = (TextView) findViewById(R.id.sample_text);
    45.     //tv.setText(stringFromJNI());
    46.  
    47.         player1Deck = (ImageView) findViewById(R.id.player1Deck);
    48.         player2Deck = (ImageView) findViewById(R.id.player2Deck);
    49.         player1Empty = (ImageView) findViewById(R.id.player1Empty);
    50.         player2Empty = (ImageView) findViewById(R.id.player2Empty);
    51.  
    52.         player1Empty.setVisibility(View.VISIBLE);
    53.         player2Empty.setVisibility(View.VISIBLE);
    54.  
    55.         b = deck();
    56.  
    57.  
    58.         player1Deck.setOnClickListener(new View.OnClickListener(){
    59.  
    60.             public void onClick(View view){
    61.                 int deal = 0;
    62.                 PlayingCards c = null;
    63.                 String name;
    64.  
    65.                 deal = getRandom();
    66.                 c = b.get(deal);
    67.                 name = c.getName2();
    68.                 pc.assignImages(name, player1Empty);
    69.  
    70.                 //flip player 1 cards
    71.                 /*pc.assignImages(pc.cards.get(countPlayer1), player1Empty);*/
    72.  
    73.                 player1Empty.setVisibility(View.VISIBLE);
    74.                 countPlayer1++;
    75.             }
    76.         });
    77.  
    78.  
    79.  
    80.         //player2Deck.setOnClickListener(new View.OnClickListener());
    81.  
    82.     }
    83.  
    84.  
    85.  
    86.  
    87.     public ArrayList<PlayingCards> deck(){
    88.  
    89.         for(int n=2; n < 15; n++) {
    90.             PlayingCards c = new PlayingCards("clubs" + n, "clubs", n);
    91.             a.add(c);
    92.         }
    93.  
    94.         for(int n=2; n < 15; n++) {
    95.             PlayingCards c = new PlayingCards("diamonds" + n, "diamonds", n);
    96.             a.add(c);
    97.         }
    98.  
    99.         for(int n=2; n < 15; n++) {
    100.             PlayingCards c = new PlayingCards("hearts" + n, "hearts", n);
    101.             a.add(c);
    102.         }
    103.  
    104.         for(int n=2; n < 15; n++) {
    105.             PlayingCards c = new PlayingCards("spades" + n, "spades", n);
    106.             a.add(c);
    107.         }
    108.  
    109.         return a;
    110.     }
    111.  
    112.  
    113.     public int getRandom(){
    114.  
    115.         Random r = new Random();
    116.         deckIndex = r.nextInt(randHigh-randLow) + randLow;
    117.  
    118.         if (cardsCalled.contains(deckIndex)) {
    119.             getRandom();
    120.         } else {
    121.             cardsCalled.add(deckIndex);
    122.         }
    123.  
    124.         return deckIndex;
    125.     }
    126.  
    127.  
    128.     /**
    129.      * A native method that is implemented by the 'native-lib' native library,
    130.      * which is packaged with this application.
    131.      */
    132.     public native String stringFromJNI();
    133.  
    134.     // Used to load the 'native-lib' library on application startup.
    135.     static {
    136.         System.loadLibrary("native-lib");
    137.     }
    138. }
    139.  
     
  10. Deleted User

    Deleted User Guest

    Please post the stack trace from your Logcat view
     
    wseng92 likes this.
  11. robrod14

    robrod14 Newbie
    Thread Starter

    Code (Text):
    1.  
    2. Runtime: FATAL EXCEPTION: main
    3.                                                                               Process: com.example.robertrodriquez.spit, PID: 870
    4.                                                                               java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
    5.                                                                                   at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:106)
    6.                                                                                   at com.example.robertrodriquez.spit.PlayingCards.assignImages(PlayingCards.java:112)
    7.                                                                                   at com.example.robertrodriquez.spit.MainActivity$1.onClick(MainActivity.java:67)
    8.                                                                                   at android.view.View.performClick(View.java:6261)
    9.                                                                                   at android.view.View$PerformClick.run(View.java:23748)
    10.                                                                                   at android.os.Handler.handleCallback(Handler.java:751)
    11.                                                                                   at android.os.Handler.dispatchMessage(Handler.java:95)
    12.                                                                                   at android.os.Looper.loop(Looper.java:154)
    13.                                                                                   at android.app.ActivityThread.main(ActivityThread.java:6776)
    14.                                                                                   at java.lang.reflect.Method.invoke(Native Method)
    15.                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
    16.                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
    17. 06-15 11:00:48.447 870-882/com.example.robertrodriquez.spit W/art: Suspending all threads took: 11.652ms
    18.  
     
  12. Deleted User

    Deleted User Guest

    You can't create an Activity like this

    Code (Text):
    1.  
    2. PlayingCards pc = new PlayingCards();
    3.  
    Activities are created with an Intent, and initialised by the Android system framework. You never create a new Activity object directly. You're getting a null pointer exception because the PlayingCards Activity has not been initialised correctly, and probably most of its expected member variables are null. Hence the NullPointerException.

    You would benefit from reading up on how to start an Activity, because you've got some basic misunderstandings here.
     
  13. wseng92

    wseng92 Android Enthusiast

    #13 wseng92, Jun 15, 2017
    Last edited: Jun 16, 2017
  14. robrod14

    robrod14 Newbie
    Thread Starter

    You are right i don't have much experience with Java or Android app development. However, I went to https://developer.android.com/reference/android/app/Activity.html and read up on activity. I'm not sure if my playing card should be an activity or not. Documentation says, "Almost all activities interact with the user" so that makes me think it should be an activity. But i'm currently just trying to display 1 card then have another card display (covering up the old card). Wouldn't making that be an activity (52 activities for each card) be over kill?
     
  15. robrod14

    robrod14 Newbie
    Thread Starter

    @wseng92 I only did the extends AppCompatActivity because i thought you were suggesting that is what i should do... I would like to start over because maybe i went at this the wrong way. Instead of looking for help with my original problem i asked for help on implementing a solution that i'm not even sure will work.

    So i have changed my code back to the original. In which the app will work for 1, 2, or 3 cards but eventually i get the following error:
    Code (Text):
    1.  
    2. 06-16 19:37:03.854 15123-15123/? E/AndroidRuntime: FATAL EXCEPTION: main
    3.                                                    Process: com.example.robertrodriquez.spit, PID: 15123
    4.                                                    java.lang.OutOfMemoryError: Failed to allocate a 1622264796 byte allocation with 16777216 free bytes and 171MB until OOM
    5.                                                        at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
    6.                                                        at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
    7.                                                        at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:700)
    8.                                                        at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:535)
    9.                                                        at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1179)
    10.                                                        at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:770)
    11.                                                        at android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:621)
    12.                                                        at android.content.res.Resources.getDrawable(Resources.java:1642)
    13.                                                        at android.content.Context.getDrawable(Context.java:525)
    14.                                                        at android.support.v4.content.ContextCompatApi21.getDrawable(ContextCompatApi21.java:30)
    15.                                                        at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:372)
    16.                                                        at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:202)
    17.                                                        at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:190)
    18.                                                        at android.support.v7.content.res.AppCompatResources.getDrawable(AppCompatResources.java:100)
    19.                                                        at android.support.v7.widget.AppCompatImageHelper.setImageResource(AppCompatImageHelper.java:73)
    20.                                                        at android.support.v7.widget.AppCompatImageView.setImageResource(AppCompatImageView.java:81)
    21.                                                        at com.example.robertrodriquez.spit.PlayingCards.assignImages(PlayingCards.java:379)
    22.                                                        at com.example.robertrodriquez.spit.MainActivity$1.onClick(MainActivity.java:67)
    23.                                                        at android.view.View.performClick(View.java:6261)
    24.                                                        at android.view.View$PerformClick.run(View.java:23748)
    25.                                                        at android.os.Handler.handleCallback(Handler.java:751)
    26.                                                        at android.os.Handler.dispatchMessage(Handler.java:95)
    27.                                                        at android.os.Looper.loop(Looper.java:154)
    28.                                                        at android.app.ActivityThread.main(ActivityThread.java:6776)
    29.                                                        at java.lang.reflect.Method.invoke(Native Method)
    30.                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
    31.                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
    This code below is in main and appears to be running fine
    Code (Text):
    1.  
    2. player1Deck.setOnClickListener(new View.OnClickListener(){
    3.  
    4.     public void onClick(View view){
    5.         int deal = 0;
    6.         PlayingCards c = null;
    7.         String name;
    8.  
    9.         deal = getRandom();
    10.         c = b.get(deal);
    11.         name = c.getName2();
    12.         pc.assignImages(name, player1Empty);
    13.  
    14.         //flip player 1 cards
    15.         /*pc.assignImages(pc.cards.get(countPlayer1), player1Empty);*/
    16.  
    17.         player1Empty.setVisibility(View.VISIBLE);
    18.         countPlayer1++;
    19.     }
    20. });
    pc.assignImages(name, player1Empty); is being called and through the debugger i see it gets executed. It is during this method that i get the OOM error. This method looks like this
    Code (Text):
    1.  
    2. public void assignImages (String card, ImageView image) {
    3.     switch(card){
    4.         //Clubs
    5.  
    6.         case "clubs2":
    7.             image.setImageResource(R.drawable.c2clubs);
    8.             break;
    9.         case "clubs3":
    10.             image.setImageResource(R.drawable.c3clubs);
    11.             break;
    12.         case "clubs4":
    13.             image.setImageResource(R.drawable.c4clubs);
    14.             break;
    15.         case "clubs5":
    16.             image.setImageResource(R.drawable.c5clubs);
    17.             break;
    18.         case "clubs6":
    19.             image.setImageResource(R.drawable.c6clubs);
    20.             break;
    21.         case "clubs7":
    22.             image.setImageResource(R.drawable.c7clubs);
    23.             break;
    24.         case "clubs8":
    25.             image.setImageResource(R.drawable.c8clubs);
    26.             break;
    27.         case "clubs9":
    28.             image.setImageResource(R.drawable.c9clubs);
    29.             break;
    30.         case "clubs10":
    31.             image.setImageResource(R.drawable.c10clubs);
    32.             break;
    33.         case "clubs11":
    34.             image.setImageResource(R.drawable.jackclubs);
    35.             break;
    36.         case "clubs12":
    37.             image.setImageResource(R.drawable.queenclubs);
    38.             break;
    39.         case "clubs13":
    40.             image.setImageResource(R.drawable.kingclubs);
    41.             break;
    42.         case "clubs14":
    43.             image.setImageResource(R.drawable.aceclub);
    44.             break;
    45.  
    46.         //Diamonds
    47.         case "diamonds2":
    48.             image.setImageResource(R.drawable.c2diamond);
    49.             break;
    50.         case "diamonds3":
    51.             image.setImageResource(R.drawable.c3diamond);
    52.             break;
    53.         case "diamonds4":
    54.             image.setImageResource(R.drawable.c4diamond);
    55.             break;
    56.         case "diamonds5":
    57.             image.setImageResource(R.drawable.c5diamond);
    58.             break;
    59.         case "diamonds6":
    60.             image.setImageResource(R.drawable.c6diamond);
    61.             break;
    62.         case "diamonds7":
    63.             image.setImageResource(R.drawable.c7diamond);
    64.             break;
    65.         case "diamonds8":
    66.             image.setImageResource(R.drawable.c8diamond);
    67.             break;
    68.         case "diamonds9":
    69.             image.setImageResource(R.drawable.c9diamond);
    70.             break;
    71.         case "diamonds10":
    72.             image.setImageResource(R.drawable.c10diamond);
    73.             break;
    74.         case "diamonds11":
    75.             image.setImageResource(R.drawable.jackdiamonds);
    76.             break;
    77.         case "diamonds12":
    78.             image.setImageResource(R.drawable.queendiamonds);
    79.             break;
    80.         case "diamonds13":
    81.             image.setImageResource(R.drawable.kingdiamonds);
    82.             break;
    83.         case "diamonds14":
    84.             image.setImageResource(R.drawable.acediamond);
    85.             break;
    86.  
    87.         //Hearts
    88.         case "hearts2":
    89.             image.setImageResource(R.drawable.c2hearts);
    90.             break;
    91.         case "hearts3":
    92.             image.setImageResource(R.drawable.c3hearts);
    93.             break;
    94.         case "hearts4":
    95.             image.setImageResource(R.drawable.c4hearts);
    96.             break;
    97.         case "hearts5":
    98.             image.setImageResource(R.drawable.c5hearts);
    99.             break;
    100.         case "hearts6":
    101.             image.setImageResource(R.drawable.c6hearts);
    102.             break;
    103.         case "hearts7":
    104.             image.setImageResource(R.drawable.c7hearts);
    105.             break;
    106.         case "hearts8":
    107.             image.setImageResource(R.drawable.c8hearts);
    108.             break;
    109.         case "hearts9":
    110.             image.setImageResource(R.drawable.c9hearts);
    111.             break;
    112.         case "hearts10":
    113.             image.setImageResource(R.drawable.c10hearts);
    114.             break;
    115.         case "hearts11":
    116.             image.setImageResource(R.drawable.jackhearts);
    117.             break;
    118.         case "hearts12":
    119.             image.setImageResource(R.drawable.queenhearts);
    120.             break;
    121.         case "hearts13":
    122.             image.setImageResource(R.drawable.kinghearts);
    123.             break;
    124.         case "hearts14":
    125.             image.setImageResource(R.drawable.acehearts);
    126.             break;
    127.  
    128.         //Spades
    129.         case "spades2":
    130.             image.setImageResource(R.drawable.c2spade);
    131.             break;
    132.         case "spades3":
    133.             image.setImageResource(R.drawable.c3spade);
    134.             break;
    135.         case "spades4":
    136.             image.setImageResource(R.drawable.c4spade);
    137.             break;
    138.         case "spades5":
    139.             image.setImageResource(R.drawable.c5spade);
    140.             break;
    141.         case "spades6":
    142.             image.setImageResource(R.drawable.c6spade);
    143.             break;
    144.         case "spades7":
    145.             image.setImageResource(R.drawable.c7spade);
    146.             break;
    147.         case "spades8":
    148.             image.setImageResource(R.drawable.c8spade);
    149.             break;
    150.         case "spades9":
    151.             image.setImageResource(R.drawable.c9spade);
    152.             break;
    153.         case "spades10":
    154.             image.setImageResource(R.drawable.c10spade);
    155.             break;
    156.         case "spades11":
    157.             image.setImageResource(R.drawable.jackspade);
    158.             break;
    159.         case "spades12":
    160.             image.setImageResource(R.drawable.queenspade);
    161.             break;
    162.         case "spades13":
    163.             image.setImageResource(R.drawable.kingspades);
    164.             break;
    165.         case "spades14":
    166.             image.setImageResource(R.drawable.acespade);
    167.             break;
    168.     }
    169. }
    can anyone pinpoint why i am getting the OOM error? I thought it was the size of my images but they are now 4kb in size. I am reassigning the new image to the same imageview thinking that would eliminate me having to destroy each image before putting the new image on top, but of course i could be wrong.
     
Loading...
Similar Threads - Getting OutOfMemoryError dealing
  1. Dougb256
    Replies:
    17
    Views:
    734
  2. ezio aulditore
    Replies:
    2
    Views:
    266
  3. Jerome Jordan
    Replies:
    2
    Views:
    369
  4. CharlesLewis
    Replies:
    3
    Views:
    366
  5. AdiA
    Replies:
    6
    Views:
    397
  6. Tom_gxz
    Replies:
    0
    Views:
    379
  7. My project
    Replies:
    9
    Views:
    560
  8. persistentone
    Replies:
    19
    Views:
    1,500
  9. Milo Williamson
    Replies:
    6
    Views:
    428
  10. Sillycat41
    Replies:
    9
    Views:
    633

Share This Page

Loading...