Problems with setImageResource() on in if statement


Last Updated:

  1. jdizzle1988

    jdizzle1988 Member This Topic's Starter

    Joined:
    Jul 1, 2010
    Messages:
    12
    Likes Received:
    1
    I have an if statment that if the condition is met, I do a setImageResource() on an ImageView and occasionaly it works, but most of the time it force closes after the fact. If I comment out the setImageResource() it will do all the other calculations and such fine. Can anyone shed some light on this?
     

    Advertisement
  2. jdizzle1988

    jdizzle1988 Member This Topic's Starter

    Joined:
    Jul 1, 2010
    Messages:
    12
    Likes Received:
    1
    Ok after posting this I think I found the solution. So I added the code as follows:

    Before:
    } ​
    else if (monstID < 4000) {

    monstName = "Shewanadon";
    imgMonster.setImageResource(R.drawable.shewanadon);

    After:
    } ​
    else if (monstID < 4000) {
    monstName = "Shewanadon";
    imgMonster = (ImageView) this.findViewById(R.id.imgMonsterPic);

    imgMonster.setImageResource(R.drawable.shewanadon);


    Not sure why that worked out like that but it did. Before I declared the ImageView outside the If statement and thats when it failed.

     
  3. cp1

    cp1 Well-Known Member

    Joined:
    Apr 30, 2010
    Messages:
    375
    Likes Received:
    35
    To get more help debugging you can check out the DDMS tab to see which error is causing the close.
     
    jdizzle1988 likes this.
  4. Boogs

    Boogs Well-Known Member

    Joined:
    Apr 6, 2010
    Messages:
    85
    Likes Received:
    16
    Off the cuff first guess is that you're image might be too large, causing an OutOfMemoryException.

    But, cp1 is right: if you want to really know what's causing the error - which could really "help us help you" - then run the program and check the LogCat window in the Debug view (if you're using Eclipse) and tell us what the error is that terminates the program. If you can't figure out which error it is exactly, then just copy a screenshot which covers all the error logs in your LogCat window (hint: they're red).
     
    jdizzle1988 likes this.

Share This Page

Loading...