0

I made my own custom adapter to support multiple types of ListView Items but I keep getting null pointer exceptions. Is there something with the convertView that I am not using? Anyone have any insight?

Here is my adapter class:

package com.lucasmikesoftware.messaround;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.ArrayList;


public class MyAdapter extends ArrayAdapter<AdapterData> {
private int layoutId;
public MyAdapter(Context context, ArrayList<AdapterData> data, int layoutId) {
    super(context, R.layout.events_list_item, data);
    this.layoutId = layoutId;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if(getItem(position) instanceof ImageAdapterData){
        return setImageListItem(position,parent);
    } else if (getItem(position) instanceof VideoAdapterData){
        return setVideoListItem(position,parent);
    } else if (getItem(position) instanceof EventAdapterData){
        return setEventListItem(position,parent);
    } else{
        return setNormalListItem(position,parent);
    }


}

public View setNormalListItem(int position,ViewGroup parent){
    LayoutInflater theInflater = LayoutInflater.from(getContext());
    View theView = theInflater.inflate(layoutId,parent,false);
    AdapterData adapterData = getItem(position);
    TextView theTextView = (TextView) theView.findViewById(R.id.normal_list_item_title);
    theTextView.setText(adapterData.getTitle());
    TextView theFullTextTextView = (TextView) theView.findViewById(R.id.normal_list_item_full_text);
    theFullTextTextView.setText(adapterData.getFullText());
    return theView;
}

public View setImageListItem(int position,ViewGroup parent){
    LayoutInflater theInflater = LayoutInflater.from(getContext());
    View theView = theInflater.inflate(layoutId,parent,false);
    ImageAdapterData adapterData = (ImageAdapterData) getItem(position);
    TextView theTextView = (TextView) theView.findViewById(R.id.image_list_item_title);
    theTextView.setText(adapterData.getTitle());
    TextView theFullTextTextView = (TextView) theView.findViewById(R.id.image_list_item_full_text);
    theFullTextTextView.setText(adapterData.getFullText());
    ImageView theImageView = (ImageView) theView.findViewById(R.id.image_list_item_image);
    Picasso.with(getContext()).load(adapterData.getImageUrl()).into(theImageView);
    return theView;
}

public View setEventListItem(int position,ViewGroup parent){
    LayoutInflater theInflater = LayoutInflater.from(getContext());
    View theView = theInflater.inflate(layoutId,parent,false);
    EventAdapterData adapterData = (EventAdapterData) getItem(position);
    TextView theTextView = (TextView) theView.findViewById(R.id.text_view);
    theTextView.setText(adapterData.getTitle());
    ImageView theImageView = (ImageView) theView.findViewById(R.id.event_image_view);
    Picasso.with(getContext()).load(adapterData.getImageUrl()).into(theImageView);
    return theView;
}

public View setVideoListItem(int position,ViewGroup parent){
    //TODO Finish this damn thing
    return parent;
}

}

Here are the error logs:

 07-30 23:55:24.928 16830-16830/com.lucasmikesoftware.messaround E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.lucasmikesoftware.messaround, PID: 16830
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
at com.lucasmikesoftware.fuckaround.MyAdapter.setImageListItem(MyAdapter.java:69)
at com.lucasmikesoftware.fuckaround.MyAdapter.getView(MyAdapter.java:42)
at android.widget.AbsListView.obtainView(AbsListView.java:2467)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1270)
at android.widget.ListView.onMeasure(ListView.java:1182)
at android.view.View.measure(View.java:17536)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5478)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at android.view.View.measure(View.java:17536)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1626)
at android.view.View.measure(View.java:17536)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5478)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17536)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:727)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:463)
at android.view.View.measure(View.java:17536)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5478)
at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:671)
at android.support.design.widget.HeaderScrollingViewBehavior.onMeasureChild(HeaderScrollingViewBehavior.java:90)
at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onMeasureChild(AppBarLayout.java:1319)
at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:736)
at android.view.View.measure(View.java:17536)
at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:1079)
at android.view.View.measure(View.java:17536)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5478)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
at android.view.View.measure(View.java:17536)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5478)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17536)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5478)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at android.view.View.measure(View.java:17536)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5478)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17536)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5478)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2629)
at android.view.View.measure(View.java:17536)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2102)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1218)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1435)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1106)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6018)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:792)
at android.view.Choreographer.doCallbacks(Choreographer.java:596)
at android.view.Choreographer.doFrame(Choreographer.java:557
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Lucas N
  • 69
  • 9
  • What is `at com.lucasmikesoftware.fuckaround.MyAdapter.setImageListItem(MyAdapter.java:69)`? – Ishita Sinha Jul 31 '16 at 05:15
  • It is the line where I try to instantiate theTextView inside the setImageListItem() – Lucas N Jul 31 '16 at 05:19
  • I don't much work with `ListView`s, but as I understand, inside `getView`, you have to check if `convertView` is null and if it is, inflate the item view. You _are_ inflating some view inside `setImageListItem`, but I'm not sure this quite does the trick. – Ishita Sinha Jul 31 '16 at 05:20
  • as error shows, you want to findView and it not exist. check your row list and make sure all of the views you inflated exist. – Amir Jul 31 '16 at 05:22
  • So, apparently, `theView` is null, even though you're doing `View theView = theInflater.inflate(layoutId,parent,false);`. Could you put some break points or print some logs and find out what's going wrong? – Ishita Sinha Jul 31 '16 at 05:22
  • What type of logs should i print? – Lucas N Jul 31 '16 at 05:43
  • Thanks for the help, I figured it out myself. I was trying to inflate into a layout that did not contain that TextView. – Lucas N Jul 31 '16 at 19:24

0 Answers0