从数字字符串setBackgroundResource导致致命错误 - 的Android / Java的 [英] setBackgroundResource from numeric string causes Fatal Error - Android/Java
问题描述
我试图从一个数字文件名设置布局的背景(它必须是数字 - 这个不能改变)串(图像1170.png)。
我尝试使用下面的例子:
然而,当我这样做的结果是:
8月六日至3日:36:47.551:E / AndroidRuntime(1765):致命异常:主要
八月6日至3日:36:47.551:E / AndroidRuntime(1765):工艺:com.app.example,PID:1765
八月6日至3日:36:47.551:E / AndroidRuntime(1765):android.content.res.Resources $ NotFoundException:资源ID#0x8c5
八月6日至3日:36:47.551:E / AndroidRuntime(1765):在android.content.res.Resources.getValue(Resources.java:1123)
八月6日至3日:36:47.551:E / AndroidRuntime(1765):在android.content.res.Resources.getDrawable(Resources.java:698)
八月6日至3日:36:47.551:E / AndroidRuntime(1765):在android.view.View.setBackgroundResource(View.java:15303)
八月6日至3日:36:47.551:E / AndroidRuntime(1765):在com.app.example.MainActivity $ 1.handleMessage(MainActivity.java:163)
八月6日至3日:36:47.551:E / AndroidRuntime(1765):在com.app.example.ServiceManager $ IncomingHandler.handleMessage(ServiceManager.java:28)
八月6日至3日:36:47.551:E / AndroidRuntime(1765):在android.os.Handler.dispatchMessage(Handler.java:102)
八月6日至3日:36:47.551:E / AndroidRuntime(1765):在android.os.Looper.loop(Looper.java:136)
八月6日至3日:36:47.551:E / AndroidRuntime(1765):在android.app.ActivityThread.main(ActivityThread.java:5001)
八月6日至3日:36:47.551:E / AndroidRuntime(1765):在java.lang.reflect.Method.invokeNative(本机方法)
八月6日至3日:36:47.551:E / AndroidRuntime(1765):在java.lang.reflect.Method.invoke(Method.java:515)
八月6日至3日:36:47.551:E / AndroidRuntime(1765):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:785)
八月6日至3日:36:47.551:E / AndroidRuntime(1765):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
八月6日至3日:36:47.551:E / AndroidRuntime(1765):在dalvik.system.NativeStart.main(本机方法)
我的code片断如下 - 这似乎是正确的 - 但似乎有什么不对的地方(显然)。任何人都可以发现我可以在这种情况下有什么错呢?
字符串SnapShotFrame_TblTriviaLU =1170;
RelativeLayout的relativeLayout2 =(RelativeLayout的)findViewById(R.id.gameplayLayout);
。INT渣油= getResources()则getIdentifier(SnapShotFrame_TblTriviaLU,绘制,getPackageName());
relativeLayout2.setBackgroundResource(渣油);
不幸的是,你不能有一个数字名称的资源。也许你可以做一个变通方法:改变drawable_1170,如果您需要的数量来比较,或一起工作,可以去除字符串绘_
字符串SnapShotFrame_TblTriviaLU =1170;
RelativeLayout的relativeLayout2 =(RelativeLayout的)findViewById(R.id.gameplayLayout);
INT渣油= getResources()则getIdentifier(绘_+ SnapShotFrame_TblTriviaLU,绘制,getPackageName());
relativeLayout2.setBackgroundResource(渣油);
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:
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);
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);
这篇关于从数字字符串setBackgroundResource导致致命错误 - 的Android / Java的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!