0

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...

nbanic
  • 1,270
  • 1
  • 8
  • 11
inigoD
  • 1,681
  • 14
  • 26
  • 3
    Your post says `itemduplicados.xml`, but also `R.layout.itemduplicado`; these don't correspond to the same thing. Was this a typo, or are you referencing the wrong file? Additionally, I would question if `dvh.duplicado` is set in the `else` portion of your code. – Cat Jun 26 '12 at 23:16
  • [How to read a logcat](http://stackoverflow.com/a/6065300/420015). – adneal Jun 26 '12 at 23:21
  • Sorry: Yes it's itemduplicado.xml and not itemduplicados.xml so the R.layout.itemduplicado is defined, and the dvh.duplicado is set in the 'if', not in the else. Sorry here there are 1:50am – inigoD Jun 26 '12 at 23:48
  • Soxxeh I fixed that problems in post, thanks – inigoD Jun 26 '12 at 23:58

1 Answers1

1

You are not setting the actual view (probably a typo)

item.setTag(item);

should be

item.setTag(dvh);
Chandra
  • 1,317
  • 11
  • 14
  • You're rigth. Thats an error: i'll fix it in code and in post But the dvh.duplicado is already null in that point so the code still throwing a nullpointerexception... – inigoD Jun 27 '12 at 05:34
  • Sorry i've not enougth reputation – inigoD Jun 27 '12 at 05:39
  • Actually, you [don't need any reputation to mark it as correct](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) (the little checkmark at the side). You'd only need reputation to upvote it (the arrows). – Cat Jun 27 '12 at 06:25