Java Android异步任务ArrayList操作导致onDraw中出现IndexOutOfBounds
我使用异步任务从数据库中获取对象并将其放入ArrayList。我也在用这个ArrayList在屏幕上画东西。这是通过一个for循环完成的,该循环在数组中循环。似乎在异步任务启动时,它会清除数组列表以填充新对象,而for循环有时会运行,这会导致indexOutOfBounds异常 我如何防止这种行为 异步:Java Android异步任务ArrayList操作导致onDraw中出现IndexOutOfBounds,java,android,asynchronous,Java,Android,Asynchronous,我使用异步任务从数据库中获取对象并将其放入ArrayList。我也在用这个ArrayList在屏幕上画东西。这是通过一个for循环完成的,该循环在数组中循环。似乎在异步任务启动时,它会清除数组列表以填充新对象,而for循环有时会运行,这会导致indexOutOfBounds异常 我如何防止这种行为 异步: // GET ORDER LISTINGS: LIST ORDERS IN RANGE public void listOrders() { if (Utili
// GET ORDER LISTINGS: LIST ORDERS IN RANGE
public void listOrders() {
if (Utility.getCurrentLocation() != null) {
handler.post(new Runnable() {
public void run() {
new ListOrdersTask().execute((Void) null);
}
});
} else {
// Cannot Determine Location
showMessage("Cannot Determine Location.");
}
}
class ListOrdersTask extends AsyncTask<Void, Void, Boolean> {
private void postData() {
if (user.loggedIn) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
"http://cyberkomm.ch/sidney/php/getOrders.php");
try {
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
4);
nameValuePairs.add(new BasicNameValuePair("LatMin", String
.valueOf(user.latitude - seekRange)));
nameValuePairs.add(new BasicNameValuePair("LatMax", String
.valueOf(user.latitude + seekRange)));
nameValuePairs.add(new BasicNameValuePair("LongMin", String
.valueOf(user.longitude - seekRange)));
nameValuePairs.add(new BasicNameValuePair("LongMax", String
.valueOf(user.longitude + seekRange)));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
BufferedReader in = new BufferedReader(
new InputStreamReader(response.getEntity()
.getContent()));
StringBuffer sb = new StringBuffer("");
String line = "";
while ((line = in.readLine()) != null) {
sb.append(line);
break;
}
in.close();
responseString = sb.toString();
responseString = responseString.substring(0,
responseString.length() - 1);
// Handle Response
user.orderList.clear();
String[] tmp1 = responseString.split("\\|");
for (String s : tmp1) {
String[] tmp2 = s.split(",");
Order tmp3 = new Order();
tmp3.orderId = tmp2[0];
tmp3.placerName = tmp2[1];
tmp3.itemName = tmp2[2];
tmp3.payment = tmp2[3];
tmp3.locationString = tmp2[4];
if (user.orderList.size() < 1) {
tmp3.yPos = (int) ((user.orderList.size() * orderHeight) + (barHeight * 1.1f)); //
// Shadow of Bar is why 1.1f
tmp3.xPos = 0;
} else {
tmp3.yPos = user.orderList.get(user.orderList
.size() - 1).yPos
+ orderHeight
+ (orderHeight / 7); // + Gap
tmp3.xPos = 0;
}
tmp3.image = orderImage1;
user.orderList.add(tmp3);
}
} catch (Exception e) {
Log.e("log_tag", "Error: " + e.toString());
}
} else {
// Must Login
showMessage("Must Login");
}
}
@Override
protected Boolean doInBackground(Void... params) {
postData();
return null;
}
}
//获取订单列表:列出范围内的订单
公开令{
if(Utility.getCurrentLocation()!=null){
handler.post(新的Runnable(){
公开募捐{
新建ListOrdersTask().execute((Void)null);
}
});
}否则{
//无法确定位置
showMessage(“无法确定位置”);
}
}
类ListOrdersTask扩展了AsyncTask{
私有void postData(){
if(user.loggedIn){
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(
"http://cyberkomm.ch/sidney/php/getOrders.php");
试一试{
ArrayList nameValuePairs=新的ArrayList(
4);
添加(新的BasicNameValuePair(“LatMin”),字符串
.valueOf(user.latitude-seekRange));
添加(新的BasicNameValuePair(“LatMax”),字符串
.valueOf(user.latitude+seekRange));
添加(新的BasicNameValuePair(“LongMin”,字符串
.valueOf(user.longitude-seekRange));
添加(新的BasicNameValuePair(“LongMax”,字符串
.valueOf(user.longide+seekRange));
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpclient.execute(httppost);
BufferedReader in=新的BufferedReader(
新的InputStreamReader(response.getEntity()
.getContent());
StringBuffer sb=新的StringBuffer(“”);
字符串行=”;
而((line=in.readLine())!=null){
某人附加(行);
打破
}
in.close();
responseString=sb.toString();
responseString=responseString.子字符串(0,
响应长度()-1);
//处理响应
user.orderList.clear();
字符串[]tmp1=responseString.split(“\\\\”);
用于(字符串s:tmp1){
字符串[]tmp2=s.split(“,”);
订单tmp3=新订单();
tmp3.orderId=tmp2[0];
tmp3.placerName=tmp2[1];
tmp3.itemName=tmp2[2];
tmp3.payment=tmp2[3];
tmp3.locationString=tmp2[4];
if(user.orderList.size()<1){
tmp3.yPos=(int)((user.orderList.size()*orderHeight)+(barHeight*1.1f))//
//酒吧的阴影为什么是1.1f
tmp3.xPos=0;
}否则{
tmp3.yPos=user.orderList.get(user.orderList
.size()-1).yPos
+订单高度
+(订单高度/7);//+间隙
tmp3.xPos=0;
}
tmp3.image=orderImage1;
user.orderList.add(tmp3);
}
}捕获(例外e){
Log.e(“Log_标记”,“错误:+e.toString());
}
}否则{
//必须登录
showMessage(“必须登录”);
}
}
@凌驾
受保护的布尔doInBackground(Void…params){
postData();
返回null;
}
}
抽签:
for (int i = 0; i < user.orderList.size(); ++i) {
if (user.orderList.size() > 0) {
textSize = scrnWidth / 25;
paint.setTextSize(textSize);
paint.setAlpha(255);
if (user.orderList.size() > 0) {
canvas.drawBitmap(user.orderList.get(i).image,
user.orderList.get(i).xPos,
user.orderList.get(i).yPos, paint);
}
paint.setAlpha(150);
if (user.orderList.size() > 0) {
canvas.drawText(
user.orderList.get(i).itemName,
user.orderList.get(i).xPos + orderHeight
+ (orderHeight / 10),
(user.orderList.get(i).yPos + textSize * 1.85f),
paint);
}
textSize = scrnWidth / 28;
paint.setTextSize(textSize);
if (user.orderList.size() > 0) {
canvas.drawText(
"@" + user.orderList.get(i).placerName,
user.orderList.get(i).xPos + orderHeight
+ (orderHeight / 10),
(user.orderList.get(i).yPos
+ (textSize * 1.85f) + (textSize * 1.25f)),
paint);
}
}
}
for(int i=0;i0){
textSize=scrnWidth/25;
paint.setTextSize(textSize);
油漆。setAlpha(255);
if(user.orderList.size()>0){
drawBitmap(user.orderList.get(i).image,
user.orderList.get(i).xPos,
user.orderList.get(i.yPos,paint);
}
油漆。setAlpha(150);
if(user.orderList.size()>0){
canvas.drawText(
user.orderList.get(i).itemName,
user.orderList.get(i).xPos+orderHeight
+(订单高度/10),
(user.orderList.get(i.yPos+textSize*1.85f),
油漆);
}
textSize=scrnWidth/28;
paint.setTextSize(textSize);
if(user.orderList.size()>0){
canvas.drawText(
“@”+user.orderList.get(i).placerName,
user.orderList.get(i).xPos+orderHeight
+(订单高度/10),