setBackgroundResource from numeric string causes F

2019-09-19 00:50发布

问题:

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 following example:

Android drawables : Is there any way to somehow link the int IDs to the R drawable IDs?

However when I do the result is the following:

06-03 08:36:47.551: E/AndroidRuntime(1765): FATAL EXCEPTION: main
06-03 08:36:47.551: E/AndroidRuntime(1765): Process: com.app.example, PID: 1765
06-03 08:36:47.551: E/AndroidRuntime(1765): android.content.res.Resources$NotFoundException: Resource ID #0x8c5
06-03 08:36:47.551: E/AndroidRuntime(1765):     at android.content.res.Resources.getValue(Resources.java:1123)
06-03 08:36:47.551: E/AndroidRuntime(1765):     at android.content.res.Resources.getDrawable(Resources.java:698)
06-03 08:36:47.551: E/AndroidRuntime(1765):     at android.view.View.setBackgroundResource(View.java:15303)
06-03 08:36:47.551: E/AndroidRuntime(1765):     at com.app.example.MainActivity$1.handleMessage(MainActivity.java:163)
06-03 08:36:47.551: E/AndroidRuntime(1765):     at com.app.example.ServiceManager$IncomingHandler.handleMessage(ServiceManager.java:28)
06-03 08:36:47.551: E/AndroidRuntime(1765):     at android.os.Handler.dispatchMessage(Handler.java:102)
06-03 08:36:47.551: E/AndroidRuntime(1765):     at android.os.Looper.loop(Looper.java:136)
06-03 08:36:47.551: E/AndroidRuntime(1765):     at android.app.ActivityThread.main(ActivityThread.java:5001)
06-03 08:36:47.551: E/AndroidRuntime(1765):     at java.lang.reflect.Method.invokeNative(Native Method)
06-03 08:36:47.551: E/AndroidRuntime(1765):     at java.lang.reflect.Method.invoke(Method.java:515)
06-03 08:36:47.551: E/AndroidRuntime(1765):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
06-03 08:36:47.551: E/AndroidRuntime(1765):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
06-03 08:36:47.551: E/AndroidRuntime(1765):     at dalvik.system.NativeStart.main(Native Method)

My code snippet is as follows - it seems correct - but there seems to be something wrong with it (obviously). Can anyone spot what I may have done wrong in this instance?

String SnapShotFrame_TblTriviaLU = "1170";
RelativeLayout relativeLayout2 = (RelativeLayout) findViewById(R.id.gameplayLayout);
            int resId = getResources().getIdentifier(SnapShotFrame_TblTriviaLU, "drawable", getPackageName());
                                relativeLayout2.setBackgroundResource(resId);

回答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_".

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


回答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?