索引超出范围无效索引0,大小为0数组列表 [英] Index out of bounds Invalid Index 0, size is 0 Array List

查看:90
本文介绍了索引超出范围无效索引0,大小为0数组列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我在一个活动中声明一个数组列表并添加到它,然后我在另一个活动中访问此数组列表并尝试将数据从列表的位置中取出...这样我就收到了错误。下面是我声明列表并添加到其中的代码,以及我在不同类中访问它的代码...





// ACTIVITY 1 - PHOTO HANDLER CLASS

Hi,

I'm declaring an array list in one activity and adding to it, I'm then accessing this array list in another activity and trying to take the data out of the location of the list...with this I'm getting the error. Below is my code for where I declare the list and add to it, and the code for where I access it in a different class...


//ACTIVITY 1 - PHOTO HANDLER CLASS

ArrayList<Bitmap> BMArray = new ArrayList<Bitmap>();
BMArray.add(receiptRot);

public ArrayList<Bitmap> GetBitMap(){
		return this.BMArray;
	}





// ACTIVITY 2



//ACTIVITY 2

PhotoHandler pH = new PhotoHandler();
int loc = 0;
 ArrayList<Bitmap> Bit = pH.GetBitMap();
//Setting newReceipt Bmp to content of Array List Location
newReceipt = Bit.get(loc);
loc++;





所以数组列表不应为空...



任何人都有任何建议?



So the array list shouldn't be empty...

Anyone have any suggestions?

推荐答案

显然, ArrayList 是空的异常抛出的点,由您的异常信息清楚地表明。即使您的代码显示对 add 的一次调用,但在尝试索引数组列表实例之前,此调用并不明显。你没有真正展示前两行代码的上下文。



使用调试器查看操作的真实顺序并跟踪添加项目的位置以及你不。确保在索引元素之前检查集合中元素的数量。



-SA
Apparently, the ArrayList is empty at the point of the exception throwing, which is clearly indicated by your exception information. Even though your code shows one call to add, it's not apparent that this call is performed before your attempt to index an array list instance. You don't really show the context of first two lines of code.

Use the debugger to see the real order of operations and track where you add an item and where you do not. Make sure you check up the number of element in a collection before indexing an element of it.

—SA


这不是将一个arraylist从一个活动传递到另一个活动的方法。你需要使用getExtra()和putExtra()将这些东西从一个活动传递给另一个。如果是位图数组,这些将清除你大多数..

http://stackoverflow.com/questions/12908048 / pass-bitmap-between-two-activities [ ^ ]

http://stackoverflow.com/questions/13510549/how-to-pass-bitmap-array-to-another-activity-in-android [ ^ ]

http://stackoverflow.com/questions/11387603/how-to-pass-a-arraylistbitmap-between-activities [ ^ ]
This is not the way to pass an arraylist from one activity to another.You need to use getExtra() and putExtra() to pass these thing from an activity to other.In case of bitmap array these will clear you most..
http://stackoverflow.com/questions/12908048/passing-bitmap-between-two-activities[^]
http://stackoverflow.com/questions/13510549/how-to-pass-bitmap-array-to-another-activity-in-android[^]
http://stackoverflow.com/questions/11387603/how-to-pass-a-arraylistbitmap-between-activities[^]


这篇关于索引超出范围无效索引0,大小为0数组列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