• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Getting OutOfMemoryError when dealing with images

robrod14

Newbie
Jun 7, 2017
10
2
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.

Java:
player1Deck.setOnClickListener(new View.OnClickListener(){

            public void onClick(View view){
                int deal = 0;
                PlayingCards c = null;
                String name;

                deal = getRandom();
                c = b.get(deal);
                name = c.getName2();
                pc.assignImages(name, player1Empty);

                //flip player 1 cards
                /*pc.assignImages(pc.cards.get(countPlayer1), player1Empty);*/

                player1Empty.setVisibility(View.VISIBLE);
                countPlayer1++;
            }
        });

This is the code from assignImages():
Java:
public void assignImages (String card, ImageView image) {
        switch(card){
            //Clubs
            case "clubs2":
                image.setImageResource(R.drawable.c2clubs);
                break;
            case "clubs3":
                image.setImageResource(R.drawable.c3clubs);
                break;
            case "clubs4":
                image.setImageResource(R.drawable.c4clubs);
                break;
            case "clubs5":
                image.setImageResource(R.drawable.c5clubs);
                break;
            case "clubs6":
                image.setImageResource(R.drawable.c6clubs);
                break;
            case "clubs7":
                image.setImageResource(R.drawable.c7clubs);
                break;
            case "clubs8":
                image.setImageResource(R.drawable.c8clubs);
                break;
            case "clubs9":
                image.setImageResource(R.drawable.c9clubs);
                break;
            case "clubs10":
                image.setImageResource(R.drawable.c10clubs);
                break;
            case "clubs11":
                image.setImageResource(R.drawable.jackclubs);
                break;
            case "clubs12":
                image.setImageResource(R.drawable.queenclubs);
                break;
            case "clubs13":
                image.setImageResource(R.drawable.kingclubs);
                break;
            case "clubs14":
                image.setImageResource(R.drawable.aceclub);
                break;
          }
}


This is the error I am getting:

Code:
java.lang.OutOfMemoryError: Failed to allocate a 245000012 byte allocation with 16777216 free bytes and 196MB until OOM
                                                                                    at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
                                                                                    at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
                                                                                    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:700)
                                                                                    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:535)
                                                                                    at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1179)
                                                                                    at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:770)
                                                                                    at android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:621)
                                                                                    at android.content.res.Resources.getDrawable(Resources.java:1642)
                                                                                    at android.content.Context.getDrawable(Context.java:525)
                                                                                    at android.support.v4.content.ContextCompatApi21.getDrawable(ContextCompatApi21.java:30)
                                                                                    at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:372)
                                                                                    at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:202)
                                                                                    at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:190)
                                                                                    at android.support.v7.content.res.AppCompatResources.getDrawable(AppCompatResources.java:100)
                                                                                    at android.support.v7.widget.AppCompatImageHelper.setImageResource(AppCompatImageHelper.java:73)
                                                                                    at android.support.v7.widget.AppCompatImageView.setImageResource(AppCompatImageView.java:81)
                                                                                    at com.example.robertrodriquez.spit.PlayingCards.assignImages(PlayingCards.java:173)
                                                                                    at com.example.robertrodriquez.spit.MainActivity$1.onClick(MainActivity.java:67)
                                                                                    at android.view.View.performClick(View.java:6261)
                                                                                    at android.view.View$PerformClick.run(View.java:23748)
                                                                                    at android.os.Handler.handleCallback(Handler.java:751)
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                    at android.os.Looper.loop(Looper.java:154)
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:6776)
                                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)


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

