我正在尝试将Strings动态地添加到ArrayList中以及索引位置,然后在Intent中将Arraylist作为额外内容传递。该过程第一次运行良好,并且在下次调用此活动时最终会在Index中退出边界异常。请参阅下面的代码,并建议这是否是代码中的缺陷。
private ArrayList<String> imagesUriArray=new ArrayList<>();
static int imageIndex=-1;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Some standard code goes here to capture the image and store it...
image = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date());
photo = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), image);
selectedImage = Uri.fromFile(photo);
final String UriString = new String(selectedImage.toString());
imageIndex++;
195: imagesUriArray.add(imageIndex,UriString);
Intent email = new Intent(ScanActivity.this, EmailActivity.class);
email.putExtra("picture", imageInByte);
email.putStringArrayListExtra("Uri", imagesUriArray);
startActivity(email);
Logcat says:
20320-20320/com.example.kittu.ClientLync E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.example.kittu.ClientLync/com.example.kittu.ClientLync.ScanActivity}: java.lang.IndexOutOfBoundsException: Invalid index 1, size is 0
at android.app.ActivityThread.deliverResults(ActivityThread.java:2536)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:2578)
at android.app.ActivityThread.access$2000(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:965)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3689)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IndexOutOfBoundsException: Invalid index 1, size is 0
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
at java.util.ArrayList.add(ArrayList.java:152)
at com.example.kittu.ClientLync.ScanActivity.onActivityResult(ScanActivity.java:195)
at android.app.Activity.dispatchActivityResult(Activity.java:3908)
at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:2578
at android.app.ActivityThread.access$2000(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:965)
答案 0 :(得分:1)
您正在访问arraylist中的无效索引,例如,如果您的arraylist为空并且您尝试在index = 1中插入,则会向您抛出IndexOutOfBoundsException
答案 1 :(得分:0)
根据您的堆栈跟踪,您看到的是IndexOutOfBoundsException
,而不是NPE。
引起:java.lang.IndexOutOfBoundsException:索引1无效,大小为0
你看到了这个,因为你试图在允许范围之外的列表中插入。允许的范围为列表长度的零,包括端点。在这种情况下,您尝试在索引1处添加元素,但列表中没有元素。
来自ArrayList.add(int index, E element)的文档:
public void add(int index, E元素)
...
<强>抛出:强>
IndexOutOfBoundsException - 如果索引超出范围(索引&lt; 0 || index&gt; size())
答案 2 :(得分:0)
试试这个,你在插入
时访问错误的索引 private ArrayList<String> imagesUriArray=new ArrayList<>();
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
imagesUriArray.add(imageIndex,UriString);
Intent email = new Intent(ScanActivity.this, EmailActivity.class);
email.putExtra("picture", imageInByte);
email.putStringArrayListExtra("Uri", imagesUriArray);
startActivity(email);
}
答案 3 :(得分:0)
https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#add(int,%20E) 阅读文档。 显式地写道,add(int,E)方法抛出IndexOutOfBoundsException - 如果索引超出范围(索引&lt; 0 || index&gt; size())