Skip to content Skip to sidebar Skip to footer

Android Java Set Image Resource

I get a force close on this if(num1.getText().equals('0')) { num1.setText('1'); ImageView hpdown1 = (ImageView)findViewById(R.id.hair); hpdown1

Solution 1:

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

ImageViewimage= (ImageView) findViewById(R.id.image);
Resourcesres= getResources(); /** from an Activity */
image.setImageDrawable(res.getDrawable(R.drawable.myimage));

Solution 2:

Is deprecated

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

Now:

image.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.menu_southamerica));

Solution 3:

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

Solution 4:

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

Post a Comment for "Android Java Set Image Resource"