Java:
private static Bitmap decodeResource(Resources res, int id) {
        Bitmap bitmap = null;
        BitmapFactory.Options options = new BitmapFactory.Options();
        for (options.inSampleSize = 1; options.inSampleSize <= 32; options.inSampleSize++) {
            try {
                bitmap = BitmapFactory.decodeResource(res, id, options);
                Log.d("hereiam", "Decoded successfully for sampleSize " + options.inSampleSize);
                break;
            } catch (OutOfMemoryError outOfMemoryError) {
                // If an OutOfMemoryError occurred, we continue with for loop and next inSampleSize value
                Log.e("heriam", "outOfMemoryError while reading file for sampleSize " + options.inSampleSize
                        + " retrying with higher value");
            }
        }
        return bitmap;

But when i change my assignImages method from above to include :

Java:
public void assignImages (String card, ImageView image) {
        switch(card){
            //Clubs
            case "clubs1":
                image.setImageBitmap(decodeResource(getResources(), R.drawable.c2clubs));

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
 
Last edited:
  • Like
Reactions: sorinboss580
That allowed getResources() to resolve but now "getApplicationContext()" will not resolve.

This is what i tried based off of your suggestion.

Code:
public void assignImages (String card, ImageView image) {
    switch(card){
        //Clubs
        case "clubs1":
            image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.c1clubs));
 
Upvote 0
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
 
Upvote 0
Code:
package com.example.robertrodriquez.spit;

import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.util.ArrayList;
import java.util.Random;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import static android.support.v4.content.res.ResourcesCompat.getDrawable;

/**
* Created by robertrodriquez on 6/5/17.
*/

public class PlayingCards extends AppCompatActivity {

    int suitLow = 1;
    int suitHigh = 5;
    int cardLow = 2;
    int cardHigh = 15;
    int suit;
    int card;


    private String suit2;
    private int number2;
    private String name2;


    public PlayingCards(){

    }

    public PlayingCards(String name2, String suit2, int number2){

        this.name2 = name2;
        this.suit2 = suit2;
        this.number2 = number2;

    }


    public String getSuit2(){

        return this.suit2;
    }

    public int getNumber2(){

        return this.number2;
    }


    public String getName2(){

        return this.name2;
    }


    /*ArrayList<PlayingCards> cards = new ArrayList();*/

    public int[] createCard() {

        //creating a random suit
        Random r = new Random();
        suit = r.nextInt(suitHigh - suitLow) + suitLow;

        //creating a random card
        Random r2 = new Random();
        card = r2.nextInt(cardHigh - cardLow) + cardLow;
        return new int[] {suit, card};
    }


    public void assignImages (String card, ImageView image) {
        switch(card){
            //Clubs
            case "clubs2":
                image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.c2clubs));
                break;
            case "clubs3":
                image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.c3clubs));
                break;
            case "clubs4":
                image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.c4clubs));
                break;
            case "clubs5":
                image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.c5clubs));
                break;
            case "clubs6":
                image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.c6clubs));
                break;
            case "clubs7":
                image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.c7clubs));
                break;
            case "clubs8":
                image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.c8clubs));
                break;
            case "clubs9":
                image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.c9clubs));
                break;
            case "clubs10":
                image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.c10clubs));
                break;
            case "clubs11":
                image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.jackclubs));
                break;
            case "clubs12":
                image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.queenclubs));
                break;
            case "clubs13":
                image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.kingclubs));
                break;
            case "clubs14":
                image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.aceclub));
                break;

            //Diamonds
            case "diamonds2":
                image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.c2diamond));
                break;
            case "diamonds3":
                image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.c3diamond));
                break;
            case "diamonds4":
                image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.c4diamond));
                break;
            case "diamonds5":
                image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.c5diamond));
                break;
            case "diamonds6":
                image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.c6diamond));
                break;
            case "diamonds7":
                image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.c7diamond));
                break;
            case "diamonds8":
                image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.c8diamond));
                break;
            case "diamonds9":
                image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.c9diamond));
                break;
            case "diamonds10":
                image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.c10diamond));
                break;
            case "diamonds11":
                image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.jackdiamonds));
                break;
            case "diamonds12":
                image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.queendiamonds));
                break;
            case "diamonds13":
                image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.kingdiamonds));
                break;
            case "diamonds14":
                image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.acediamond));
                break;

            //Hearts
            case "hearts2":
                image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.c2hearts));
                break;
            case "hearts3":
                image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.c3hearts));
                break;
            case "hearts4":
                image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.c4hearts));
                break;
            case "hearts5":
                image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.c5hearts));
                break;
            case "hearts6":
                image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.c6hearts));
                break;
            case "hearts7":
                image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.c7hearts));
                break;
            case "hearts8":
                image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.c8hearts));
                break;
            case "hearts9":
                image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.c9hearts));
                break;
            case "hearts10":
                image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.c10hearts));
                break;
            case "hearts11":
                image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.jackhearts));
                break;
            case "hearts12":
                image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.queenhearts));
                break;
            case "hearts13":
                image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.kinghearts));
                break;
            case "hearts14":
                image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.acehearts));
                break;

            //Spades
            case "spades2":
                image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.c2spade));
                break;
            case "spades3":
                image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.c3spade));
                break;
            case "spades4":
                image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.c4spade));
                break;
            case "spades5":
                image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.c5spade));
                break;
            case "spades6":
                image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.c6spade));
                break;
            case "spades7":
                image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.c7spade));
                break;
            case "spades8":
                image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.c8spade));
                break;
            case "spades9":
                image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.c9spade));
                break;
            case "spades10":
                image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.c10spade));
                break;
            case "spades11":
                image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.jackspade));
                break;
            case "spades12":
                image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.queenspade));
                break;
            case "spades13":
                image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.kingspades));
                break;
            case "spades14":
                image.setImageBitmap(decodeResource(getApplicationContext().getResources(), R.drawable.acespade));
                break;
        }
    }



    /*public void assignImages (String card, ImageView image) {
        switch(card){
            //Clubs
   
            case "clubs2":
                image.setImageResource(R.drawable.c2clubs);
                break;
            case "clubs3":
                image.setImageResource(R.drawable.c3clubs);
                break;
            case "clubs4":
                image.setImageResource(R.drawable.c4clubs);
                break;
            case "clubs5":
                image.setImageResource(R.drawable.c5clubs);
                break;
            case "clubs6":
                image.setImageResource(R.drawable.c6clubs);
                break;
            case "clubs7":
                image.setImageResource(R.drawable.c7clubs);
                break;
            case "clubs8":
                image.setImageResource(R.drawable.c8clubs);
                break;
            case "clubs9":
                image.setImageResource(R.drawable.c9clubs);
                break;
            case "clubs10":
                image.setImageResource(R.drawable.c10clubs);
                break;
            case "clubs11":
                image.setImageResource(R.drawable.jackclubs);
                break;
            case "clubs12":
                image.setImageResource(R.drawable.queenclubs);
                break;
            case "clubs13":
                image.setImageResource(R.drawable.kingclubs);
                break;
            case "clubs14":
                image.setImageResource(R.drawable.aceclub);
                break;

            //Diamonds
            case "diamonds2":
                image.setImageResource(R.drawable.c2diamond);
                break;
            case "diamonds3":
                image.setImageResource(R.drawable.c3diamond);
                break;
            case "diamonds4":
                image.setImageResource(R.drawable.c4diamond);
                break;
            case "diamonds5":
                image.setImageResource(R.drawable.c5diamond);
                break;
            case "diamonds6":
                image.setImageResource(R.drawable.c6diamond);
                break;
            case "diamonds7":
                image.setImageResource(R.drawable.c7diamond);
                break;
            case "diamonds8":
                image.setImageResource(R.drawable.c8diamond);
                break;
            case "diamonds9":
                image.setImageResource(R.drawable.c9diamond);
                break;
            case "diamonds10":
                image.setImageResource(R.drawable.c10diamond);
                break;
            case "diamonds11":
                image.setImageResource(R.drawable.jackdiamonds);
                break;
            case "diamonds12":
                image.setImageResource(R.drawable.queendiamonds);
                break;
            case "diamonds13":
                image.setImageResource(R.drawable.kingdiamonds);
                break;
            case "diamonds14":
                image.setImageResource(R.drawable.acediamond);
                break;

            //Hearts
            case "hearts2":
                image.setImageResource(R.drawable.c2hearts);
                break;
            case "hearts3":
                image.setImageResource(R.drawable.c3hearts);
                break;
            case "hearts4":
                image.setImageResource(R.drawable.c4hearts);
                break;
            case "hearts5":
                image.setImageResource(R.drawable.c5hearts);
                break;
            case "hearts6":
                image.setImageResource(R.drawable.c6hearts);
                break;
            case "hearts7":
                image.setImageResource(R.drawable.c7hearts);
                break;
            case "hearts8":
                image.setImageResource(R.drawable.c8hearts);
                break;
            case "hearts9":
                image.setImageResource(R.drawable.c9hearts);
                break;
            case "hearts10":
                image.setImageResource(R.drawable.c10hearts);
                break;
            case "hearts11":
                image.setImageResource(R.drawable.jackhearts);
                break;
            case "hearts12":
                image.setImageResource(R.drawable.queenhearts);
                break;
            case "hearts13":
                image.setImageResource(R.drawable.kinghearts);
                break;
            case "hearts14":
                image.setImageResource(R.drawable.acehearts);
                break;

            //Spades
            case "spades2":
                image.setImageResource(R.drawable.c2spade);
                break;
            case "spades3":
                image.setImageResource(R.drawable.c3spade);
                break;
            case "spades4":
                image.setImageResource(R.drawable.c4spade);
                break;
            case "spades5":
                image.setImageResource(R.drawable.c5spade);
                break;
            case "spades6":
                image.setImageResource(R.drawable.c6spade);
                break;
            case "spades7":
                image.setImageResource(R.drawable.c7spade);
                break;
            case "spades8":
                image.setImageResource(R.drawable.c8spade);
                break;
            case "spades9":
                image.setImageResource(R.drawable.c9spade);
                break;
            case "spades10":
                image.setImageResource(R.drawable.c10spade);
                break;
            case "spades11":
                image.setImageResource(R.drawable.jackspade);
                break;
            case "spades12":
                image.setImageResource(R.drawable.queenspade);
                break;
            case "spades13":
                image.setImageResource(R.drawable.kingspades);
                break;
            case "spades14":
                image.setImageResource(R.drawable.acespade);
                break;
        }
    }*/



    /*public void assignImages (String card, ImageView image) {
        switch(card){
            //Clubs

            case "clubs2":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c2clubs));
                break;
            case "clubs3":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c3clubs));
                break;
            case "clubs4":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c4clubs));
                break;
            case "clubs5":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c5clubs));
                break;
            case "clubs6":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c6clubs));
                break;
            case "clubs7":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c7clubs));
                break;
            case "clubs8":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c8clubs));
                break;
            case "clubs9":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c9clubs));
                break;
            case "clubs10":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c10clubs));
                break;
            case "clubs11":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.jackclubs));
                break;
            case "clubs12":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.queenclubs));
                break;
            case "clubs13":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.kingclubs));
                break;
            case "clubs14":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.aceclub));
                break;

            //Diamonds
            case "diamonds2":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c2diamond));
                break;
            case "diamonds3":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c3diamond));
                break;
            case "diamonds4":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c4diamond));
                break;
            case "diamonds5":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c5diamond));
                break;
            case "diamonds6":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c6diamond));
                break;
            case "diamonds7":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c7diamond));
                break;
            case "diamonds8":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c8diamond));
                break;
            case "diamonds9":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c9diamond));
                break;
            case "diamonds10":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c10diamond));
                break;
            case "diamonds11":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.jackdiamonds));
                break;
            case "diamonds12":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.queendiamonds));
                break;
            case "diamonds13":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.kingdiamonds));
                break;
            case "diamonds14":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.acediamond));
                break;

            //Hearts
            case "hearts2":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c2hearts));
                break;
            case "hearts3":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c3hearts));
                break;
            case "hearts4":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c4hearts));
                break;
            case "hearts5":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c5hearts));
                break;
            case "hearts6":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c6hearts));
                break;
            case "hearts7":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c7hearts));
                break;
            case "hearts8":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c8hearts));
                break;
            case "hearts9":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c9hearts));
                break;
            case "hearts10":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c10hearts));
                break;
            case "hearts11":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.jackhearts));
                break;
            case "hearts12":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.queenhearts));
                break;
            case "hearts13":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.kinghearts));
                break;
            case "hearts14":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.acehearts));
                break;

            //Spades
            case "spades2":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c2spade));
                break;
            case "spades3":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c3spade));
                break;
            case "spades4":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c4spade));
                break;
            case "spades5":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c5spade));
                break;
            case "spades6":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c6spade));
                break;
            case "spades7":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c7spade));
                break;
            case "spades8":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c8spade));
                break;
            case "spades9":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c9spade));
                break;
            case "spades10":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c10spade));
                break;
            case "spades11":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.jackspade));
                break;
            case "spades12":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.queenspade));
                break;
            case "spades13":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.kingspades));
                break;
            case "spades14":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.acespade));
                break;
        }
    }*/


    private static Bitmap decodeResource(Resources res, int id) {
        Bitmap bitmap = null;
        BitmapFactory.Options options = new BitmapFactory.Options();
        for (options.inSampleSize = 1; options.inSampleSize <= 32; options.inSampleSize++) {
            try {
                bitmap = BitmapFactory.decodeResource(res, id, options);
                Log.d("hereiam", "Decoded successfully for sampleSize " + options.inSampleSize);
                break;
            } catch (OutOfMemoryError outOfMemoryError) {
                // If an OutOfMemoryError occurred, we continue with for loop and next inSampleSize value
                Log.e("heriam", "outOfMemoryError while reading file for sampleSize " + options.inSampleSize
                        + " retrying with higher value");
            }
        }
        return bitmap;
    }
}
 
