在我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);
}
}
}
可以顯示完整的適配器類? –
那麼它看起來像「位圖」是空的。你需要弄清楚爲什麼。我們沒有任何關於這方面的信息。 –
如果我刪除該行日誌說:'BitmapNOTnull'所以我不明白! –