java - Error on setting custom ListView Image -


i want set custom listview arrayadapter. every listitem has imageview , 2 textviews.

the layout of each item rowlayout.xml:

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" >  <imageview     android:id="@+id/item_icon"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_alignparentleft="true"     android:layout_alignparenttop="true"     android:adjustviewbounds="true"     android:maxheight="80dp"     android:maxwidth="80dp"     android:src="@drawable/logo" />      <textview     android:id="@+id/item_title"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_above="@+id/item_subtitle"     android:layout_torightof="@+id/item_icon"     android:text="titel"     android:textappearance="?android:attr/textappearancelarge" />  <textview     android:id="@+id/item_subtitle"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_alignbottom="@+id/item_icon"     android:layout_alignleft="@+id/item_title"     android:layout_marginbottom="16dp"     android:text="untertitel" />  </relativelayout> 

this how set listview in badges activity:

public class badges extends actionbaractivity{  list<badge> mybadges = new arraylist<badge>();  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.badges_layout);      populatebadgelist();     populatelistview();  }  private void populatebadgelist() {     mybadges.add(new badge("some text", "some further text", r.drawable.badge1));     //and on }  private void populatelistview() {     arrayadapter<badge> adapter = new mylistadapter();     listview list = (listview) findviewbyid(r.id.listview);     list.setadapter(adapter); }  private class mylistadapter extends arrayadapter<badge> {     public mylistadapter() {         super(badges.this, r.layout.rowlayout, mybadges);     }      @override     public view getview(int position, view convertview, viewgroup parent) {          view itemview = convertview;         if(itemview == null) {             itemview = getlayoutinflater().inflate(r.layout.rowlayout, parent, false);         }          badge currentbadge = mybadges.get(position);          imageview itemicon = (imageview) findviewbyid(r.id.item_icon);         //the error on following line:         itemicon.setimageresource(currentbadge.geticonid());          textview itemtitle = (textview) findviewbyid(r.id.item_title);         itemtitle.settext(currentbadge.gettitle());          textview itemsubtitle = (textview) findviewbyid(r.id.item_subtitle);         itemsubtitle.settext(currentbadge.getsubtitle());          return itemview;     } } 

here badge class:

public class badge { string title; string subtitle; int iconid;  public badge(string title, string subtitle, int iconid) {     this.title = title;     this.subtitle = subtitle;     this.iconid = iconid; }  public string gettitle() {     return title; }  public string getsubtitle() {     return subtitle; }  public int geticonid() {     return iconid; } 

}

i following error:

java.lang.nullpointerexception: attempt invoke virtual method 'void android.widget.imageview.setimageresource(int)' on null object reference 

i marked line @ error occurs in code. reason this?

instead of using

imageview itemicon = (imageview) findviewbyid(r.id.item_icon); textview itemtitle = (textview) findviewbyid(r.id.item_title); textview itemsubtitle = (textview) findviewbyid(r.id.item_subtitle); 

use

imageview itemicon = (imageview) itemview.findviewbyid(r.id.item_icon); textview itemtitle = (textview) itemview.findviewbyid(r.id.item_title); textview itemsubtitle = (textview)itemview.findviewbyid(r.id.item_subtitle); 

Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

android - CollapsingToolbarLayout: position the ExpandedText programmatically -

Listeners to visualise results of load test in JMeter -