Upvote 0
Code:
package com.example.robertrodriquez.spit;

import android.support.v4.content.res.TypedArrayUtils;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;



//import android.widget.TextView;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Random;

public class MainActivity extends AppCompatActivity {

    ImageView player1Deck, player2Deck, player1Empty, player2Empty;

    int countPlayer1 = 0;
    int[] drawCard;
    PlayingCards pc = new PlayingCards();
    int[] deck;

    ArrayList<PlayingCards> a = new ArrayList<>();
    ArrayList<PlayingCards> b = new ArrayList<PlayingCards>();
    ArrayList<Integer> cardsCalled = new ArrayList<>();

    int randHigh = 52;
    int randLow = 0;
    int deckIndex = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    // Example of a call to a native method
    //TextView tv = (TextView) findViewById(R.id.sample_text);
    //tv.setText(stringFromJNI());

        player1Deck = (ImageView) findViewById(R.id.player1Deck);
        player2Deck = (ImageView) findViewById(R.id.player2Deck);
        player1Empty = (ImageView) findViewById(R.id.player1Empty);
        player2Empty = (ImageView) findViewById(R.id.player2Empty);

        player1Empty.setVisibility(View.VISIBLE);
        player2Empty.setVisibility(View.VISIBLE);

        b = deck();


        player1Deck.setOnClickListener(new View.OnClickListener(){

            public void onClick(View view){
                int deal = 0;
                PlayingCards c = null;
                String name;

                deal = getRandom();
                c = b.get(deal);
                name = c.getName2();
                pc.assignImages(name, player1Empty);

                //flip player 1 cards
                /*pc.assignImages(pc.cards.get(countPlayer1), player1Empty);*/

                player1Empty.setVisibility(View.VISIBLE);
                countPlayer1++;
            }
        });



