Partage
  • Partager sur Facebook
  • Partager sur Twitter

[Android] ViewStub - accèder à composant interne

Sujet résolu
    21 mai 2012 à 12:28:50

    Salut,

    J'ai un petit problème avec mon activity. J'ai un ImageButton dans le code de mon viewstub et j'essaye juste de le récupérer mais je prends un nullPointerException.

    //mise en place du fond // Toujours après les options d'affichage
    this.setContentView(R.layout.main);
    
    ViewStub stub = (ViewStub) this.findViewById(R.id.viewstub); // OK
    if(stub!=null){ // OK
        stub.inflate();
        ImageButton modeConnexion = (ImageButton) stub.findViewById(R.id.modedeconnecte);
        //modeConnexion est null
    }
    


    le code XML du viewstub :

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    
        <ImageButton
    	    android:id="@+id/modedeconnecte"
    	    android:layout_width="wrap_content"
    	    android:layout_height="wrap_content"
    	    android:layout_alignParentTop="true"
    	    android:layout_alignParentRight="true"
    	    android:layout_marginTop="65dp"
    	    android:layout_marginRight="5dp"
    	    android:padding="5dp"
    	    android:background="@drawable/mybutton_background"
    	    android:onClick="modeDeconnecte"
    	    android:contentDescription="@string/deconnectedMode"
    	    android:textColor="#fff"
    	    android:textSize="15dip" />
            
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/modedeconnecte"
            android:layout_marginBottom="5dp"
            android:layout_marginTop="5dp"
            android:contentDescription="scanCardbackground"
            android:scaleType="center"
            android:src="@drawable/scanprof" />
    
    </RelativeLayout>
    
    • Partager sur Facebook
    • Partager sur Twitter
      21 mai 2012 à 12:34:52

      Bonjour,

      Tu dois récupérer l'inflation de ton stub :
      ViewStub stub = (ViewStub) findViewById(R.id.stub);
      View inflated = stub.inflate();
      ImageButton modeConnexion = (ImageButton) inflated.findViewById(R.id.modedeconnecte);
      • Partager sur Facebook
      • Partager sur Twitter
      Si vous voulez me retrouver, rendez-vous sur ZesteDeSavoir.
        21 mai 2012 à 12:37:06

        Effectivement je n'avais pas remarqué que inflate renvoyait un objet View !

        Merci beaucoup pour l'aide.
        • Partager sur Facebook
        • Partager sur Twitter

        [Android] ViewStub - accèder à composant interne

        × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
        × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
        • Editeur
        • Markdown