2014-01-21 62 views
-1

在我ArrayAdapter我得到這個錯誤的logcat:IndexOutOfBoundsExeption在我的適配器

01-21 18:34:21.705: E/AndroidRuntime(10863): FATAL EXCEPTION: main 
01-21 18:34:21.705: E/AndroidRuntime(10863): Process: it.rs.esempioparsinglistview, PID: 10863 
01-21 18:34:21.705: E/AndroidRuntime(10863): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0 
01-21 18:34:21.705: E/AndroidRuntime(10863): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255) 
01-21 18:34:21.705: E/AndroidRuntime(10863): at java.util.ArrayList.get(ArrayList.java:308) 
01-21 18:34:21.705: E/AndroidRuntime(10863): at it.rs.esempioparsinglistview.ParsingArrayAdapter.getView(ParsingArrayAdapter.java:80) 
01-21 18:34:21.705: E/AndroidRuntime(10863): at android.widget.AbsListView.obtainView(AbsListView.java:2263) 
01-21 18:34:21.705: E/AndroidRuntime(10863): at android.widget.ListView.measureHeightOfChildren(ListView.java:1263) 
01-21 18:34:21.705: E/AndroidRuntime(10863): at android.widget.ListView.onMeasure(ListView.java:1175) 
01-21 18:34:21.705: E/AndroidRuntime(10863): at android.view.View.measure(View.java:16504) 
01-21 18:34:21.705: E/AndroidRuntime(10863): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125) 
01-21 18:34:21.705: E/AndroidRuntime(10863): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404) 
01-21 18:34:21.705: E/AndroidRuntime(10863): at android.widget.LinearLayout.measureVertical(LinearLayout.java:695) 
01-21 18:34:21.705: E/AndroidRuntime(10863): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588) 
01-21 18:34:21.705: E/AndroidRuntime(10863): at android.view.View.measure(View.java:16504) 
01-21 18:34:21.705: E/AndroidRuntime(10863): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125) 
01-21 18:34:21.705: E/AndroidRuntime(10863): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 
01-21 18:34:21.705: E/AndroidRuntime(10863): at android.view.View.measure(View.java:16504) 
01-21 18:34:21.705: E/AndroidRuntime(10863): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125) 
01-21 18:34:21.705: E/AndroidRuntime(10863): at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327) 
01-21 18:34:21.705: E/AndroidRuntime(10863): at android.view.View.measure(View.java:16504) 
01-21 18:34:21.705: E/AndroidRuntime(10863): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125) 
01-21 18:34:21.705: E/AndroidRuntime(10863): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 
01-21 18:34:21.705: E/AndroidRuntime(10863): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2552) 
01-21 18:34:21.705: E/AndroidRuntime(10863): at android.view.View.measure(View.java:16504) 
01-21 18:34:21.705: E/AndroidRuntime(10863): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1916) 
01-21 18:34:21.705: E/AndroidRuntime(10863): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1113) 
01-21 18:34:21.705: E/AndroidRuntime(10863): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1295) 
01-21 18:34:21.705: E/AndroidRuntime(10863): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000) 
01-21 18:34:21.705: E/AndroidRuntime(10863): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670) 
01-21 18:34:21.705: E/AndroidRuntime(10863): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761) 
01-21 18:34:21.705: E/AndroidRuntime(10863): at android.view.Choreographer.doCallbacks(Choreographer.java:574) 
01-21 18:34:21.705: E/AndroidRuntime(10863): at android.view.Choreographer.doFrame(Choreographer.java:544) 
01-21 18:34:21.705: E/AndroidRuntime(10863): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747) 
01-21 18:34:21.705: E/AndroidRuntime(10863): at android.os.Handler.handleCallback(Handler.java:733) 
01-21 18:34:21.705: E/AndroidRuntime(10863): at android.os.Handler.dispatchMessage(Handler.java:95) 
01-21 18:34:21.705: E/AndroidRuntime(10863): at android.os.Looper.loop(Looper.java:136) 
01-21 18:34:21.705: E/AndroidRuntime(10863): at android.app.ActivityThread.main(ActivityThread.java:5081) 
01-21 18:34:21.705: E/AndroidRuntime(10863): at java.lang.reflect.Method.invoke(Native Method) 
01-21 18:34:21.705: E/AndroidRuntime(10863): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:781) 
01-21 18:34:21.705: E/AndroidRuntime(10863): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 

