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

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
  • 69,000
  • 34
  • 174
  • 243
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
  • 227,407
  • 48
  • 383
  • 507
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,808
  • 1
  • 17
  • 23