L'ajout dynamique d'un enfant à LinearLayout avec l'obtention de chaque enfant en position

J'ai un problème avec l'obtention de la position de l'enfant de LinearLayout. D'abord, je suis en ajoutant dynamiquement un certain nombre de boutons et puis je suis en train de revenir de chaque enfant, de l'index et de l'afficher dans un TextView. Ici, je suis de partage de code:

source java:

private String[] categories;

private LinearLayout ll;
private TextView tv;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    categories = getResources().getStringArray(R.array.categories);

    tv = (TextView) findViewById(R.id.text);
    ll = (LinearLayout) findViewById(R.id.hsvLinearLayout);

    for(int i = 0; i < categories.length; i++) {
        Button btn = new Button(this);
        btn.setText(categories[i]);
        btn.setOnClickListener(buttonClick);
        ll.addView(btn);
    }
}

OnClickListener buttonClick = new OnClickListener() {
    public void onClick(View v) {
        tv.setText(ll.indexOfChild(v));
    }
};

structure xml:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

<HorizontalScrollView
    android:id="@+id/Footer"
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:scrollbars="none"
    android:fadingEdge="none"
    >

    <LinearLayout
        android:id="@+id/hsvLinearLayout"
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        >

    </LinearLayout>

</HorizontalScrollView>

<TextView
    android:id="@+id/text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />

ressources:

<string name="today">Today</string>
<string name="life">Life</string>
<string name="corner">Corner</string>
<string name="banks">Banks</string>
<string name="it">IT</string>
<string name="fun">Fun</string>

<array name="categories">
    <item>@string/today</item>
    <item>@string/life</item>
    <item>@string/corner</item>
    <item>@string/banks</item>
    <item>@string/it</item>
    <item>@string/fun</item>
</array>

L'ajout dynamique est bien, mais la façon dont je suis le réglage de la OnClickListener produire une erreur. Toute aide sera utile! Le but de cela est que si je veux ajouter une ou plusieurs touche(s) à la HorizontalScrollView ne pas être nécessaire de modifier de nombreux fichiers, et juste aller pour sting.xml et créer un nouvel élément dans le catégories-tableau!

C'est ce que LogCat produit:

07-12 22:37:17.680: INFO/System.out(331): waiting for debugger to settle...
07-12 22:37:17.900: INFO/System.out(331): debugger has settled (1441)
07-12 22:37:20.870: INFO/ActivityManager(61): Displayed activity com.test/.TestDynam: 10203 ms (total 10203 ms)
07-12 22:37:25.552: WARN/ResourceType(331): No package identifier when getting value for resource number 0x00000000
07-12 22:37:26.871: DEBUG/dalvikvm(132): GC freed 190 objects / 8976 bytes in 901ms

À préciser - le démarrage de l'application sans aucune erreur, mais quand je clique sur un bouton, il produit cette ligne du journal ci-dessus:

07-12 22:37:25.552: WARN/ResourceType(331): No package identifier when getting value for resource number 0x00000000

Et c'est à partir de débogueur:

TestDynam [Android Application] 
    DalvikVM[localhost:8611]    
        Thread [<3> main] (Suspended (exception Resources$NotFoundException))   
            ViewRoot.handleMessage(Message) line: 1704  
            ViewRoot(Handler).dispatchMessage(Message) line: 99 
            Looper.loop() line: 123 
            ActivityThread.main(String[]) line: 4203    
            Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
            Method.invoke(Object, Object...) line: 521  
            ZygoteInit$MethodAndArgsCaller.run() line: 791  
            ZygoteInit.main(String[]) line: 549 
            NativeStart.main(String[]) line: not available [native method]  
        Thread [<13> Binder Thread #2] (Running)    
        Thread [<11> Binder Thread #1] (Running)
InformationsquelleAutor nenito | 2011-07-12