gpt4 book ai didi

java - 乘以 double 时索引超出范围

转载 作者:行者123 更新时间:2023-11-30 04:05:50 25 4
gpt4 key购买 nike

第一次发帖。提前感谢您的帮助。

我有两个 Activity 。第一个 Activity 通过 inputType 为“numberDecimal”的 edittext 字段从用户读取 3 个数字。它 bundle 这些并通过 Intent 将它们发送到第二个 Activity 。

第二个 Activity 读入 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 的数组中的第 5 个(位置 4 处的项目)。

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com