0

This is my MainActivity.

public class MainActivity extends Activity implements OnItemClickListener {


public static final String[] titles = new String[] { "Strawberry",
        "Banana", "Orange", "Mixed" };

public static final String[] descriptions = new String[] {
        "It is an aggregate accessory fruit",
        "It is the largest herbaceous flowering plant", "Citrus Fruit",
        "Mixed Fruits" };

public static final int[] image = { R.drawable.a_1, R.drawable.a_2,
        R.drawable.a_3, R.drawable.a_4 };

public ListView listView;
List<RowItem> rowItems;

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

    rowItems = new ArrayList<RowItem>();
    for (int i = 0; i < titles.length; i++) {
        RowItem item = new RowItem(image[i], titles[i], descriptions[i]);
        rowItems.add(item);
    }

    listView = (ListView) findViewById(R.id.list);
    customeBaseAdapter adapter = new customeBaseAdapter(this, rowItems);
    listView.setAdapter(adapter);
    listView.setOnItemClickListener(this);
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
        long id) {
    // TODO Auto-generated method stub
}
}

This is my CustomAdapter

public class customeBaseAdapter extends BaseAdapter {


Context context;
List<RowItem> rowItems;

public customeBaseAdapter(Context context, List<RowItem> items) {
    // TODO Auto-generated constructor stub

    this.context = context;
    this.rowItems = items;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return 4;
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return rowItems.get(position);
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public View getView(int position, View view, ViewGroup group) {
    // TODO Auto-generated method stub
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (view == null) {
        view = inflater.inflate(R.layout.list_item, null);
    }

    ImageView imageView = (ImageView) view.findViewById(R.id.image);
    TextView titleTextView = (TextView) view.findViewById(R.id.title);
    TextView desc = (TextView) view.findViewById(R.id.desc);
    imageView.setImageResource(rowItems.get(position).getImageId());
    titleTextView.setText(rowItems.get(position).getTitle());
    desc.setText(rowItems.get(position).getDesc());
    return view;
}
}

This is my RowItem class

package com.example.customlistview;

import android.R.integer;

public class RowItem {

private int imageId;
private String title;
private String desc;

public RowItem(int imageId, String title, String desc) {
    this.imageId = imageId;
    this.title = title;
    this.desc = desc;
}

public int getImageId() {
    return imageId;
}

public void setImageId(int imageId) {
    this.imageId = imageId;
}

public String getDesc() {
    return desc;
}

public void setDesc(String desc) {
    this.desc = desc;
}

public String getTitle() {
    return title;
}

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

@Override
public String toString() {
    return title + "\n" + desc;
}
}

Logcat error.

10-15 01:04:05.135: E/AndroidRuntime(9901): FATAL EXCEPTION: main
10-15 01:04:05.135: E/AndroidRuntime(9901): java.lang.NullPointerException
10-15 01:04:05.135: E/AndroidRuntime(9901):     at com.example.customlistview.customeBaseAdapter.getView(customeBaseAdapter.java:56)
10-15 01:04:05.135: E/AndroidRuntime(9901):     at android.widget.AbsListView.obtainView(AbsListView.java:2350)
10-15 01:04:05.135: E/AndroidRuntime(9901):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1409)
10-15 01:04:05.135: E/AndroidRuntime(9901):     at android.widget.ListView.onMeasure(ListView.java:1273)
10-15 01:04:05.135: E/AndroidRuntime(9901):     at android.view.View.measure(View.java:15286)
10-15 01:04:05.135: E/AndroidRuntime(9901):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4832)
10-15 01:04:05.135: E/AndroidRuntime(9901):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
10-15 01:04:05.135: E/AndroidRuntime(9901):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
10-15 01:04:05.135: E/AndroidRuntime(9901):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
10-15 01:04:05.135: E/AndroidRuntime(9901):     at android.view.View.measure(View.java:15286)
10-15 01:04:05.135: E/AndroidRuntime(9901):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4832)
10-15 01:04:05.135: E/AndroidRuntime(9901):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
10-15 01:04:05.135: E/AndroidRuntime(9901):     at android.view.View.measure(View.java:15286)

In my custom adapter class what happened i don't know. I tryed so much but don't know how to solve it.

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • please highlight or mention what is your line 56 in 'customBaseAdapter'. – madteapot Oct 14 '14 at 19:45
  • imageView.setImageResource(rowItems.get(position).getImageId()); This line man.@Setu – Md.Masud Parvez Oct 14 '14 at 19:54
  • have you tried my answer(style)? to test really if its the imageview, push down the imageview line below the textview, then test once more, and if your nullpointer exception is still on line 56, then i myt be right.. – Elltz Oct 14 '14 at 20:10
  • honestly speaking I created a test project by copying your code and creating a layout on my machine and it ran perfectly fine. I even tested all variables with debugger but everything seemed fine to me. may be there is a problem with your layout or something. also please try to code 'getCount()' method as suggested by @Elltz. – madteapot Oct 14 '14 at 20:16

3 Answers3

0

I'm not home to test the code, but if you want to try another application is doing very well

https://github.com/thest1/LazyList

From here I took

Lazy load of images in ListView

Community
  • 1
  • 1
Kelo Adler
  • 13
  • 3
0

As the error itself explains, you have problem in getView of your adapter. I think you need to perform the following changes:

if (view == null) {
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    view = inflater.inflate(R.layout.list_item, group, false);
}

Hope it helps.

waqaslam
  • 67,549
  • 16
  • 165
  • 178
0

change getView as i shown below in your code and also add ViewHolder class.

private class ViewHolder { ImageView imageView; TextView txtTitle; TextView txtDesc; }

@Override
public View getView(int position, View convertView, ViewGroup group) {
    // TODO Auto-generated method stub
    ViewHolder holder = null;
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.list_item, null);
        holder = new ViewHolder();
        holder.txtDesc = (TextView) convertView.findViewById(R.id.desc);
        holder.txtTitle = (TextView) convertView.findViewById(R.id.title);
        holder.imageView = (ImageView) convertView.findViewById(R.id.icon);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    RowItem rowItem = (RowItem) getItem(position);
    holder.txtDesc.setText(rowItem.getDesc());
    holder.txtTitle.setText(rowItem.getTitle());
    holder.imageView.setImageResource(rowItem.getImageId());

    return convertView; 
}

`

you are accessing listview rows without adding it.

Android Official Doc for ListView

and For Complete Code Read Here

Rahul Mandaliya
  • 738
  • 1
  • 12
  • 36