線80是這樣的:

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

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.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 
     } 

     cache.titolo.setText(titoli.get(pos)); //set the title 

     cache.immagini.setImageBitmap(bitmap.get(pos)); //set the image 


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

逸岸,如果我評論該行我沒有得到錯誤,但我無法顯示列表中的圖像..爲什麼這個錯誤?我該怎麼做才能在列表中插入位圖?

FULL 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 IMMAGINE = R.id.imageView1; 

    List<Bitmap> bitmap ; 

    ArrayList<String> titoli; //lista dei titoli 

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


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

    @Override 
    public int getCount() 
    { 
     return titoli.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.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 
     } 

     cache.titolo.setText(titoli.get(pos)); //imposto il titolo 

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


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

    private class CacheRiga { // classe per la cache delle righe 
     public TextView titolo; // cache titolo 
     public ImageView immagini; // cache images 
    } 

} 

MAINACTIVITY:

public class MainActivity extends Activity{ 
    ProgressDialog mProgressDialog; 
    public static final String TAG_TITOLI = "titoli"; 
    private static final String TAG_IMMAGINE = "immagine"; 
    ListView lista; 
    Bitmap bitmap; 
    public ImageView immagine; 
    public ImageView logoimg; 

    static final String BLOG_URL = "http://www.multiplayer.it"; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     lista = (ListView)this.findViewById(R.id.main_lista);//recupero lista da id 



     //View inflatedView = getLayoutInflater().inflate(R.layout.riga_listview, null); 
     //immaginebtn = (Button)findViewById(R.id.immaginebtn); 
     //creo ed eseguo l'asynctask 
     ParsingPaginaWeb parsing = new ParsingPaginaWeb(); 
     parsing.execute(""); 

     // Immagine btn 
     /*immaginebtn.setOnClickListener(new OnClickListener() { 
      public void onClick(View arg0) { 
       // Execute Logo AsyncTask 
       new Logo().execute(); 
      } 
     });*/ 
     //new Logo().execute(); 

