关于android中的整数数组

时间:2010-07-30 05:59:19

标签: android

首先,我在strings.xml文件中声明一个整数数组,并向它们添加3个项目 然后我用

使用那些项目
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]);
        }

我得到了例外,请给我一些解决方法如何显示数组

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(ActivityThread.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

2 个答案:

答案 0 :(得分:1)

这里的问题是您将整数直接传递给setText方法。然后,它所调用的是setText(int),它意味着接收资源ID,而不是值。所以,你唯一需要做的就是用这个整数创建一个String,这样调用的方法就是setText(String)(你可以做你想要的):

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]));
}

答案 1 :(得分:0)

在执行此操作之前,您是否设置了setContentView()? 因为在访问任何资源之前未设置setContentView()时可能找不到资源错误。