Skip to content Skip to sidebar Skip to footer

Setbackgroundresource From Numeric String Causes Fatal Error - Android/java

I'm attempting to set the background of a layout from a numeric filename (it has to be numeric - this cannot be change) string (the image is 1170.png). I attempted to use the foll

Solution 1:

Unfortunately, you can't have a resource with a numeric name. Perhaps you can do a workaround: change to drawable_1170 and if you need the number to compare or to work with, you can remove the string "drawable_".

StringSnapShotFrame_TblTriviaLU="1170";
RelativeLayoutrelativeLayout2= (RelativeLayout) findViewById(R.id.gameplayLayout);
            intresId= getResources().getIdentifier("drawable_"+SnapShotFrame_TblTriviaLU, "drawable", getPackageName());
                                relativeLayout2.setBackgroundResource(resId);

Solution 2:

I asume you tried to use R.drawable.1170 with no success and then tried the solution you posted.

Resource identifiers MUST start with a character. Thats because Java identifiers must start with a character.

Read more here Why can an identifier not start with a number?

Post a Comment for "Setbackgroundresource From Numeric String Causes Fatal Error - Android/java"