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
  • 127
  • 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

18

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));
3

Is deprecated

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

Now:

image.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.menu_southamerica));
Eric Aya
  • 68,765
  • 33
  • 165
  • 232
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
  • 222,293
  • 47
  • 371
  • 489
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,841
  • 1
  • 16
  • 23