开发者

about integerarrays ina android

first of all i declare an integer array in my strings.xml file and add 3 items to them then i use that items with

private TextView Number12;
Number12=(TextView)findViewById(R.id.TextView01);
 Resources r = getResources();
        int[] bases = r.getIntArray(R.array.UserBases);
 for(i=0;i<bases[i];i++)
        {
            Number12.setText(bases[i]);
        }

I got the exception,please give me some solution how to display an array

07-30 11:30:10.020: ERROR/AndroidRuntime(657): FATAL EXCEPTION: main
07-30 11:30:10.020: ERROR/AndroidRuntime(657): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.User_Interface/com.User_Interface.HelloWorld1}: android.content.res.Resources$NotFoundException: String resource ID #0x1
07-30 11:30:10.020: ERROR/AndroidRuntime(657):     at android.app.ActivityThread.performLaunchActivity(Ac开发者_StackOverflow社区tivityThread.java:2663)
07-30 11:30:10.020: ERROR/AndroidRuntime(657):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-30 11:30:10.020: ERROR/AndroidRuntime(657):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-30 11:30:10.020: ERROR/AndroidRuntime(657):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-30 11:30:10.020: ERROR/AndroidRuntime(657):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-30 11:30:10.020: ERROR/AndroidRuntime(657):     at android.os.Looper.loop(Looper.java:123)
07-30 11:30:10.020: ERROR/AndroidRuntime(657):     at android.app.ActivityThread.main(ActivityThread.java:4627)
07-30 11:30:10.020: ERROR/AndroidRuntime(657):     at java.lang.reflect.Method.invokeNative(Native Method)
07-30 11:30:10.020: ERROR/AndroidRuntime(657):     at java.lang.reflect.Method.invoke(Method.java:521)
07-30 11:30:10.020: ERROR/AndroidRuntime(657):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-30 11:30:10.020: ERROR/AndroidRuntime(657):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-30 11:30:10.020: ERROR/AndroidRuntime(657):     at dalvik.system.NativeStart.main(Native Method)
07-30 11:30:10.020: ERROR/AndroidRuntime(657): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x1
07-30 11:30:10.020: ERROR/AndroidRuntime(657):     at android.content.res.Resources.getText(Resources.java:201)
07-30 11:30:10.020: ERROR/AndroidRuntime(657):     at android.widget.TextView.setText(TextView.java:2817)
07-30 11:30:10.020: ERROR/AndroidRuntime(657):     at com.User_Interface.HelloWorld1.onCreate(HelloWorld1.java:55)
07-30 11:30:10.020: ERROR/AndroidRuntime(657):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-30 11:30:10.020: ERROR/AndroidRuntime(657):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
07-30 11:30:10.020: ERROR/AndroidRuntime(657):     ... 11 more


The problem here is that you are passing an integer directly to the setText method. Then, what it's being called is setText(int) which is meant to receive a resource ID, not a value. So, the only thing you have to do here is creating a String with that integer, so that the method that will be called is setText(String) (which do what you want):

private TextView Number12;
Number12=(TextView)findViewById(R.id.TextView01);
Resources r = getResources();
int[] bases = r.getIntArray(R.array.UserBases);
for(i=0;i<bases[i];i++){
      Number12.setText(Integer.toString(bases[i]));
}


Have u set the setContentView() before doing this? Because resource may not found error occurs when you have not set setContentView() before accessing any resource.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