Welcome to another findviewbyid() returns null
question!
I wrote an adapter class for displaying an array into a ListView. And gues what findviewbyid() returns null. (I checked all the other questions on this topic but they're not related to my problem).
So here is my Adapter Class:
package de.kosmowski.discogs.wants;
import java.util.ArrayList;
import android.content.Context;
import android.util.Log;
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 de.kosmowski.discogs.model.Want;
public class WantAdapter extends ArrayAdapter<Want> {
private ArrayList<Want> items;
public WantAdapter(Context context, int textViewResourceId, ArrayList<Want> items) {
super(context, textViewResourceId, items);
this.items = items;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
Want w = items.get(position);
Log.d("WantAdapter", "want:" + w);
if (v == null) {
LayoutInflater vi = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(w != null && w.getType()!= null && (w.getType().equals(Want.TYPE_RELEASE) || w.getType().equals(Want.TYPE_MASTER)))
{
Log.d(this.getClass().getCanonicalName(), "Choosing Release List Item");
v = vi.inflate(R.layout.releaselistitem, null);
}
else
{
v = vi.inflate(R.layout.lplistitem, null);
}
}
if (w != null) {
ImageView icon = (ImageView) v.findViewById(R.id.typeicon); //Returns NULL at this line
TextView tt = (TextView) v.findViewById(R.id.text1);
if(w.getType() != null)
{
if(w.getType().equals(Want.TYPE_ARTIST))
{
icon.setImageResource(R.drawable.resulttype_artist);
}
else if(w.getType().equals(Want.TYPE_RELEASE))
{
icon.setImageResource(R.drawable.resulttype_lp);
}
else if(w.getType().equals(Want.TYPE_MASTER))
{
icon.setImageResource(R.drawable.resulttype_lp_master);
}
else
{
icon.setImageResource(R.drawable.defaultresult);
}
}
else
{
icon.setImageResource(R.drawable.defaultresult);
}
if (tt != null) {
tt.setText(w.getName()); }
}
return v;
}
}
I marked the line where null is returned.
And here is the inflated xml file (both the lplistitem and the releaselistitem are currently exactly the same):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<ImageView android:scaleType="center" android:layout_gravity="center_vertical" android:src="@drawable/defaultresult" android:id="@+id/typeicon" android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:paddingLeft="6dip"
android:textSize="16px"
android:minHeight="?android:attr/listPreferredItemHeight"
/>
</LinearLayout>
Can you see any obvious errors? Everything seems right to me.