1. Check out our companion app, Forums for Android! Download from Google Play

Problems with setImageResource() on in if statement

Discussion in 'Application Development' started by jdizzle1988, Jul 1, 2010.

  1. jdizzle1988

    jdizzle1988 Member
    Thread Starter
    16

    Jul 1, 2010
    12
    1
    16
    IS Technician
    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
    Thread Starter
    16

    Jul 1, 2010
    12
    1
    16
    IS Technician
    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
    53

    Apr 30, 2010
    375
    35
    53
    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
    18

    Apr 6, 2010
    85
    16
    18
    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...