convertView.findViewById returning null even though convertView is not null
<p>I'm trying to display a ListView with different types of rows.
I have a <code>Result</code> abstract class inherited by <code>Artist</code>, <code>Label</code>, <code>Release</code> etc.
I'm getting results from a JSON response. Parsing is ok, my factory manages to create some <code>Result</code> objects properly.</p>

<p>Using these guides <a href="http://antew.com/?p=162" rel="nofollow">1</a>, <a href="http://android.amberfog.com/?p=296" rel="nofollow">2</a>, I ended up extending <code>BaseAdapter</code>.</p>

<pre><code>public class SearchResultsActivity extends ListActivity {

    private static final String SEARCH_API_ENDPOINT = "http://xxxx.com/database/search?q=";

    SearchResultAdapter searchAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.result_list);
        searchAdapter = new SearchResultAdapter();
        // handleIntent is needed because android:launchMode="singleTop" mode uses onNewIntent
        handleIntent(getIntent());
    }

    @Override
    protected void onNewIntent(Intent intent) {
        handleIntent(intent);
    }

    private void handleIntent(Intent intent) {

        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            String query = intent.getStringExtra(SearchManager.QUERY); // get user input from search widget 
            String[][] params = new String[][]{{"q", query}}; // prepare GET parameters
            HttpRequestHandler requestHandler = new HttpRequestHandler("GET");
            String result;

            requestHandler.setURL(HttpRequestHandler.API_URL, HttpRequestHandler.SEARCH_QUERY_ENDPOINT);
            requestHandler.addParameters(params);
            result = requestHandler.sendRequest();
            if (result != null &amp;&amp; !result.isEmpty()) {
                try {
                    ArrayList&lt;Result&gt; resultList = new ResultFactory(new JSONObject(result)).getResults(); // JSON to Result objects
                    for (Result res: resultList) {
                        searchAdapter.addItem(res);
                    }
                    setListAdapter(searchAdapter);
                } catch (JSONException e) {
                    System.out.println("xxjsonexception");
                }
            }
        }
    }

    public class SearchResultAdapter extends BaseAdapter {

        private List&lt;Result&gt; searchResults = new ArrayList&lt;Result&gt;();
        private LayoutInflater mInflater;

        public SearchResultAdapter() {
            mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        public void addItem(Result result) {
            searchResults.add(result);
            notifyDataSetChanged();
        }

        @Override
        public Object getItem(int position) {
            return searchResults.get(position);
        }

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

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

        @Override
        public int getViewTypeCount() {
            return Result.Type.values().length; // enum length
        }

        @Override
        public int getItemViewType(int position) {
            return searchResults.get(position).getType().ordinal();
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Result tmp = searchResults.get(position); // only for testing
            System.out.println("xxItem type " + tmp.getType());
            View v = searchResults.get(position).getView(mInflater, convertView);
            if (v == null) {
                System.out.println("xxgetviewnull");
            } else {
                System.out.println("xxgetviewok");
            }
            return v;
        }
    }
}
</code></pre>

<p>Result.java :</p>

<pre><code>public abstract class Result {
    // general search result class

    public enum Type {
        ARTIST,
        RELEASE,
        MASTER,
        LABEL,
        ERROR,
    }

    public static final String NODE_RESULTS = "results";
    public static final String NODE_TITLE = "title";
    public static final String NODE_THUMB = "thumb";
    public static final String NODE_RESSOURCE = "resource_url";
    public static final String NODE_TYPE = "type";
    public static final String NODE_URI = "uri";
    public static final String NODE_ID = "id";

    public static final String TYPE_ARTIST = "artist";
    public static final String TYPE_RELEASE = "release";
    public static final String TYPE_MASTER = "master";
    public static final String TYPE_LABEL = "label";


    protected String thumb;
    protected String title;
    protected String ressource_url;
    protected static Type type;
    protected String uri;
    protected int id;


    public void setData(JSONObject jsonResult) {
        try {
            setId(jsonResult.getInt(Result.NODE_ID));
            setUri(jsonResult.getString(Result.NODE_URI));
            setRessource_url(jsonResult.getString(Result.NODE_RESSOURCE));
            setThumb(jsonResult.getString(Result.NODE_THUMB));
            setTitle(jsonResult.getString(Result.NODE_TITLE));
        } catch (JSONException e) {
            type = Type.ERROR;
        }
    }

    // getters and setters ..

    public abstract View getView(LayoutInflater inflater, View convertView);
}
</code></pre>

<p>Artist.java :</p>

<pre><code>public class Artist extends Result{

    public Artist(){
        type = Type.ARTIST;
    }

    @Override
    public View getView(LayoutInflater inflater, View convertView) {
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.list_item_artist, null);
        }
        TextView textTitle = (TextView) convertView.findViewById(R.id.title_artist);
        if (textTitle == null) {
            System.out.println("xxtexttitle null");
        } else {
            System.out.println("xxtexttitleok " + title);
        }
        textTitle.setText(title);
        return convertView;
    }

}
</code></pre>

<p>Label.java </p>

<pre><code>public class Label extends Result {

    public Label() {
        type = Type.LABEL;
    }

    @Override
    public View getView(LayoutInflater inflater, View convertView) {
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.list_item_label, null);
        }
        if (convertView == null) {
            System.out.println("xxconvertviewisnull");
        } else {
            System.out.println("xxconvertviewok");
        }
        TextView titleText = (TextView) convertView.findViewById(R.id.title_label);
        titleText.setText(title);
        return convertView;
    }

}
</code></pre>

<p>As you can see, my <code>Artist</code> and <code>Label</code> class are very similar but only my <code>Label</code> class throws a NPE at <code>TextView titleText = (TextView) convertView.findViewById(R.id.title_label)</code>.   </p>

<p>This is not a <code>setContentView()</code> as I am doing it in <code>onCreate</code>.<br>
My <code>Result</code> objects are never <code>null</code> nor do they have <code>null</code> fields.<br>
I do check to see if <code>convertView</code> is <code>null</code> in my <code>getView</code> methods, inflating a View if ever it is <code>null</code>.<br>
I am not using a ViewHolder because <code>Release</code> and <code>Master</code> will be displaying extra fields.</p>

<p>EDIT : </p>

<p>list_item_label.xml</p>

<pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent"&gt;

    &lt;TextView android:id="@+id/title_label"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"/&gt;

&lt;/LinearLayout&gt;
</code></pre>

<p>list_item_artist.xml</p>

<pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent"&gt;

    &lt;TextView android:id="@+id/title_artist"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:textColor="#FFAAAA"
              android:textSize="14pt"/&gt;

&lt;/LinearLayout&gt;
</code></pre>
