NullPointerException in custom adapter getView

15,723

Solution 1

Change this:

vi = inflater.inflate(R.layout.result_list_item, null);

To this:

vi = inflater.inflate(R.layout.result_list_item, parent, false);

this is how your adapter should be:

public class NoPicAdapter extends ArrayAdapter<NewAndCalendar> {

    private ArrayList<NewAndCalendar> data;
    private Activity mActivity;
    private LayoutInflater inflater = null;

    public NoPicAdapter(Activity a, ArrayList<NewAndCalendar> d) {
        super(a, R.layout.no_pic_list_item, d);
        mActivity = a;
        data = d;
        inflater = (LayoutInflater) mActivity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }


    @Override
    public View getView(int position, @Nullable View convertView, ViewGroup parent) {
        View vi = convertView;
        if (vi == null)
            vi = inflater.inflate(R.layout.no_pic_list_item, parent, false);
        TextView title = (TextView) vi.findViewById(R.id.noPicTitle);
        TextView subtitle = (TextView) vi.findViewById(R.id.noPicSubtitle);

        title.setText(data.get(position).getmTitle());
        subtitle.setText(data.get(position).getmPubDate());

        return vi;
    }

}

Solution 2

Try this

vi = LayoutInflater.from(mActivity).inflate(R.layout.result_list_item, null);

if mActivity is your Activity context passed from the activity where you are creating the object of this adapter.

Solution 3

are you initializing inflater? if not initialize it.

inflater = getLayoutInflater();

or

 inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Share:
15,723
thepoosh
Author by

thepoosh

A java (mostly) developer at Freightos interested in Android, Flutter, Java, GCP,k8s and anything I can wrap my head around

Updated on July 08, 2022

Comments

  • thepoosh
    thepoosh about 2 years

    I'm coding a custom adapter for a ListView that has a custom list to it.

    obviously, I have to write the getView function. so here is my code:

    /*1*/ public View getView(int position, View convertView, ViewGroup parent) {
    /*2*/    View vi = convertView;
    /*3*/    if (convertView == null)
    /*4*/        vi = inflater.inflate(R.layout.result_list_item, null);
    /*5*/
    /*6*/    TextView title = (TextView) vi.findViewById(R.id.tvTitle);
    /*7*/    TextView subtitle = (TextView) vi.findViewById(R.id.tvSubTitle);
    /*8*/    ImageView image = (ImageView) vi.findViewById(R.id.imageView1);
    /*9*/
    /*10*/   title.setText(data.get(position).getDescription().toString());
    /*11*/   subtitle.setText(data.get(position).getDate().toString());
    /*12*/   String img = "http://someLink/" + data.get(position).getId() + "a_t.jpg";
    /*13*/   imageLoader.DisplayImage(img, image);
    /*14*/   return vi;
    /*15*/ }
    

    But, I have noticed that on line number 6 the program crashes with a NullPointer exception.

    here is my crash report:

    04-18 09:33:32.745: E/AndroidRuntime(2209): FATAL EXCEPTION: main
    04-18 09:33:32.745: E/AndroidRuntime(2209): java.lang.NullPointerException
    04-18 09:33:32.745: E/AndroidRuntime(2209):     at com.package.mojo.NoPicAdapter.getView(NoPicAdapter.java:46)
    04-18 09:33:32.745: E/AndroidRuntime(2209):     at android.widget.AbsListView.obtainView(AbsListView.java:1554)
    04-18 09:33:32.745: E/AndroidRuntime(2209):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1264)
    04-18 09:33:32.745: E/AndroidRuntime(2209):     at android.widget.ListView.onMeasure(ListView.java:1175)
    04-18 09:33:32.745: E/AndroidRuntime(2209):     at android.view.View.measure(View.java:8366)
    04-18 09:33:32.745: E/AndroidRuntime(2209):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
    04-18 09:33:32.745: E/AndroidRuntime(2209):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
    04-18 09:33:32.745: E/AndroidRuntime(2209):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
    04-18 09:33:32.745: E/AndroidRuntime(2209):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
    04-18 09:33:32.745: E/AndroidRuntime(2209):     at android.view.View.measure(View.java:8366)
    04-18 09:33:32.745: E/AndroidRuntime(2209):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
    04-18 09:33:32.745: E/AndroidRuntime(2209):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
    04-18 09:33:32.745: E/AndroidRuntime(2209):     at android.view.View.measure(View.java:8366)
    04-18 09:33:32.745: E/AndroidRuntime(2209):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:531)
    04-18 09:33:32.745: E/AndroidRuntime(2209):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
    04-18 09:33:32.745: E/AndroidRuntime(2209):     at android.view.View.measure(View.java:8366)
    04-18 09:33:32.745: E/AndroidRuntime(2209):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
    04-18 09:33:32.745: E/AndroidRuntime(2209):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
    04-18 09:33:32.745: E/AndroidRuntime(2209):     at android.view.View.measure(View.java:8366)
    04-18 09:33:32.745: E/AndroidRuntime(2209):     at android.view.ViewRoot.performTraversals(ViewRoot.java:847)
    04-18 09:33:32.745: E/AndroidRuntime(2209):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1868)
    04-18 09:33:32.745: E/AndroidRuntime(2209):     at android.os.Handler.dispatchMessage(Handler.java:99)
    04-18 09:33:32.745: E/AndroidRuntime(2209):     at android.os.Looper.loop(Looper.java:123)
    04-18 09:33:32.745: E/AndroidRuntime(2209):     at android.app.ActivityThread.main(ActivityThread.java:3691)
    04-18 09:33:32.745: E/AndroidRuntime(2209):     at java.lang.reflect.Method.invokeNative(Native Method)
    04-18 09:33:32.745: E/AndroidRuntime(2209):     at java.lang.reflect.Method.invoke(Method.java:507)
    04-18 09:33:32.745: E/AndroidRuntime(2209):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
    04-18 09:33:32.745: E/AndroidRuntime(2209):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
    04-18 09:33:32.745: E/AndroidRuntime(2209):     at dalvik.system.NativeStart.main(Native Method)
    

    of course, the layout and the items themselves exist...

    any thoughts?

    update: when i look at the variables while debugging, I see that vi was not inflated (vi == null), why?

  • thepoosh
    thepoosh about 12 years
    the initialization code is in the constructor: inflater = (LayoutInflater) mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  • Shubhayu
    Shubhayu about 12 years
    and is mActivity the Activity Context u r passing from the Activity?
  • thepoosh
    thepoosh about 12 years
    changed to the second version, vi still equals null
  • thepoosh
    thepoosh about 12 years
    yes, mActivity is the activity passed on for the constructor.
  • thepoosh
    thepoosh about 12 years
    this is my whole code for the custom adapter
  • waqaslam
    waqaslam about 12 years
    instead of extending to BaseAdapter, you should extend to ArrayAdapter. Moreover, don't set your inflater object as static, since it may cause memory leaks.
  • thepoosh
    thepoosh about 12 years
    that did the trick, the only problem now is the very new NullPointerException i got from the rest of the code...
  • thepoosh
    thepoosh about 12 years
    no more static for me. why should I extend ArrayAdapter if the view is ListView? I took the idea for the code from fedorvlasovs' LazyAdapter
  • waqaslam
    waqaslam about 12 years
    because when you use BaseAdapter, you need to take care of some other overrided methods too. However, in your case your are simply showing items in list. So its better to use ArrayAdapter.
  • thepoosh
    thepoosh about 12 years
    problems with implementing SAXParser I think. something is not being parsed correctly