java.lang.IndexOutOfBoundsException:索引0无效,大小为0随机

时间:2014-02-17 14:58:29

标签: java android

我知道这种问题不容易解决,因为可能是一切的原因。无论如何我试着解释..我有一个Listview解析网页中的一些标签..我需要的信息是:页面中每篇文章的位图,标题,描述以及文章发布的时间。一切顺利,但有时,随机,我收到一个错误,日志是:

02-17 15:41:13.852: E/ESEMPIO(24448): ERRORE NEL PARSING
02-17 15:41:17.192: E/AndroidRuntime(24448): FATAL EXCEPTION: main
02-17 15:41:17.192: E/AndroidRuntime(24448): Process: it.dd.multiplayerit, PID: 24448
02-17 15:41:17.192: E/AndroidRuntime(24448): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
02-17 15:41:17.192: E/AndroidRuntime(24448):    at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
02-17 15:41:17.192: E/AndroidRuntime(24448):    at java.util.ArrayList.get(ArrayList.java:308)
02-17 15:41:17.192: E/AndroidRuntime(24448):    at it.dd.multiplayerit.ParsingArrayAdapter.getView(ParsingArrayAdapter.java:120)
02-17 15:41:17.192: E/AndroidRuntime(24448):    at android.widget.AbsListView.obtainView(AbsListView.java:2263)
02-17 15:41:17.192: E/AndroidRuntime(24448):    at android.widget.ListView.makeAndAddView(ListView.java:1790)
02-17 15:41:17.192: E/AndroidRuntime(24448):    at android.widget.ListView.fillDown(ListView.java:691)
02-17 15:41:17.192: E/AndroidRuntime(24448):    at android.widget.ListView.fillFromTop(ListView.java:752)
02-17 15:41:17.192: E/AndroidRuntime(24448):    at android.widget.ListView.layoutChildren(ListView.java:1630)
02-17 15:41:17.192: E/AndroidRuntime(24448):    at android.widget.AbsListView.onLayout(AbsListView.java:2091)
02-17 15:41:17.192: E/AndroidRuntime(24448):    at android.view.View.layout(View.java:14824)
02-17 15:41:17.192: E/AndroidRuntime(24448):    at android.view.ViewGroup.layout(ViewGroup.java:4631)
02-17 15:41:17.192: E/AndroidRuntime(24448):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
02-17 15:41:17.192: E/AndroidRuntime(24448):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
02-17 15:41:17.192: E/AndroidRuntime(24448):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
02-17 15:41:17.192: E/AndroidRuntime(24448):    at android.view.View.layout(View.java:14824)
02-17 15:41:17.192: E/AndroidRuntime(24448):    at android.view.ViewGroup.layout(ViewGroup.java:4631)
02-17 15:41:17.192: E/AndroidRuntime(24448):    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
02-17 15:41:17.192: E/AndroidRuntime(24448):    at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
02-17 15:41:17.192: E/AndroidRuntime(24448):    at android.view.View.layout(View.java:14824)
02-17 15:41:17.192: E/AndroidRuntime(24448):    at android.view.ViewGroup.layout(ViewGroup.java:4631)
02-17 15:41:17.192: E/AndroidRuntime(24448):    at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:374)
02-17 15:41:17.192: E/AndroidRuntime(24448):    at android.view.View.layout(View.java:14824)
02-17 15:41:17.192: E/AndroidRuntime(24448):    at android.view.ViewGroup.layout(ViewGroup.java:4631)
02-17 15:41:17.192: E/AndroidRuntime(24448):    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
02-17 15:41:17.192: E/AndroidRuntime(24448):    at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
02-17 15:41:17.192: E/AndroidRuntime(24448):    at android.view.View.layout(View.java:14824)
02-17 15:41:17.192: E/AndroidRuntime(24448):    at android.view.ViewGroup.layout(ViewGroup.java:4631)
02-17 15:41:17.192: E/AndroidRuntime(24448):    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1987)
02-17 15:41:17.192: E/AndroidRuntime(24448):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1744)
02-17 15:41:17.192: E/AndroidRuntime(24448):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
02-17 15:41:17.192: E/AndroidRuntime(24448):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
02-17 15:41:17.192: E/AndroidRuntime(24448):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
02-17 15:41:17.192: E/AndroidRuntime(24448):    at android.view.Choreographer.doCallbacks(Choreographer.java:574)
02-17 15:41:17.192: E/AndroidRuntime(24448):    at android.view.Choreographer.doFrame(Choreographer.java:544)
02-17 15:41:17.192: E/AndroidRuntime(24448):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
02-17 15:41:17.192: E/AndroidRuntime(24448):    at android.os.Handler.handleCallback(Handler.java:733)
02-17 15:41:17.192: E/AndroidRuntime(24448):    at android.os.Handler.dispatchMessage(Handler.java:95)
02-17 15:41:17.192: E/AndroidRuntime(24448):    at android.os.Looper.loop(Looper.java:136)
02-17 15:41:17.192: E/AndroidRuntime(24448):    at android.app.ActivityThread.main(ActivityThread.java:5081)
02-17 15:41:17.192: E/AndroidRuntime(24448):    at java.lang.reflect.Method.invoke(Native Method)
02-17 15:41:17.192: E/AndroidRuntime(24448):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:781)
02-17 15:41:17.192: E/AndroidRuntime(24448):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)

