倍数加倍时索引超出范围

倍数加倍时索引超出范围

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

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

第二项活动读取3个数字。然后,需要将它们乘以一些常数。这就是问题所在。如果相乘的结果太大(例如,大于两百...阈值不恒定...不接近应容纳双精度数的阈值),则抛出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的数组中的第5个(位置4的项目)项目。

关于java - 倍数加倍时索引超出范围,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11697954/

10-08 23:25