4

I get a force close on this

if(num1.getText().equals("0")) {
            num1.setText("1");
            ImageView hpdown1  = (ImageView)findViewById(R.id.hair);
            hpdown1.setImageResource(R.drawable.haie2);
        }

Please help..

Hailei Edwards
  • 137
  • 2
  • 2
  • 10
  • 2
    I suspect there'll be an exception somewhere in a log. Find it and it'll give a lot more information... – Jon Skeet Oct 17 '11 at 21:27

4 Answers4

20

You can use getDrawable method from Resources class. For example :

ImageView image  = (ImageView) findViewById(R.id.image);
Resources res = getResources(); /** from an Activity */
image.setImageDrawable(res.getDrawable(R.drawable.myimage));
Julien Rolland
  • 213
  • 1
  • 4
3

Is deprecated

getResources().<strike>getDrawable</strike>(R.drawable.myimage));

Now:

image.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.menu_southamerica));
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
2

You don't say, but I'm guessing a null pointer exception. Make sure that hpdown is not null before you call setImageResource.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
0

Something tells me that your issue is that your resource is: R.drawable.haie2 which to me, in the context of your code, looks like it's a spelling mistake that is supposed to be R.drawable.hair2

d0nut
  • 2,835
  • 1
  • 18
  • 23