ERROR / AndroidRuntime(328):引起:java.lang.IndexOutOfBoundsException:索引10无效,大小为10

时间:2011-06-15 05:28:04

标签: android android-xml android-asynctask

**如何解决这个ERROR / 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);
    }
}

1 个答案:

答案 0 :(得分:1)

您已撰写int no_of_rows = list_productname.size()。所有arraylist的大小是否大于或等于list_productname的大小是否意味着如果任何arraylist的大小小于list_productname那么将导致ArrayIndexOutOfBoundsExcption。 因为你循环从几个arraylist获取数据,直到你达到list_productname大小是错误的。由于这个逻辑而受到影响的其他数组列表是list_productid, list_offername, list_offerid等。你可以认为其他arraylist的数据数量最少no_of_rows

由于 迪帕克