0

I want to set a custom ListView with ArrayAdapter. Every ListItem has an ImageView and two 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 is how I set the ListView in my 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 so 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 is on the 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 is the 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 get the following error:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference

I marked the line at which the error occurs in the code. What could be the reason for this?

4 Answers 4

1

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);
Sign up to request clarification or add additional context in comments.

Comments

1

Replace

ImageView itemIcon = (ImageView) findViewById(R.id.item_icon);

this with

ImageView itemIcon = (ImageView) itemView.findViewById(R.id.item_icon);

and similarly

TextView itemTitle = (TextView) itemView.findViewById(R.id.item_title);
TextView itemSubtitle = (TextView) itemView.findViewById(R.id.item_subTitle);

with

TextView itemTitle = (TextView) findViewById(R.id.item_title);
TextView itemSubtitle = (TextView) findViewById(R.id.item_subTitle);

Comments

0

the view you just inflate didn't add to your activity's layout. So with the activity method

ImageView itemIcon = (ImageView) findViewById(R.id.item_icon);
itemIcon.setImageResource(currentBadge.getIconID());

You couldn't find the view with the specific id. Instead, you should use

ImageView itemIcon = (ImageView) itemView.findViewById(R.id.item_icon);
itemIcon.setImageResource(currentBadge.getIconID());

It will work properly. Other views are similar.

Comments

0

I think your problem is in this line: ArrayAdapter adapter = new MyListAdapter();

Change to this: MyListAdapter adapter = new MyListAdapter();

Also, put item view in front of findViewById methods (ImageView itemIcon = (ImageView) itemview.findViewById(R.id.item_icon); )

I would actually recommend generalising your adapter class, so that you could pass any resource (row layout) and list to it :) (However, it is not necessary for the app to work.)

So something like this:

private class MyListAdapter extends ArrayAdapter<Badge> {

   int resource;
   public MyListAdapter(Context context, int resource, List<Badge> items) {
       super(context, resource, items);
       this.resource = resource;
   }

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View itemView = convertView;
    if(itemView == null) {
        itemView = getLayoutInflater().inflate(resource, parent, false);
    }

    Badge currentBadge = getItem(position);

    ImageView itemIcon = (ImageView) itemview.findViewById(R.id.item_icon);
    //the error is on the following line:
    itemIcon.setImageResource(currentBadge.getIconID());

    TextView itemTitle = (TextView) itemview.findViewById(R.id.item_title);
    itemTitle.setText(currentBadge.getTitle());

    TextView itemSubtitle = (TextView) itemview.findViewById(R.id.item_subTitle);
    itemSubtitle.setText(currentBadge.getSubtitle());

    return itemView;
   }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.