ParsingArrayAdapter的第120行,我有这一行:

cache.immagini.setImageBitmap(bitmap.get(pos));

要让我需要的所有值首先解析标题,描述和日期..然后是位图。这是MainActivity中的代码,其中我填充了listview

private class ParsingPaginaWeb extends AsyncTask<String,String,String> {
        //ArrayList<Bitmap> bitmap = new ArrayList<Bitmap>(); 
        //ArrayList<String> content; //lista delle descrizioni

        @Override
        protected void onPreExecute()
        {   
            //prima di eseguire il parsing inizializzo gli arraylist

            titoli = new ArrayList<String>();
            bitmap = new ArrayList<Bitmap>(); 
            data = new ArrayList<String>();
            categoria = new ArrayList<String>();

            //content = new ArrayList<String>();
        }

        @Override
        protected String doInBackground(String... params) {
            try {
                Document doc = Jsoup.connect(BLOG_URL)
                        .get();
                Elements sezioni = doc.getElementsByClass("archive_box");
                for (Element riga : sezioni) {

                    Element info = riga.getElementsByClass("text").first();

                    String titolo = riga.getElementsByTag("h2").first().text();
                    System.out.println(titolo);
                    titoli.add(titolo);

                    String date = info.getElementsByClass("date").first().text();
                    System.out.println(date);
                    data.add(date);

                    // categoria
                    String category = info.getElementsByClass("category").first().text();
                    System.out.println(category);
                    categoria.add(category);               

                }

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }


        @Override
        protected void onPostExecute(String result)
        {

            new Logo().execute();

        }

// for bitmap

// Logo AsyncTask

    private class Logo extends AsyncTask<Void, Void, Void> {


        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            mProgressDialog = new ProgressDialog(MainActivity.this);
            mProgressDialog.setTitle("Multiplayer.it");
            mProgressDialog.setMessage("Caricamento articoli...");
            mProgressDialog.setIndeterminate(false);
            mProgressDialog.show();
            mProgressDialog.setCanceledOnTouchOutside(false);

        }

        @Override
        protected Void doInBackground(Void... params) {

            try {
                // Connect to the web site
                Document document = Jsoup.connect(BLOG_URL).get();
                // Using Elements to get the class data 
                // Locate the src attribute
                for(Element img : document.select("div.col-1-1 img[src]")) {
                    String ImgSrc = img.attr("src");
                    // Download image from URL
                    InputStream is = new java.net.URL(ImgSrc).openStream();

                    //add Bitmap to an array
                    bitmap.add(BitmapFactory.decodeStream(is));
                 }
            } catch (IOException e) {
                e.printStackTrace();
                Log.e("ESEMPIO", "ERRORE NEL PARSING DELLE IMMAGINI");
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            // Set downloaded image into ImageView
            // Controllo se bitmap è nullo
            if (bitmap!=null){
                Log.d("Bitmap MP.it", "BitmapNOTnull");         
            }else{
                Log.d("Bitmap null MP.it", "Bitmapnull");    
            }

            // Check if titoli is empty
            if (titoli.isEmpty()){
                Log.d("Array empty"empty");
            }


            logoimg = (ImageView) findViewById(R.id.imageView1);


            mProgressDialog.dismiss();

            ParsingArrayAdapter adapter = new ParsingArrayAdapter(MainActivity.this, titoli, bitmap, data, categoria);
            lista.setAdapter(adapter);
        }
    }

哪里可能是问题?

使用适配器编辑

public class ParsingArrayAdapter extends ArrayAdapter<String>{

    //riferimenti statici alle risorse e agli id
    private final static int LAYOUT = R.layout.riga_listview;
    private final static int TITOLO = R.id.riga_listview_titolo;
    private final static int DATA = R.id.riga_listview_data;
    private final static int CATEGORIA = R.id.riga_listview_categoria;
    private final static int IMMAGINE = R.id.imageView1;
    //links
   // private final static int LINKS = R.id.riga_listview_links;

    ArrayList<Bitmap> bitmap;


    ArrayList<String> links;
    ArrayList<String> titoli; //lista dei titoli

    ArrayList<String> data; //lista delle date
    ArrayList<String> categoria; //lista delle categorie

    Context c; //context
    LayoutInflater inflater; //layout inflater

    //ImageLoader imageLoader=new  ImageLoader(ParsingArrayAdapter.getApplicationContext());

    public ParsingArrayAdapter(Context context,ArrayList<String> titoli, ArrayList<Bitmap> bitmap, ArrayList<String> data, ArrayList<String> categoria)
    {
        super(context,TITOLO);
        this.c = context;
        this.titoli = titoli;
        //this.links = links;
        this.data = data;
        this.categoria = categoria;
        this.bitmap = bitmap;
        this.inflater = LayoutInflater.from(c);

        if (this.titoli.size() > this.bitmap.size()){
            Log.d("Array compare", "Titoli > Bitmap!"+ " " + titoli.size() + " " + bitmap.size()); 
            titoli.remove(titoli.size() - 1);
        }

        if (this.titoli.size() == this.bitmap.size())
            Log.d("Array compare", "Titoli = Bitmap!" + " " + titoli.size() + " " + bitmap.size());  

        if (this.titoli.size() > this.data.size())
            Log.d("Array compare", "Titoli > Data!"+ " " + titoli.size() + " " + data.size());
        if (this.titoli.size() == this.data.size())
            Log.d("Array compare", "Titoli = Bitmap!" + " " + titoli.size() + " " + data.size());

    }

    @Override
    public int getCount()
    {
        return titoli.size();

        //return bitmap.size();//ritorno lunghezza lista ( = numero dei titoli)
    }


    //quando la lista richiede una view
    @SuppressLint("NewApi")
    @Override
    public View getView(int pos,View view,ViewGroup parent)
    {
        CacheRiga cache; //cache
        if(view==null)//se è la prima volta che viene richiesta la view
        {
            // creo la view ma non l'attacco alla lista in quanto devo ancora modificare
            // i testi delle textview
            view = inflater.inflate(LAYOUT, parent,false); 
            cache = new CacheRiga(); //inizializzo la cache
            cache.titolo = (TextView) view.findViewById(TITOLO); //collego titolo
            cache.dateArticoli = (TextView) view.findViewById(DATA); //collego la data
            cache.categoriatext = (TextView) view.findViewById(CATEGORIA); //collego la data

            //cache.linkstext = (TextView) view.findViewById(LINKS); //collego la data


            cache.immagini = (ImageView) view.findViewById(IMMAGINE);//collego descrizione


            view.setTag(cache);//collego view con cache
        }
        else
        {
            cache = (CacheRiga) view.getTag(); //altrimenti prendo la cache dalla view
        }
        if (this.titoli.size() > 0 && pos < this.titoli.size()){
            cache.titolo.setText(titoli.get(pos)); //imposto il titolo
        }

        /*if (this.links.size() > 0 && pos < this.links.size()){
            cache.linkstext.setText(links.get(pos)); //imposto il titolo
        }*/

        if (this.data.size() > 0 && pos < this.data.size()){
            cache.dateArticoli.setText(data.get(pos));       
       }
        cache.categoriatext.setText(categoria.get(pos));    
        cache.immagini.setImageBitmap(bitmap.get(pos));

        //ImageView iv = (ImageView) view.findViewById(IMMAGINE);
        //iv.setImageBitmap(getItem(pos));


        if (bitmap!=null){
            Log.d("Bitmap MP.it", "BitmapNOTnull");         
        }else{
            Log.d("Bitmap null MP.it", "Bitmapnull");    
        }

        if (data!=null){
            Log.d("Data", "Data NOT null");         
        }else{
            Log.d("Data", "Data null");    
        }


        return view;
    }

    private class CacheRiga { // classe per la cache delle righe
        public TextView titolo; // cache titolo
        public TextView dateArticoli; // cache data
        public TextView categoriatext; // cache categoria
        //public TextView linkstext;
        public ImageView immagini; // cache descrizione
    }

0 个答案:

没有答案