我试图将字符串和索引位置一起动态添加到ArrayList中,然后将其作为Intent中的附加项传递给Arraylist。该过程第一次运行良好,并且在下次调用此活动时将最终导致Index out of bounds Exception。请查看下面的代码,并建议这是否是代码中的缺陷。

 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)


最佳答案

private ArrayList<String> imagesUriArray=new ArrayList<>();
static int imageIndex=-1;
..
imageIndex++;
imagesUriArray.add(imageIndex,UriString);


第一次调用onActivityResult时,imageIndex为-1,因此在递增它之后,您正在向列表的第一个索引(0)添加一个项目。

但是,由于imageIndex是静态的,因此下次在另一个对象上调用onActivityResult时,即使列表为空,imageIndex也为0,因此,您尝试向空List的索引1添加元素。将索引抛出异常。如果将imageIndex更改为非静态,该问题将得到解决。

避免出现异常的另一种方法是将imagesUriArray.add(imageIndex,UriString);更改为imagesUriArray.add(UriString);。这会将元素添加到列表的下一个可用索引。

关于java - 将多个字符串放入Arraylist,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30653880/

10-09 13:43