错误/AndroidRuntime(328):引起:java.lang.IndexOutOfBoundsException:索引 10 无效,大小为 10
**如何解决此错误/AndroidRuntime(328): 引起原因:
java.lang.IndexOutOfBoundsException:索引 10 无效,大小为 10 这个问题...**
**Log cat:**
06-15 05:11:39.499: ERROR/AndroidRuntime(328): FATAL EXCEPTION: AsyncTask #2
06-15 05:11:39.499: ERROR/AndroidRuntime(328): java.lang.RuntimeException: An error
occured while executing doInBackground()
06-15 05:11:39.499: ERROR/AndroidRuntime(328): at android.os.AsyncTask$3.done(AsyncTask.java:200)
06-15 05:11:39.499: ERROR/AndroidRuntime(328): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
06-15 05:11:39.499: ERROR/AndroidRuntime(328): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
06-15 05:11:39.499: ERROR/AndroidRuntime(328): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
06-15 05:11:39.499: ERROR/AndroidRuntime(328): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
06-15 05:11:39.499: ERROR/AndroidRuntime(328): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
06-15 05:11:39.499: ERROR/AndroidRuntime(328): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
06-15 05:11:39.499: ERROR/AndroidRuntime(328): at java.lang.Thread.run(Thread.java:1096)
**06-15 05:11:39.499: ERROR/AndroidRuntime(328): Caused by: java.lang.IndexOutOfBoundsException: Invalid index 10, size is 10**
06-15 05:11:39.499: ERROR/AndroidRuntime(328): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
06-15 05:11:39.499: ERROR/AndroidRuntime(328): at java.util.ArrayList.get(ArrayList.java:311)
06-15 05:11:39.499: ERROR/AndroidRuntime(328): at $MyGroupByCategorySync.doInBackground(GroupByCategoryProductSearchMainActivity.java:691)
06-15 05:11:39.499: ERROR/AndroidRuntime(328): at android.os.AsyncTask$2.call(AsyncTask.java:185)
06-15 05:11:39.499: ERROR/AndroidRuntime(328): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
06-15 05:11:39.499: ERROR/AndroidRuntime(328): ... 4 more
代码:
private void createSingleRow(String bycategory_info[],Drawable product_image)
{
Log.e("------------", ".........................................................................................");
Log.v("-->","GroupByCategoryProductSearchMainActivity STARTING createSingleRow()");
String productname = bycategory_info[1];
String city = bycategory_info[4];
String state = bycategory_info[4];
final String strOfferid=bycategory_info[3];
// for(int row_id=0;row_id<no_of_rows;row_id++)
// {
TableRow table_row = new TableRow(this);
TextView tv_blank = new TextView(this);
TextView tv_photo = new TextView(this);
TextView txt_pname = new TextView(this);
TextView txt_city = new TextView(this);
TextView txt_state = new TextView(this);
TextView img_line = new TextView(this);
LinearLayout line_layout = new LinearLayout(this);
LinearLayout row_layout = new LinearLayout(this);
table_row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT ,LayoutParams.FILL_PARENT));
table_row.setPadding(0, 5, 0, 5);
tv_blank.setWidth(11);
tv_photo.setBackgroundDrawable(product_image);
Log.i("--: VALUE :--", "PRODUCT IMAGE SET TO ROW = "+single_row_id+" "+product_image);
tv_photo.setLayoutParams(new TableRow.LayoutParams(45 ,45));
tv_photo.setGravity(Gravity.CENTER_VERTICAL);
txt_pname.setBackgroundResource(R.drawable.empty);
txt_pname.setText(productname);
Log.i("--: VALUE :--", "PRODUCT NAME SET TO ROW = "+single_row_id+" "+productname);
txt_pname.setTextColor(color_black);
txt_pname.setTextSize(14);
txt_pname.setWidth(110);
txt_pname.setHeight(60);
txt_pname.setPadding(10,0,0,0);
txt_pname.setGravity(Gravity.LEFT);
txt_pname.setGravity(Gravity.CENTER_VERTICAL);
txt_city.setBackgroundResource(R.drawable.empty);
txt_city.setText(city);
Log.i("--: VALUE :--", "location NAME SET TO ROW = "+single_row_id+" "+city);
txt_city.setTextColor(color_black);
txt_city.setTextSize(13);
txt_city.setWidth(65);
txt_city.setHeight(60);
txt_city.setPadding(15,0,0,0);
txt_city.setGravity(Gravity.LEFT);
txt_city.setGravity(Gravity.CENTER_VERTICAL);
txt_state.setBackgroundResource(R.drawable.empty);
txt_state.setText(state);
Log.i("--: VALUE :--", "PRODUCT NAME SET TO ROW = "+single_row_id+" "+state);
txt_state.setTextColor(color_black);
txt_state.setTextSize(13);
txt_state.setWidth(65);
txt_state.setHeight(60);
txt_state.setPadding(15,0,0,0);
txt_state.setGravity(Gravity.LEFT);
txt_state.setGravity(Gravity.CENTER_VERTICAL);
img_line.setBackgroundResource(R.drawable.separater_line);
img_line.setLayoutParams(new TableRow.LayoutParams(LayoutParams.FILL_PARENT ,2));
line_layout.setGravity(Gravity.CENTER_HORIZONTAL);
//table_row.setBackgroundColor(color_white);
/* table_row.setGravity(Gravity.CENTER_VERTICAL);
table_row.addView(tv_blank);
table_row.addView(tv_photo);
table_row.addView(btn_name);*/
row_layout.setGravity(Gravity.CENTER_VERTICAL);
row_layout.addView(tv_blank);
row_layout.addView(tv_photo);
row_layout.addView(txt_pname);
row_layout.addView(txt_city);
row_layout.addView(txt_state);
table_row.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
XMLDataParser.parseXML(XMLDataParser.GROUP_PRODUCT_ITEM_DETAIL_PARSER_CODE,strOfferid);
Intent intent_show_detail = new Intent(GroupByCategoryProductSearchMainActivity.this,GroupByCategoryItemDetail.class);
Toast.makeText(GroupByCategoryProductSearchMainActivity.this, "Row pressed", 1000);
startActivity(intent_show_detail);
finish();
}
});
table_row.addView(row_layout);
line_layout.addView(img_line);
tl_group_product_list_by_category.addView(table_row);
tl_group_product_list_by_category.addView(line_layout);
// }
Log.v("-->","GroupByCategoryProductSearchMainActivity ENDING createSingleRow()");
single_row_id++;
}
问题:
class MyGroupByCategorySync extends AsyncTask
{
String bycategory_info[] = new String[9];
Drawable product_image = null;
int no_of_rows = list_productname.size();
@Override
protected Object doInBackground(Object... params)
{
Log.i("--: doInBackground :-- ", "no_of_rows = "+no_of_rows);
for(int i=0;i<no_of_rows;i++)
{
bycategory_info[0] = list_productid.get(i).toString();
bycategory_info[1] = list_productname.get(i).toString();
bycategory_info[2] = list_offername.get(i).toString();
bycategory_info[3] = list_offerid.get(i).toString();
bycategory_info[4] = list_location.get(i).toString();
bycategory_info[5] = list_price.get(i).toString();
bycategory_info[6] = list_prdescription.get(i).toString();
bycategory_info[7] = list_catname.get(i).toString();
bycategory_info[8] = list_categoryid.get(i).toString();
product_image = loadImageFromWebOperations(list_thumbnail.get(i).toString());
XMLData.group_by_category_product_image_list.add(product_image);
if(!is_searched)
XMLData.initial_list_of_product_by_category.add(new ByCategory(bycategory_info[0],bycategory_info[1],list_thumbnail.get(i).toString(),bycategory_info[2],bycategory_info[3],bycategory_info[4],bycategory_info[5],bycategory_info[6],bycategory_info[7],bycategory_info[8], product_image));
else
XMLData.searched_list_of_product_by_category.add(new ByCategory(bycategory_info[0],bycategory_info[1],list_thumbnail.get(i).toString(),bycategory_info[2],bycategory_info[3],bycategory_info[4],bycategory_info[5],bycategory_info[6],bycategory_info[7],bycategory_info[8], product_image));
publishProgress();
SystemClock.sleep(15);
}
Log.v("-->","ENDING doInBackground()");
XMLData.is_by_category_product_data_parsed = true;
return null;
}
@Override
protected void onPostExecute(Object result)
{
super.onPostExecute(result);
pressed_once = false;
single_row_id = 0;
Log.i("--: VALUE AFTER onPostExecute:--","pressed_once = "+pressed_once);
}
@Override
protected void onProgressUpdate(Object... values)
{
createSingleRow(bycategory_info, product_image);
}
}
**How to solve this ERROR/AndroidRuntime(328):
Caused by:
java.lang.IndexOutOfBoundsException: Invalid index 10, size is 10
this problem ...**
**Log cat:**
06-15 05:11:39.499: ERROR/AndroidRuntime(328): FATAL EXCEPTION: AsyncTask #2
06-15 05:11:39.499: ERROR/AndroidRuntime(328): java.lang.RuntimeException: An error
occured while executing doInBackground()
06-15 05:11:39.499: ERROR/AndroidRuntime(328): at android.os.AsyncTask$3.done(AsyncTask.java:200)
06-15 05:11:39.499: ERROR/AndroidRuntime(328): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
06-15 05:11:39.499: ERROR/AndroidRuntime(328): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
06-15 05:11:39.499: ERROR/AndroidRuntime(328): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
06-15 05:11:39.499: ERROR/AndroidRuntime(328): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
06-15 05:11:39.499: ERROR/AndroidRuntime(328): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
06-15 05:11:39.499: ERROR/AndroidRuntime(328): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
06-15 05:11:39.499: ERROR/AndroidRuntime(328): at java.lang.Thread.run(Thread.java:1096)
**06-15 05:11:39.499: ERROR/AndroidRuntime(328): Caused by: java.lang.IndexOutOfBoundsException: Invalid index 10, size is 10**
06-15 05:11:39.499: ERROR/AndroidRuntime(328): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
06-15 05:11:39.499: ERROR/AndroidRuntime(328): at java.util.ArrayList.get(ArrayList.java:311)
06-15 05:11:39.499: ERROR/AndroidRuntime(328): at $MyGroupByCategorySync.doInBackground(GroupByCategoryProductSearchMainActivity.java:691)
06-15 05:11:39.499: ERROR/AndroidRuntime(328): at android.os.AsyncTask$2.call(AsyncTask.java:185)
06-15 05:11:39.499: ERROR/AndroidRuntime(328): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
06-15 05:11:39.499: ERROR/AndroidRuntime(328): ... 4 more
Code :
private void createSingleRow(String bycategory_info[],Drawable product_image)
{
Log.e("------------", ".........................................................................................");
Log.v("-->","GroupByCategoryProductSearchMainActivity STARTING createSingleRow()");
String productname = bycategory_info[1];
String city = bycategory_info[4];
String state = bycategory_info[4];
final String strOfferid=bycategory_info[3];
// for(int row_id=0;row_id<no_of_rows;row_id++)
// {
TableRow table_row = new TableRow(this);
TextView tv_blank = new TextView(this);
TextView tv_photo = new TextView(this);
TextView txt_pname = new TextView(this);
TextView txt_city = new TextView(this);
TextView txt_state = new TextView(this);
TextView img_line = new TextView(this);
LinearLayout line_layout = new LinearLayout(this);
LinearLayout row_layout = new LinearLayout(this);
table_row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT ,LayoutParams.FILL_PARENT));
table_row.setPadding(0, 5, 0, 5);
tv_blank.setWidth(11);
tv_photo.setBackgroundDrawable(product_image);
Log.i("--: VALUE :--", "PRODUCT IMAGE SET TO ROW = "+single_row_id+" "+product_image);
tv_photo.setLayoutParams(new TableRow.LayoutParams(45 ,45));
tv_photo.setGravity(Gravity.CENTER_VERTICAL);
txt_pname.setBackgroundResource(R.drawable.empty);
txt_pname.setText(productname);
Log.i("--: VALUE :--", "PRODUCT NAME SET TO ROW = "+single_row_id+" "+productname);
txt_pname.setTextColor(color_black);
txt_pname.setTextSize(14);
txt_pname.setWidth(110);
txt_pname.setHeight(60);
txt_pname.setPadding(10,0,0,0);
txt_pname.setGravity(Gravity.LEFT);
txt_pname.setGravity(Gravity.CENTER_VERTICAL);
txt_city.setBackgroundResource(R.drawable.empty);
txt_city.setText(city);
Log.i("--: VALUE :--", "location NAME SET TO ROW = "+single_row_id+" "+city);
txt_city.setTextColor(color_black);
txt_city.setTextSize(13);
txt_city.setWidth(65);
txt_city.setHeight(60);
txt_city.setPadding(15,0,0,0);
txt_city.setGravity(Gravity.LEFT);
txt_city.setGravity(Gravity.CENTER_VERTICAL);
txt_state.setBackgroundResource(R.drawable.empty);
txt_state.setText(state);
Log.i("--: VALUE :--", "PRODUCT NAME SET TO ROW = "+single_row_id+" "+state);
txt_state.setTextColor(color_black);
txt_state.setTextSize(13);
txt_state.setWidth(65);
txt_state.setHeight(60);
txt_state.setPadding(15,0,0,0);
txt_state.setGravity(Gravity.LEFT);
txt_state.setGravity(Gravity.CENTER_VERTICAL);
img_line.setBackgroundResource(R.drawable.separater_line);
img_line.setLayoutParams(new TableRow.LayoutParams(LayoutParams.FILL_PARENT ,2));
line_layout.setGravity(Gravity.CENTER_HORIZONTAL);
//table_row.setBackgroundColor(color_white);
/* table_row.setGravity(Gravity.CENTER_VERTICAL);
table_row.addView(tv_blank);
table_row.addView(tv_photo);
table_row.addView(btn_name);*/
row_layout.setGravity(Gravity.CENTER_VERTICAL);
row_layout.addView(tv_blank);
row_layout.addView(tv_photo);
row_layout.addView(txt_pname);
row_layout.addView(txt_city);
row_layout.addView(txt_state);
table_row.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
XMLDataParser.parseXML(XMLDataParser.GROUP_PRODUCT_ITEM_DETAIL_PARSER_CODE,strOfferid);
Intent intent_show_detail = new Intent(GroupByCategoryProductSearchMainActivity.this,GroupByCategoryItemDetail.class);
Toast.makeText(GroupByCategoryProductSearchMainActivity.this, "Row pressed", 1000);
startActivity(intent_show_detail);
finish();
}
});
table_row.addView(row_layout);
line_layout.addView(img_line);
tl_group_product_list_by_category.addView(table_row);
tl_group_product_list_by_category.addView(line_layout);
// }
Log.v("-->","GroupByCategoryProductSearchMainActivity ENDING createSingleRow()");
single_row_id++;
}
Problem in :
class MyGroupByCategorySync extends AsyncTask
{
String bycategory_info[] = new String[9];
Drawable product_image = null;
int no_of_rows = list_productname.size();
@Override
protected Object doInBackground(Object... params)
{
Log.i("--: doInBackground :-- ", "no_of_rows = "+no_of_rows);
for(int i=0;i<no_of_rows;i++)
{
bycategory_info[0] = list_productid.get(i).toString();
bycategory_info[1] = list_productname.get(i).toString();
bycategory_info[2] = list_offername.get(i).toString();
bycategory_info[3] = list_offerid.get(i).toString();
bycategory_info[4] = list_location.get(i).toString();
bycategory_info[5] = list_price.get(i).toString();
bycategory_info[6] = list_prdescription.get(i).toString();
bycategory_info[7] = list_catname.get(i).toString();
bycategory_info[8] = list_categoryid.get(i).toString();
product_image = loadImageFromWebOperations(list_thumbnail.get(i).toString());
XMLData.group_by_category_product_image_list.add(product_image);
if(!is_searched)
XMLData.initial_list_of_product_by_category.add(new ByCategory(bycategory_info[0],bycategory_info[1],list_thumbnail.get(i).toString(),bycategory_info[2],bycategory_info[3],bycategory_info[4],bycategory_info[5],bycategory_info[6],bycategory_info[7],bycategory_info[8], product_image));
else
XMLData.searched_list_of_product_by_category.add(new ByCategory(bycategory_info[0],bycategory_info[1],list_thumbnail.get(i).toString(),bycategory_info[2],bycategory_info[3],bycategory_info[4],bycategory_info[5],bycategory_info[6],bycategory_info[7],bycategory_info[8], product_image));
publishProgress();
SystemClock.sleep(15);
}
Log.v("-->","ENDING doInBackground()");
XMLData.is_by_category_product_data_parsed = true;
return null;
}
@Override
protected void onPostExecute(Object result)
{
super.onPostExecute(result);
pressed_once = false;
single_row_id = 0;
Log.i("--: VALUE AFTER onPostExecute:--","pressed_once = "+pressed_once);
}
@Override
protected void onProgressUpdate(Object... values)
{
createSingleRow(bycategory_info, product_image);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您已编写
int no_of_rows = list_productname.size()
。是否所有 arraylist 的大小都大于或等于list_productname
的大小意味着如果任何 arraylist 的大小小于list_productname
则将导致 ArrayIndexOutOfBoundsExcption。因为您正在循环从多个数组列表获取数据,直到达到 list_productname 大小,这是错误的。由于此逻辑而受到影响的其他数组列表有
list_productid、list_offername、list_offerid
等。您假设其他 arraylist 具有最小no_of_rows
数据数量。谢谢
迪帕克
You have written
int no_of_rows = list_productname.size()
. Does all the arraylist have size greater than or equal to that oflist_productname
means if any arraylist have lesser size thanlist_productname
then that will cause ArrayIndexOutOfBoundsExcption.because you are looping to get data from several arraylist until you reach list_productname size which is wrong. other array list which suffers because of this logic are
list_productid, list_offername, list_offerid
etc. You are asuming that other arraylist have minimumno_of_rows
number of data.Thanks
Deepak