     // Launching new screen on Selecting Single ListItem 
      lista.setOnItemClickListener(new OnItemClickListener() { 

       @Override 
       public void onItemClick(AdapterView<?> parent, View view, 
         int position, long id) { 
        // getting values from selected ListItem 
        String titoli = ((TextView) view.findViewById(R.id.riga_listview_titolo)).getText().toString(); 
        immagine = ((ImageView) view.findViewById(R.id.imageView1)); 

        // Starting new intent 
        Intent in = new Intent(getApplicationContext(), SingleActivity.class); 
        in.putExtra(TAG_TITOLI, titoli); 
        in.putExtra(TAG_IMMAGINE, bitmap); 
        //in.putExtra(TAG_CONTENT, cont); 
        startActivity(in); 

       } 
      }); 

    } 

    private class ParsingPaginaWeb extends AsyncTask<String,String,String> { 

     ArrayList<String> titoli; //lista dei titoli 
     ArrayList<Bitmap> bitmap = new ArrayList<Bitmap>(); 
     //ArrayList<String> content; //lista delle descrizioni 

     @Override 
     protected void onPreExecute() 
     { 
      //prima di eseguire il parsing inizializzo gli arraylist 
      mProgressDialog = new ProgressDialog(MainActivity.this); 
      mProgressDialog.setTitle("Multiplayer.it"); 
      mProgressDialog.setMessage("Caricamento articoli..."); 
      mProgressDialog.setIndeterminate(false); 
      mProgressDialog.show(); 

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

     @Override 
     protected String doInBackground(String... params) { 
      try { 

       Document doc = Jsoup.connect(BLOG_URL).get(); 
       Elements nodeBlogStats = doc.select("div.news-col-0 h3"); //per multiplayer.it Elements nodeBlogStats = doc.select("div.news-col-0 h3"); per ftv #comunePartINI > option 
       for(Element sezione : nodeBlogStats) 
       { 
        titoli.add(sezione.text()); 



       } 
      } catch (Exception e) { 
       // In caso di errore 
       Log.e("ESEMPIO", "ERRORE NEL PARSING"); 
      } 
      return null; 
     } 



     @Override 
     protected void onPostExecute(String result) 
     { 
      // dopo che ho eseguito il parsing mostro i dati nella listview 
      // usando il custom array adpater ParsingArrayAdapter 
      ParsingArrayAdapter adapter = new ParsingArrayAdapter(MainActivity.this, titoli, bitmap); 
      lista.setAdapter(adapter); 
      mProgressDialog.dismiss(); 
      new Logo().execute(); 
     } 


    } 

    // Classe per caricamento immagini.. 

    // Logo AsyncTask 

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


     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      mProgressDialog = new ProgressDialog(MainActivity.this); 
      mProgressDialog.setTitle("Immagini"); 
      mProgressDialog.setMessage("Loading images..."); 
      mProgressDialog.setIndeterminate(false); 
      mProgressDialog.show(); 
     } 

     @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 
       //Elements img = document.select("div.news-col-0 img[src]"); 
       // Locate the src attribute 
       for(Element img : document.select("div.news-col-0 img[src]")) { 
        String ImgSrc = img.attr("src"); 
       // Download image from URL 
        InputStream is = new java.net.URL(ImgSrc).openStream(); 
        bitmap = BitmapFactory.decodeStream(is); 
       } 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      return null; 
     } 

     @Override 
     protected void onPostExecute(Void result) { 
      // Set downloaded image into ImageView 
      logoimg = (ImageView) findViewById(R.id.imageView1); 
      logoimg.setImageBitmap(bitmap); 
      mProgressDialog.dismiss(); 

      // ParsingArrayAdapter adapter = new ParsingArrayAdapter(MainActivity.this, titoli); 
      // lista.setAdapter(adapter); 
     } 
    } 

} 
+0

可以顯示完整的適配器類? –

+1

那麼它看起來像「位圖」是空的。你需要弄清楚爲什麼。我們沒有任何關於這方面的信息。 –

+0

如果我刪除該行日誌說:'BitmapNOTnull'所以我不明白! –

回答

1

你不加入位圖到您的ArrayList。我想在您的標誌的AsyncTask,在doInBackground,而不是:

for(Element img : document.select("div.news-col-0 img[src]")) { 
    String ImgSrc = img.attr("src"); 
    // Download image from URL 
    InputStream is = new java.net.URL(ImgSrc).openStream(); 
    bitmap = BitmapFactory.decodeStream(is); 
} 

你想做的事:

for(Element img : document.select("div.news-col-0 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)); 
} 
+0

你是對的,我認爲..但是在'add'中我得到這樣的語法錯誤:'方法添加(位圖)未定義類型位圖' –

+0

這是因爲您的MainActivity中的全局位圖是類型位圖 - 將其更改爲列表。 – Melquiades

+0

我要離開'new Logo()。execute();'in onPostExecute?' –

0

看來你的位圖陣列太小(但不爲空)。

請檢查位圖數組是否具有與titoli數組相同或更大的項。 例如:

public ParsingArrayAdapter(Context context,ArrayList<String> titoli, List<Bitmap> bitmap) 
    { 
     super(context,TITOLO); 
     this.c = context; 
     this.titoli = titoli; 
     this.bitmap = bitmap; 
     this.inflater = LayoutInflater.from(c); 
     if (this.titoli.size() > this.bitmap.size()) 
     Log.d("Array compare", "Titoli > Bitmap!"); 
    } 

和檢查日誌輸出

+0

如何檢查它?想着它我不知道是否有相同的大小..我該怎麼辦? –

+0

我已經更新了我的回答 – Dimmerg

+0

好吧,它返回'01-21 18:55:41.735:D/Array比較(12551):Titoli> Bitmap! '那麼你有什麼建議去做? –