        //player2Deck.setOnClickListener(new View.OnClickListener());

    }




    public ArrayList<PlayingCards> deck(){

        for(int n=2; n < 15; n++) {
            PlayingCards c = new PlayingCards("clubs" + n, "clubs", n);
            a.add(c);
        }

        for(int n=2; n < 15; n++) {
            PlayingCards c = new PlayingCards("diamonds" + n, "diamonds", n);
            a.add(c);
        }

        for(int n=2; n < 15; n++) {
            PlayingCards c = new PlayingCards("hearts" + n, "hearts", n);
            a.add(c);
        }

        for(int n=2; n < 15; n++) {
            PlayingCards c = new PlayingCards("spades" + n, "spades", n);
            a.add(c);
        }

        return a;
    }


    public int getRandom(){

        Random r = new Random();
        deckIndex = r.nextInt(randHigh-randLow) + randLow;

        if (cardsCalled.contains(deckIndex)) {
            getRandom();
        } else {
            cardsCalled.add(deckIndex);
        }

        return deckIndex;
    }


    /**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     */
    public native String stringFromJNI();

    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("native-lib");
    }
}
 
Upvote 0
Code:
Runtime: FATAL EXCEPTION: main
                                                                              Process: com.example.robertrodriquez.spit, PID: 870
                                                                              java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
                                                                                  at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:106)
                                                                                  at com.example.robertrodriquez.spit.PlayingCards.assignImages(PlayingCards.java:112)
                                                                                  at com.example.robertrodriquez.spit.MainActivity$1.onClick(MainActivity.java:67)
                                                                                  at android.view.View.performClick(View.java:6261)
                                                                                  at android.view.View$PerformClick.run(View.java:23748)
                                                                                  at android.os.Handler.handleCallback(Handler.java:751)
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                  at android.os.Looper.loop(Looper.java:154)
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:6776)
                                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
