I'm learning Android and I've done a custom control. When I put it in a Layout, it works fine. I put it in this way in itemduplicados.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.android.customViews.CtlDuplicado
android:id="@+id/dupCancion"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
Then I want to do a list of many of this controls, so I use a listview in listadoduplicado.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LstListadoDuplicados"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>
An Activity for this with a onCreate:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listadoduplicados);
AdaptDuplicados adapter = new AdaptDuplicados(this, Deduplicator.deduplicate());
final ListView lstDuplicados = (ListView) findViewById(R.id.LstListadoDuplicados);
lstDuplicados.setAdapter(adapter);
lstDuplicados.setVisibility(View.VISIBLE);
}
And then a class that extends arrayadapter to populate the listview with ctlduplicados:
public AdaptDuplicados(Activity context, ArrayList<Duplicado> datos) {
super(context, R.layout.itemduplicado, datos);
this.context = context;
this.datos = datos;
}
public View getView(int position, View convertView, ViewGroup parent) {
View item = convertView;
DuplicadoViewHolder dvh;
if (item == null){
LayoutInflater li = context.getLayoutInflater();
item = li.inflate(R.layout.itemduplicado, null);
dvh = new DuplicadoViewHolder();
dvh.duplicado = (CtlDuplicado)item.findViewById(R.id.dupCancion);
item.setTag(item);
}else{
dvh = (DuplicadoViewHolder)item.getTag();
}
dvh.duplicado.setCanciones(datos.get(position));//here is the nullpointerexception cause duplicado is null
return (item);
}
Well, the I have a nullpointerexception because dvh.duplicado is null.
Debbuging in eclipse I see that in the line
item = li.inflate(R.layout.itemduplicado, null);
The item is created and item contains a CtlDuplicado well created in an internal array called mChildren, but when I get item.findViewById(R.id.dupCancion) it returns null...
The R.id.dupCancion is defined in project's R.
Can someone give me any clue of what is happening?
EDIT: the dvh.duplicado that is null is the one in the 'if', this;
dvh.duplicado = (CtlDuplicado)item.findViewById(R.id.dupCancion);
R.id.dupCancion exists and there is a CtlDuplicado created in item's internal array called mChildren but when I call findViewById in item it returns null...
Actualization:
I tried to access the controls inside my custom control and I can reach them in this way:
lbltxt1 = (TextView)item.findViewById(R.id.lbltxt1);
lbltxt2 = (TextView)item.findViewById(R.id.lbltxt2);
lblSubtxt1 = (TextView)item.findViewById(R.id.lblSubtxt1);
With item coming from the 'if'...
So I can reach the components of my control but not the control itself...
The CtlDuplicado is created (I have reach to the constructor via debugger) its drawed properly but then I cannot access it via findViewById... but I can access their components via that function... Ufff...