1

第一次发帖。我在这里先向您的帮助表示感谢。

我有两个活动。第一个活动通过 inputType 为“numberDecimal”的 edittext 字段从用户那里读取 3 个数字。它捆绑这些并通过意图将它们发送到第二个活动。

第二个活动读取 3 个数字。然后它需要将它们乘以一些常数。这就是问题所在。如果乘法的结果太大(例如,大于几百……不是恒定阈值……不接近 double 应该能够容纳的值),那么它会引发 indexoutofboundsexception .

//@SuppressWarnings ("serial)")
public class CreationActivity extends Activity {

@Override
public void onCreate (Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.creation);
    TextView txtTitle = (TextView) findViewById(R.id.labelTitle);
    TextView txtArtist = (TextView) findViewById(R.id.labelArtist);
    TextView txtTime = (TextView) findViewById(R.id.labelTitle);

    try {
        Bundle extras = getIntent().getExtras();
        double Hours;
        double Minutes;
        double Seconds;

        if (extras!=null){
            double timerTime[] = extras.getDoubleArray("Time");
            Hours = timerTime[0];
            Minutes = timerTime[1];
            Seconds = timerTime[2];
                            //next line is where the error happens:
            double desired_time = timerTime[0] * 5000;
                            //do some stuff
        }else{
            txtTitle.setText("No data");
        }
    } catch (NumberFormatException e){
    }
}
}   

LogCat 是:

07-27 21:58:41.736: E/AndroidRuntime(31560): FATAL EXCEPTION: main
07-27 21:58:41.736: E/AndroidRuntime(31560): java.lang.RuntimeException: Unable to start     activity ComponentInfo{com.decker.timer/com.decker.timer.CreationActivity}:    java.lang.IndexOutOfBoundsException: Invalid index 4, size is 4
07-27 21:58:41.736: E/AndroidRuntime(31560):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
07-27 21:58:41.736: E/AndroidRuntime(31560):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
07-27 21:58:41.736: E/AndroidRuntime(31560):    at android.app.ActivityThread.access$600(ActivityThread.java:123)
07-27 21:58:41.736: E/AndroidRuntime(31560):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
07-27 21:58:41.736: E/AndroidRuntime(31560):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-27 21:58:41.736: E/AndroidRuntime(31560):    at android.os.Looper.loop(Looper.java:137)
07-27 21:58:41.736: E/AndroidRuntime(31560):    at android.app.ActivityThread.main(ActivityThread.java:4424)
07-27 21:58:41.736: E/AndroidRuntime(31560):    at java.lang.reflect.Method.invokeNative(Native Method)
07-27 21:58:41.736: E/AndroidRuntime(31560):    at java.lang.reflect.Method.invoke(Method.java:511)
07-27 21:58:41.736: E/AndroidRuntime(31560):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-27 21:58:41.736: E/AndroidRuntime(31560):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-27 21:58:41.736: E/AndroidRuntime(31560):    at dalvik.system.NativeStart.main(Native Method)
07-27 21:58:41.736: E/AndroidRuntime(31560): Caused by: java.lang.IndexOutOfBoundsException: Invalid index 4, size is 4
07-27 21:58:41.736: E/AndroidRuntime(31560):    at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
07-27 21:58:41.736: E/AndroidRuntime(31560):    at java.util.ArrayList.get(ArrayList.java:304)
07-27 21:58:41.736: E/AndroidRuntime(31560):    at com.decker.timer.CreationActivity.onCreate(CreationActivity.java:172)
07-27 21:58:41.736: E/AndroidRuntime(31560):    at android.app.Activity.performCreate(Activity.java:4465)
07-27 21:58:41.736: E/AndroidRuntime(31560):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
07-27 21:58:41.736: E/AndroidRuntime(31560):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
07-27 21:58:41.736: E/AndroidRuntime(31560):    ... 11 more
07-27 21:58:49.260: D/TimerActivity(31583): onClicked
4

1 回答 1

1

听起来您正在尝试访问长度为 4 的数组中的第 5 个(位置 4 的项目)项目。

于 2012-07-28T03:17:46.407 回答