06-15 11:00:48.447 870-882/com.example.robertrodriquez.spit W/art: Suspending all threads took: 11.652ms
 
Upvote 0
You can't create an Activity like this

Code:
PlayingCards pc = new PlayingCards();

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.
 
Upvote 0
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?
 
Upvote 0
@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:
06-16 19:37:03.854 15123-15123/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                   Process: com.example.robertrodriquez.spit, PID: 15123
                                                   java.lang.OutOfMemoryError: Failed to allocate a 1622264796 byte allocation with 16777216 free bytes and 171MB until OOM
                                                       at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
                                                       at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
                                                       at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:700)
                                                       at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:535)
                                                       at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1179)
                                                       at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:770)
                                                       at android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:621)
                                                       at android.content.res.Resources.getDrawable(Resources.java:1642)
                                                       at android.content.Context.getDrawable(Context.java:525)
                                                       at android.support.v4.content.ContextCompatApi21.getDrawable(ContextCompatApi21.java:30)
                                                       at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:372)
                                                       at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:202)
                                                       at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:190)
                                                       at android.support.v7.content.res.AppCompatResources.getDrawable(AppCompatResources.java:100)
                                                       at android.support.v7.widget.AppCompatImageHelper.setImageResource(AppCompatImageHelper.java:73)
                                                       at android.support.v7.widget.AppCompatImageView.setImageResource(AppCompatImageView.java:81)
                                                       at com.example.robertrodriquez.spit.PlayingCards.assignImages(PlayingCards.java:379)
                                                       at com.example.robertrodriquez.spit.MainActivity$1.onClick(MainActivity.java:67)
                                                       at android.view.View.performClick(View.java:6261)
                                                       at android.view.View$PerformClick.run(View.java:23748)
                                                       at android.os.Handler.handleCallback(Handler.java:751)
                                                       at android.os.Handler.dispatchMessage(Handler.java:95)
                                                       at android.os.Looper.loop(Looper.java:154)
                                                       at android.app.ActivityThread.main(ActivityThread.java:6776)
                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)

