1

I'm trying to show an image and text each list view item. I read a few tutorial but I still could not.

I'm getting this error when I try to open TagsActivity:

Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference

In this line:

this.tagList.setAdapter(adapter);

TagsActivity.java

public class TagsActivity extends AppCompatActivity {
public ListView tagList;
final List<Tag> tags = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.activity_main);

    tags.add(new Tag("Black & White"));
    tags.add(new Tag("Geometric"));

    this.tagList = (ListView) this.findViewById(R.id.list_tags);
    LazyAdapter adapter = new LazyAdapter(this, this.tags);
    this.tagList.setAdapter(adapter);
}
}

Tag.java

public class Tag {
private String title;

public Tag(String title) {
    super();
    this.title = title;
}

public String getTitle() {
    return this.title;
}

public void setTitle(String title) {
    this.title = title;
}
}

JazyAdapter.java

public class LazyAdapter extends BaseAdapter {
private LayoutInflater inflater;
private List<Tag> tags;

public LazyAdapter(Activity activity, List<Tag> tags) {
    this.inflater = (LayoutInflater) activity.getSystemService(
            Context.LAYOUT_INFLATER_SERVICE);
    this.tags = tags;
}

@Override
public int getCount() {
    return this.tags.size();
}

@Override
public Tag getItem(int position) {
    return this.tags.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView (int position, View convertView, ViewGroup parent) {
    View row;
    row = this.inflater.inflate(R.layout.list_item, null);

    TextView textView = (TextView) row.findViewById(R.id.title);
    ImageView imageView = (ImageView) row.findViewById(R.id.icon);

    Tag tag = this.tags.get(position);

    textView.setText(tag.getTitle());
    imageView.setImageResource(R.drawable.icon);

    return row;
}
}

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="15dp"
android:background="@drawable/border_bottom">

<LinearLayout
    android:id="@+id/thumb"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="2dp"
    android:layout_marginRight="5dp"
    android:layout_alignParentLeft="true">
    <ImageView
        android:id="@+id/icon"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="5dp"
        android:src="@drawable/icon" />
</LinearLayout>

<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/thumb"
    android:layout_toRightOf="@+id/thumb"
    android:layout_toEndOf="@+id/thumb"
    android:layout_centerVertical="true"
    android:textStyle="bold"
    android:textSize="24sp"
    android:text="Black &amp; White"
    android:paddingTop="5dp"/>

<ImageView
    android:layout_width="20dp"
    android:layout_height="20dp"
    android:layout_margin="15dp"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:src="@drawable/arrow_right"/>
</RelativeLayout>

activity_tags.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="{relativePackage}.${activityClass}" >

<ListView
    android:id="@+id/list_tags"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
</RelativeLayout>

How can I solve this?

Umut Çağdaş Coşkun
  • 1,197
  • 2
  • 15
  • 33

2 Answers2

0

this.setContentView(R.layout.activity_main); should be this.setContentView(R.layout.activity_tags);

Nathanael
  • 701
  • 5
  • 12
0

You're getting this error because of "View row" is null. Just replace that in your LazyLoader.java

View row;
row = this.inflater.inflate(R.layout.list_item, null);

With:

View row = convertView;
if (row == null) {
    row = this.inflater.inflate(R.layout.list_item, null);
}