This code below is in main and appears to be running fine
Code:
player1Deck.setOnClickListener(new View.OnClickListener(){

    public void onClick(View view){
        int deal = 0;
        PlayingCards c = null;
        String name;

        deal = getRandom();
        c = b.get(deal);
        name = c.getName2();
        pc.assignImages(name, player1Empty);

        //flip player 1 cards
        /*pc.assignImages(pc.cards.get(countPlayer1), player1Empty);*/

        player1Empty.setVisibility(View.VISIBLE);
        countPlayer1++;
    }
});

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:
public void assignImages (String card, ImageView image) {
    switch(card){
        //Clubs

        case "clubs2":
            image.setImageResource(R.drawable.c2clubs);
            break;
        case "clubs3":
            image.setImageResource(R.drawable.c3clubs);
            break;
        case "clubs4":
            image.setImageResource(R.drawable.c4clubs);
            break;
        case "clubs5":
            image.setImageResource(R.drawable.c5clubs);
            break;
        case "clubs6":
            image.setImageResource(R.drawable.c6clubs);
            break;
        case "clubs7":
            image.setImageResource(R.drawable.c7clubs);
            break;
        case "clubs8":
            image.setImageResource(R.drawable.c8clubs);
            break;
        case "clubs9":
            image.setImageResource(R.drawable.c9clubs);
            break;
        case "clubs10":
            image.setImageResource(R.drawable.c10clubs);
            break;
        case "clubs11":
            image.setImageResource(R.drawable.jackclubs);
            break;
        case "clubs12":
            image.setImageResource(R.drawable.queenclubs);
            break;
        case "clubs13":
            image.setImageResource(R.drawable.kingclubs);
            break;
        case "clubs14":
            image.setImageResource(R.drawable.aceclub);
            break;

        //Diamonds
        case "diamonds2":
            image.setImageResource(R.drawable.c2diamond);
            break;
        case "diamonds3":
            image.setImageResource(R.drawable.c3diamond);
            break;
        case "diamonds4":
            image.setImageResource(R.drawable.c4diamond);
            break;
        case "diamonds5":
            image.setImageResource(R.drawable.c5diamond);
            break;
        case "diamonds6":
            image.setImageResource(R.drawable.c6diamond);
            break;
        case "diamonds7":
            image.setImageResource(R.drawable.c7diamond);
            break;
        case "diamonds8":
            image.setImageResource(R.drawable.c8diamond);
            break;
        case "diamonds9":
            image.setImageResource(R.drawable.c9diamond);
            break;
        case "diamonds10":
            image.setImageResource(R.drawable.c10diamond);
            break;
        case "diamonds11":
            image.setImageResource(R.drawable.jackdiamonds);
            break;
        case "diamonds12":
            image.setImageResource(R.drawable.queendiamonds);
            break;
        case "diamonds13":
            image.setImageResource(R.drawable.kingdiamonds);
            break;
        case "diamonds14":
            image.setImageResource(R.drawable.acediamond);
            break;

        //Hearts
        case "hearts2":
            image.setImageResource(R.drawable.c2hearts);
            break;
        case "hearts3":
            image.setImageResource(R.drawable.c3hearts);
            break;
        case "hearts4":
            image.setImageResource(R.drawable.c4hearts);
            break;
        case "hearts5":
            image.setImageResource(R.drawable.c5hearts);
            break;
        case "hearts6":
            image.setImageResource(R.drawable.c6hearts);
            break;
        case "hearts7":
            image.setImageResource(R.drawable.c7hearts);
            break;
        case "hearts8":
            image.setImageResource(R.drawable.c8hearts);
            break;
        case "hearts9":
            image.setImageResource(R.drawable.c9hearts);
            break;
        case "hearts10":
            image.setImageResource(R.drawable.c10hearts);
            break;
        case "hearts11":
            image.setImageResource(R.drawable.jackhearts);
            break;
        case "hearts12":
            image.setImageResource(R.drawable.queenhearts);
            break;
        case "hearts13":
            image.setImageResource(R.drawable.kinghearts);
            break;
        case "hearts14":
            image.setImageResource(R.drawable.acehearts);
            break;

        //Spades
        case "spades2":
            image.setImageResource(R.drawable.c2spade);
            break;
        case "spades3":
            image.setImageResource(R.drawable.c3spade);
            break;
        case "spades4":
            image.setImageResource(R.drawable.c4spade);
            break;
        case "spades5":
            image.setImageResource(R.drawable.c5spade);
            break;
        case "spades6":
            image.setImageResource(R.drawable.c6spade);
            break;
        case "spades7":
            image.setImageResource(R.drawable.c7spade);
            break;
        case "spades8":
            image.setImageResource(R.drawable.c8spade);
            break;
        case "spades9":
            image.setImageResource(R.drawable.c9spade);
            break;
        case "spades10":
            image.setImageResource(R.drawable.c10spade);
            break;
        case "spades11":
            image.setImageResource(R.drawable.jackspade);
            break;
        case "spades12":
            image.setImageResource(R.drawable.queenspade);
            break;
        case "spades13":
            image.setImageResource(R.drawable.kingspades);
            break;
        case "spades14":
            image.setImageResource(R.drawable.acespade);
            break;
    }
}

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.
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones