IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Android Discussion :

Changer de Fragment depuis Fragment inconnu


Sujet :

Android

  1. #21
    Membre du Club
    Homme Profil pro
    Développeur Java
    Inscrit en
    Juin 2015
    Messages
    185
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Développeur Java

    Informations forums :
    Inscription : Juin 2015
    Messages : 185
    Points : 54
    Points
    54
    Par défaut
    J'ai essayé autre chose. A la place de
    j'ai mis
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    Frag3 frag = (Frag3) getSupportFragmentManager().findFragmentById(R.id.layoutfrag3);
    frag.bouton1();
    mais l'appli crash et le LogCat me renvoie un nullPointerException à la ligne "frag.bouton1();".

    Pff, j'suis sur que je passe à côté d'un truc évident, mais je le vois pas...
    Some help please !

  2. #22
    Expert éminent

    Avatar de Feanorin
    Profil pro
    Inscrit en
    Avril 2004
    Messages
    4 589
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2004
    Messages : 4 589
    Points : 9 149
    Points
    9 149
    Par défaut
    tu es sur que le fragment avec id layoutfrag3 est bien ajouté dans le layout que tu charges depuis ton activité ?

    Car lui il ne le trouve pas
    Responsable Android de Developpez.com (Twitter et Facebook)
    Besoin d"un article/tutoriel/cours sur Android, consulter la page cours
    N'hésitez pas à consulter la FAQ Android et à poser vos questions sur les forums d'entraide mobile d'Android.

  3. #23
    Membre du Club
    Homme Profil pro
    Développeur Java
    Inscrit en
    Juin 2015
    Messages
    185
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Développeur Java

    Informations forums :
    Inscription : Juin 2015
    Messages : 185
    Points : 54
    Points
    54
    Par défaut
    Oui, mais peut-être l'ai-je mal déclaré.

    Mon fragment Frag3 contient :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    View mainView = inflater.inflate(R.layout.layoutfrag3, container, false);
    return mainView;
    layoutfrag3.xml a pour en-tête :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="left"
        android:orientation="horizontal"
        android:id="@+id/layoutfrag3" >
    Y'a-t-il une erreur ?

  4. #24
    Expert éminent

    Avatar de Feanorin
    Profil pro
    Inscrit en
    Avril 2004
    Messages
    4 589
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2004
    Messages : 4 589
    Points : 9 149
    Points
    9 149
    Par défaut
    Montres moi plutôt ton layout xml de ton activité qui s'occupe de tes fragments .
    Responsable Android de Developpez.com (Twitter et Facebook)
    Besoin d"un article/tutoriel/cours sur Android, consulter la page cours
    N'hésitez pas à consulter la FAQ Android et à poser vos questions sur les forums d'entraide mobile d'Android.

  5. #25
    Membre du Club
    Homme Profil pro
    Développeur Java
    Inscrit en
    Juin 2015
    Messages
    185
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Développeur Java

    Informations forums :
    Inscription : Juin 2015
    Messages : 185
    Points : 54
    Points
    54
    Par défaut
    Le voici :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="left"
        android:orientation="horizontal"
        tools:context="com.vahia.monappli.MainActivity" >        
        <ScrollView
            android:layout_width="275dp"
            android:layout_height="wrap_content"
            android:gravity="center" >
           <LinearLayout
                android:layout_width="275dp"
            	android:layout_height="wrap_content"
            	android:orientation="vertical"
            	android:layout_gravity="left" >        	
            	<Button
            	    style="@style/whiteButtonTitle"
            	    android:layout_width="fill_parent"
            	    android:layout_height="50dp"
            	    android:text="@string/butt0" />
            	<View
            	    android:layout_width="fill_parent"
            	    android:layout_height="0.2dp"
            	    style="@style/trait" />
                <Button
                    android:id="@+id/bouton1"
                    android:layout_width="fill_parent"
                    android:layout_height="50dp"
                    android:text="@string/butt1"                
                    style="@style/whiteButton" />
                <View
            	    android:layout_width="fill_parent"
            	    android:layout_height="0.2dp"
            	    style="@style/trait" />
                <Button
                    android:id="@+id/bouton2"
                    android:layout_width="fill_parent"
                    android:layout_height="50dp"
                    android:text="@string/butt2"                
                    style="@style/whiteButton" />
                <View
            	    android:layout_width="fill_parent"
            	    android:layout_height="0.2dp"
            	    style="@style/trait" />
                <Button
                    android:id="@+id/bouton3"
                    android:layout_width="fill_parent"
                    android:layout_height="50dp"
                    android:text="@string/butt3"
                    style="@style/whiteButton" />
                <View
            	    android:layout_width="fill_parent"
            	    android:layout_height="0.2dp"
            	    style="@style/trait" />
            </LinearLayout>
        </ScrollView>
        <TableLayout 
       	android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center" >
            <TableRow
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >
            <View
                android:layout_width="2dp"
                android:background="#adadad" />
            </TableRow>
    </TableLayout>
        <LinearLayout
            android:id="@+id/frags"
            android:orientation="vertical"
            android:layout_width="1000dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center" >
        </LinearLayout>
    </LinearLayout>
    La partie qui contient le fragment est le LinearLayout de la fin (id:frags).
    Je ne l'ai pas déclaré en <fragment></fragment> comme spécifié dans la déclaration des fragments dynamiques de ce tuto : http://mathias-seguy.developpez.com/...oid/fragments/

  6. #26
    Expert éminent

    Avatar de Feanorin
    Profil pro
    Inscrit en
    Avril 2004
    Messages
    4 589
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2004
    Messages : 4 589
    Points : 9 149
    Points
    9 149
    Par défaut
    Ok donc il ne faut pas que tu recherches les fragments par Id alors mais par Tag.

    findFragmentByTag(), le tag est celui que tu as associé lors de l'ajout du fragment dans le fragmentManager .
    Responsable Android de Developpez.com (Twitter et Facebook)
    Besoin d"un article/tutoriel/cours sur Android, consulter la page cours
    N'hésitez pas à consulter la FAQ Android et à poser vos questions sur les forums d'entraide mobile d'Android.

  7. #27
    Membre du Club
    Homme Profil pro
    Développeur Java
    Inscrit en
    Juin 2015
    Messages
    185
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Développeur Java

    Informations forums :
    Inscription : Juin 2015
    Messages : 185
    Points : 54
    Points
    54
    Par défaut
    C'est un peu ce que je craignais. J'ai pas bien compris le système de tags donc jusque là je l'ai soigneusement évité...
    J'ai utilisé le tag que j'ai utilisé lors de la transaction de mes fragments mais ça me renvoie le même NullPointerException. Dans le doute, j'ai essayé d'autres tags présents dans MainActivity mais idem.
    Je suis peut-être complètement à côté de la plaque...

  8. #28
    Expert éminent

    Avatar de Feanorin
    Profil pro
    Inscrit en
    Avril 2004
    Messages
    4 589
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2004
    Messages : 4 589
    Points : 9 149
    Points
    9 149
    Par défaut
    En gros :

    quand tu rajoutes tes fragments dans ta backstack avec les FragmentTransaction lors de l'appel de la méthode add ou replace tu peux rajouter en fin de paramètre le tag associé à cet action.
    Ce tag qui est un string te permet plus tard de pouvoir retrouver un fragment présent dans ta backstack avec le tag recherché.


    Du style ici je rajoute mon fragment dynamique mainFragment à qui j'associé le tag mainFragmentTag qui est un string que j'aurais défini en static.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    fTransaction.add(R.id.firstpane, mainFragment, mainFragmentTag);
    Si un jour j'ai besoin de le retrouver alors je n'aurais plus qu'à appelé la méthode findFragmentByTag avec le tag mainFragmentTag
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    getFragmentManager().findFragmentByTag(mainFragmentTag);
    Soit il trouve le fragment dans la backstack et me le retourne soit il n'y est pas présent car je l'ai supprimé entre temps et là j'ai null.
    Responsable Android de Developpez.com (Twitter et Facebook)
    Besoin d"un article/tutoriel/cours sur Android, consulter la page cours
    N'hésitez pas à consulter la FAQ Android et à poser vos questions sur les forums d'entraide mobile d'Android.

  9. #29
    Membre du Club
    Homme Profil pro
    Développeur Java
    Inscrit en
    Juin 2015
    Messages
    185
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Développeur Java

    Informations forums :
    Inscription : Juin 2015
    Messages : 185
    Points : 54
    Points
    54
    Par défaut
    Un grand merci pour ces explications très claires ! Ce coup-ci j'ai bien compris le principe des tags.

    Un dernier problème demeure. Quand, dans mon activité, après avoir invoqué avec succès le bon fragment, je lui demande d'exécuter une de ses méthode (qui implémente ses textViews), j'ai un NullPointerException sur la première ligne de ladite méthode.
    Je pense que le problème vient du fait que, vu que c'est l'activité qui l'invoque, c'est elle qui essaie de l'exécuter dans son propre layout. Or, dans son layout, les textViews n'existent pas.
    Quand je lance l'exécution de la méthode directement dans le onCreateView du fragment ça fonctionne. Mais ça ne m'intéresse pas car je veux pouvoir appeler plusieurs fois ce fragment mais lui faire exécuter des méthodes différentes suivant depuis quel bouton de l'activité il est invoqué.

    Du coup, existe-il un moyen simple de :
    - dire au fragment d'exécuter une de ses méthode dans son propre environnement ?
    - ou d'exécuter la méthode dans l'activité en ciblant les textViews du layout du fragment ?

  10. #30
    Expert éminent

    Avatar de Feanorin
    Profil pro
    Inscrit en
    Avril 2004
    Messages
    4 589
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2004
    Messages : 4 589
    Points : 9 149
    Points
    9 149
    Par défaut
    Je ne vois pas pourquoi cela poserait problème mais plus simple à faire dans ce cas tu mets tout tes composants graphiques appartenant au fragments associé en tant que menbre comme cela lors de l'appel à la méthode du fragment demandé tu pourras interagir directement avec ces composants .
    Responsable Android de Developpez.com (Twitter et Facebook)
    Besoin d"un article/tutoriel/cours sur Android, consulter la page cours
    N'hésitez pas à consulter la FAQ Android et à poser vos questions sur les forums d'entraide mobile d'Android.

  11. #31
    Membre du Club
    Homme Profil pro
    Développeur Java
    Inscrit en
    Juin 2015
    Messages
    185
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Développeur Java

    Informations forums :
    Inscription : Juin 2015
    Messages : 185
    Points : 54
    Points
    54
    Par défaut
    En tant que membre, c'est à dire ?

  12. #32
    Expert éminent

    Avatar de Feanorin
    Profil pro
    Inscrit en
    Avril 2004
    Messages
    4 589
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2004
    Messages : 4 589
    Points : 9 149
    Points
    9 149
    Par défaut
    En tant que variable de la classe.
    Responsable Android de Developpez.com (Twitter et Facebook)
    Besoin d"un article/tutoriel/cours sur Android, consulter la page cours
    N'hésitez pas à consulter la FAQ Android et à poser vos questions sur les forums d'entraide mobile d'Android.

  13. #33
    Membre du Club
    Homme Profil pro
    Développeur Java
    Inscrit en
    Juin 2015
    Messages
    185
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Développeur Java

    Informations forums :
    Inscription : Juin 2015
    Messages : 185
    Points : 54
    Points
    54
    Par défaut
    J'ai déclaré tous les textViews dans mon activité, je les ai récupéré avec des
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
     textView1 = (textView) findViewById (R.id.textview1);
    mais ça me renvoie aussi un NullPointerException à la ligne
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    textView1.setText ("texte1")
    A moins qu'il faille les déclarer différemment ?!

  14. #34
    Expert éminent

    Avatar de Feanorin
    Profil pro
    Inscrit en
    Avril 2004
    Messages
    4 589
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2004
    Messages : 4 589
    Points : 9 149
    Points
    9 149
    Par défaut
    Peux tu me montrer le code entier quand tu récupères la textView ?
    Responsable Android de Developpez.com (Twitter et Facebook)
    Besoin d"un article/tutoriel/cours sur Android, consulter la page cours
    N'hésitez pas à consulter la FAQ Android et à poser vos questions sur les forums d'entraide mobile d'Android.

  15. #35
    Membre du Club
    Homme Profil pro
    Développeur Java
    Inscrit en
    Juin 2015
    Messages
    185
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Développeur Java

    Informations forums :
    Inscription : Juin 2015
    Messages : 185
    Points : 54
    Points
    54
    Par défaut
    Pas de souci :

    Le code de mon activité MainActivity :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    package com.vahia.monappli;
     
    import android.app.Activity;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentActivity;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.app.FragmentTransaction;
    import android.util.DisplayMetrics;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
     
    public class MainActivity extends FragmentActivity {
    	private int height = 1;
    	private Button1 = null;
    	private Button2 = null;
    	private Button3 = null;
    	public Fragment layoutcontenuapd5;
    	public Fragment Frag;
    	public Fragment firstFragGen;
    	private static String frag3Tag;
    	private static String frag4Tag;
    	private static String frag1Tag;
    	private static String frag2Tag;
    	private String mFragment;
    	private final Frag1 mFrag1 = new Frag1();
    	private final Frag2 mFrag2 = new ();
    	public Frag3 mFrag3 = new Frag3();
    	private final Frag4 mFrag4 = new Frag4();
    	private final PassesD5 mPassesD5 = new PassesD5();
     
    	public TextView textView1 = null;
    	public TextView textView2 = null;
    	public TextView textView3 = null;
    	public TextView textView4 = null;
    	public TextView textView5 = null;
    	public TextView textView6 = null;
    	public TextView textView7 = null;
    	public TextView textView8 = null;
    	public TextView textView9 = null;
    	public TextView textView10 = null;
     
     
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		DisplayMetrics metrics = new DisplayMetrics();
    		getWindowManager().getDefaultDisplay().getMetrics(metrics);
    		height = metrics.heightPixels;
    		if (height == 1080)
    			setContentView(R.layout.activity_main_activity_d51080);
    		else
    		setContentView(R.layout.activity_main_activity_d5);
     
    		firstFrag();
     
    		bouton1 = (Button) findViewById(R.id.bouton1);
    		bouton1.setOnClickListener(new View.OnClickListener() {
    			@Override
    			public void onClick(View mainView) {				
    				goToFrag3(mainView);
     
    				//mFrag3.passes();
    				bouton1();
    			}});
     
    	}
     
    	@Override
    	public boolean onCreateOptionsMenu(Menu menu) {
    		// Inflate the menu; this adds items to the action bar if it is present.
    		getMenuInflater().inflate(R.menu.main_activity, menu);
    		return true;
    	}
     
    	@Override
    	public boolean onOptionsItemSelected(MenuItem item) {
    		// Handle action bar item clicks here. The action bar will
    		// automatically handle clicks on the Home/Up button, so long
    		// as you specify a parent activity in AndroidManifest.xml.
    		int id = item.getItemId();
    		if (id == R.id.action_settings) {
    			return true;
    		}
    		return super.onOptionsItemSelected(item);
    	}
     
     
    	private void showFrag (final Fragment fragment) {
    		if (fragment == null)
    			return;
    		mFragment = ((Object) fragment).getClass().getSimpleName();
    		final FragmentManager fm = getSupportFragmentManager();
    		final FragmentTransaction ft = fm.beginTransaction();
    		ft.replace(R.id.frag, fragment, frag3Tag);
    		ft.commit();
    	}
     
    	private void firstFrag () {
    		firstFragGen = new Frag0();
    		final FragmentManager fm1 = getSupportFragmentManager();
    		final FragmentTransaction ft1 = fm1.beginTransaction();
    		ft1.add(R.id.frag, firstFragGen);
    		ft1.commit();
    	}
     
    	public void goToFrag1(View mainView) {
    		showFrag(this.mFrag1);
    	}
     
    	public void goToFrag2(View mainView) {
    		showFrag(this.mFrag2);
    	}
     
    	public void goToFrag3(View mainView) {
    		showFrag(this.mFrag3);
    	}
     
    	public void goToFrag4(View mainView) {
    		showFrag(this.mFrag4);
    	}
     
    	public void bouton1(){
     
    		textView1 = (TextView) findViewById(R.id.textview1);
    		textView2 = (TextView) findViewById(R.id.textview2);
    		textView3 = (TextView) findViewById(R.id.textview3);
    		textView4 = (TextView) findViewById(R.id.textview4);
    		textView5 = (TextView) findViewById(R.id.textview5);
    		textView6 = (TextView) findViewById(R.id.textview6);
    		textView7 = (TextView) findViewById(R.id.textview7);
    		textView8 = (TextView) findViewById(R.id.textview8);
    		textView9 = (TextView) findViewById(R.id.textview9);
    		textView10 = (TextView) findViewById(R.id.textview10);
     
    		textView1.setText("texte1");
    		textView2.setText("texte2");
    		textView3.setText("texte3");
    		textView4.setText("texte4");
    		textView5.setText("texte5");
    		textView6.setText("texte6");
    		textView7.setText("texte7");
    		textView8.setText("texte8");
    		textView9.setText("texte9");
    		textView10.setText("texte10");
    	}	
    }
    celui de son layout :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="left"
        android:orientation="horizontal"
        tools:context="com.vahia.monappli.MainActivity" >        
        <ScrollView
            android:layout_width="275dp"
            android:layout_height="wrap_content"
            android:gravity="center" >
           <LinearLayout
                android:layout_width="275dp"
            	android:layout_height="wrap_content"
            	android:orientation="vertical"
            	android:layout_gravity="left" >        	
            	<Button
                        android:id="@+id/bouton1"
            	    style="@style/whiteButtonTitle"
            	    android:layout_width="fill_parent"
            	    android:layout_height="50dp"
            	    android:text="@string/ba101" />
            	<View
            	    android:layout_width="fill_parent"
            	    android:layout_height="0.2dp"
            	    style="@style/trait" />
                <Button
                    android:id="@+id/bouton2"
                    android:layout_width="fill_parent"
                    android:layout_height="50dp"
                    android:text="@string/passes"                
                    style="@style/whiteButton" />
                <View
            	    android:layout_width="fill_parent"
            	    android:layout_height="0.2dp"
            	    style="@style/trait" />
                <Button
                    android:id="@+id/bouton3"
                    android:layout_width="fill_parent"
                    android:layout_height="50dp"
                    android:text="@string/gazhp"                
                    style="@style/whiteButton" />
                <View
            	    android:layout_width="fill_parent"
            	    android:layout_height="0.2dp"
            	    style="@style/trait" />
            </LinearLayout>
        </ScrollView>
        <TableLayout 
       		android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center" >
            <TableRow
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >
            <View
                android:layout_width="2dp"
                android:background="#adadad" />
            </TableRow>
    </TableLayout>
    	<LinearLayout
            android:id="@+id/frag"
            android:orientation="vertical"
            android:layout_width="1000dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center" >
        </LinearLayout>
    </LinearLayout>
    Le code de mon fragment :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    package com.vahia.monappli;
     
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.TextView;
     
    public class Frag3 extends Fragment {
     
    	public TextView textView1 = null;
    	public TextView textView2 = null;
    	public TextView textView3 = null;
    	public TextView textView4 = null;
    	public TextView textView5 = null;
    	public TextView textView6 = null;
    	public TextView textView7 = null;
    	public TextView textView8 = null;
    	public TextView textView9 = null;
    	public TextView textView10 = null;
    	private View mainView;
     
    	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    		View mainView = inflater.inflate(R.layout.layoutfrag3, container, false);
     
    		textView1 = (TextView) mainView.findViewById(R.id.textview1);
    		textView2 = (TextView) mainView.findViewById(R.id.textview2);
    		textView3 = (TextView) mainView.findViewById(R.id.textview3);
    		textView4 = (TextView) mainView.findViewById(R.id.textview4);
    		textView5 = (TextView) mainView.findViewById(R.id.textview5);
    		textView6 = (TextView) mainView.findViewById(R.id.textview6);
    		textView7 = (TextView) mainView.findViewById(R.id.textview7);
    		textView8 = (TextView) mainView.findViewById(R.id.textview8);
    		textView9 = (TextView) mainView.findViewById(R.id.textview9);
    		textView10 = (TextView) mainView.findViewById(R.id.textview10);
     
    		//bouton1();
     
    		return mainView;
     
    }
     
     
     
    	public void bouton1(){
    		textView1.setText("texte1");
    		textView2.setText("texte2");
    		textView3.setText("texte3");
    		textView4.setText("texte4");
    		textView5.setText("texte5");
    		textView6.setText("texte6");
    		textView7.setText("texte7");
    		textView8.setText("texte8");
    		textView9.setText("texte9");
    		textView10.setText("texte10");
    	}
     
    }
    et celui du layout du fragment :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
     
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="left"
        android:orientation="horizontal"
        android:id="@+id/layoutfrag3" >        
     
    <ScrollView
            android:layout_width="1005dp"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal" >
    <TableLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"    
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="20dp" >
        <TableRow 
            android:layout_width="wrap_content"
        	android:layout_height="wrap_content"
        	android:gravity="center" >
            <TextView
                android:id="@+id/textview1"
                android:layout_width="wrap_content"
            	android:layout_height="wrap_content"
            	android:gravity="center"        	
            	android:text=" "
            	style="@style/titre" />
        </TableRow>
        <TableRow 
            android:layout_width="wrap_content"
        	android:layout_height="wrap_content"
        	android:gravity="center" >
            <TextView
                android:id="@+id/textview2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="15dp"
                android:layout_marginBottom="30dp"
                android:gravity="center"
                android:text=" "
                style="@style/description" />
        </TableRow>
    	<TableRow 
            android:layout_width="wrap_content"
        	android:layout_height="wrap_content"
        	android:gravity="center" >
           	<TextView
                android:id="@+id/textview3"
                android:layout_width="35dp"
                android:layout_height="wrap_content"
                android:layout_marginBottom="5dp"
                android:gravity="center"
                android:text=" "
                style="@style/catnum" />
           	<TextView
                android:id="@+id/textview4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="5dp"
                android:gravity="center"
                android:text=" "
                style="@style/catsil" />
        </TableRow>
        <TableRow 
            android:layout_width="wrap_content"
        	android:layout_height="wrap_content"
        	android:gravity="center" >
            <TextView
                android:layout_width="wrap_content"
            	android:layout_height="wrap_content"
            	android:gravity="center"        	
            	style="@style/risque" />
            <TextView
                android:id="@+id/textview5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_span="3"
                android:gravity="center"
                android:text=" "
                style="@style/catnum" />
        </TableRow>
        <TableRow 
            android:layout_width="wrap_content"
        	android:layout_height="wrap_content"
        	android:gravity="center" >
            <TextView
                android:id="@+id/textview6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_span="4"
                android:gravity="center"
                android:text=" "
                style="@style/catnum" />
        </TableRow>
    	<TableRow 
            android:layout_width="wrap_content"
        	android:layout_height="wrap_content"
        	android:gravity="center" >
            <TextView
                android:id="@+id/textview7"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_span="4"
                android:inputType="textMultiLine"
                android:gravity="center"
                android:text=" "
                style="@style/cataction" />
           </TableRow>
           <TableRow 
            android:layout_width="wrap_content"
        	android:layout_height="wrap_content"
        	android:gravity="center" >
            <TextView
                android:id="@+id/textview8"
                android:layout_width="wrap_content"
                android:layout_height="15dp"
                android:gravity="center"
                android:text=" "
                style="@style/cataction" />
            </TableRow>
           <TableRow 
            android:layout_width="wrap_content"
        	android:layout_height="wrap_content"
        	android:gravity="center" >
            <View
            	    android:layout_width="fill_parent"
            	    android:layout_height="0.2dp" 
            	    android:layout_span="6"       	    
            	    style="@style/trait" />
            </TableRow>
            <TableRow 
            android:layout_width="wrap_content"
        	android:layout_height="wrap_content"
        	android:gravity="center" >
            <TextView
                android:id="@+id/textview9"
                android:layout_width="wrap_content"
                android:layout_height="40dp"
                android:gravity="center"
                android:text=" "
                style="@style/cataction" />
        </TableRow>
       	<TableRow 
            android:layout_width="wrap_content"
        	android:layout_height="wrap_content"
        	android:gravity="center" >
            <TextView
                android:id="@+id/textview10"
                android:layout_width="wrap_content"
            	android:layout_height="wrap_content"
            	android:gravity="center"        	
            	android:text=" "
            	style="@style/titre" />
        </TableRow>        
    </TableLayout>
    </ScrollView>
    </LinearLayout
    Petite précision :
    - quand je supprime la demande d'implémentation des textviews, tout fonctionne. Les fragments se modifient et affichent les textviews vides.
    - quand je mets "bouton1()" (pour implémenter les textviews) dans le onCreateView du fragment, ça marche aussi, les textes apparaissent bien. Mais ça ne m'intéresse pas car je veux pouvoir appeler ce même fragment mais y implémenter différemment ses textviews suite au clic sur le bouton3 de MainActivity par exemple.
    - quand je place la méthode "bouton1()" dans le fragment et que je l'appelle depuis MainActivity via "mFrag3.bouton1();", j'ai un NullPointerException dans mon fragment à la ligne 47.
    - quand je place la méthode "bouton1()" dans le fragment et que je l'appelle depuis MainActivity via "ClasseDuFragment.bouton1();", j'ai une erreur dans Eclipse : "Cannot make a static reference to the non-static method bouton1() from the type Frag3".
    - quand je place la méthode "bouton1()" dans MainActivity, quand je l'appelle j'ai un NullPointerException dans MainActivity à la ligne 139.

  16. #36
    Membre du Club
    Homme Profil pro
    Développeur Java
    Inscrit en
    Juin 2015
    Messages
    185
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Développeur Java

    Informations forums :
    Inscription : Juin 2015
    Messages : 185
    Points : 54
    Points
    54
    Par défaut
    ça ne pourrait pas être un problème de variable ou autres à déclarer en static ?

  17. #37
    Expert éminent

    Homme Profil pro
    Ingénieur systèmes et réseaux
    Inscrit en
    Février 2007
    Messages
    4 253
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Ingénieur systèmes et réseaux
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Février 2007
    Messages : 4 253
    Points : 7 618
    Points
    7 618
    Billets dans le blog
    3
    Par défaut
    Non mais ça:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
     
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    		View mainView = inflater.inflate(R.layout.layoutfrag3, container, false);
     
    		textView1 = (TextView) findViewById(R.id.textview1);
    		textView2 = (TextView) findViewById(R.id.textview2);
    		textView3 = (TextView) findViewById(R.id.textview3);
    		textView4 = (TextView) findViewById(R.id.textview4);
    		textView5 = (TextView) findViewById(R.id.textview5);
    		textView6 = (TextView) findViewById(R.id.textview6);
    		textView7 = (TextView) findViewById(R.id.textview7);
    		textView8 = (TextView) findViewById(R.id.textview8);
    		textView9 = (TextView) findViewById(R.id.textview9);
    		textView10 = (TextView) findViewById(R.id.textview10);
     
    		//bouton1();
     
    		return mainView;
     
    }
    C'est pas bon...

    findViewById va systématiquement retourner null, puisque les views ne sont pas encore passées au fragment (on est en train des les créer, et sont référencées par "mainView" dans le code ci-dessus).

    If faut donc faire: mainView.findViewById() !
    N'oubliez pas de cliquer sur mais aussi sur si un commentaire vous a été utile !
    Et surtout

  18. #38
    Membre du Club
    Homme Profil pro
    Développeur Java
    Inscrit en
    Juin 2015
    Messages
    185
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Développeur Java

    Informations forums :
    Inscription : Juin 2015
    Messages : 185
    Points : 54
    Points
    54
    Par défaut
    Je me suis manqué en copiant le code, dans mon fragment mes textviews sont bien déclarés mainView.findViewById()...

    Voilà, corrigé (mille excuses ).

  19. #39
    Membre du Club
    Homme Profil pro
    Développeur Java
    Inscrit en
    Juin 2015
    Messages
    185
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Développeur Java

    Informations forums :
    Inscription : Juin 2015
    Messages : 185
    Points : 54
    Points
    54
    Par défaut
    J'ai vu pas mal de topics sur le net traitant de mon problème. J'ai testé leurs solutions mais rien ne fonctionne pour moi.

Discussions similaires

  1. Changer image d'un imageView dans un fragment depuis l'activity
    Par toufou dans le forum Composants graphiques
    Réponses: 2
    Dernier message: 30/10/2014, 08h52
  2. Retrouver des fragments depuis le fragment manager
    Par roukish dans le forum Android
    Réponses: 0
    Dernier message: 16/05/2014, 11h14
  3. Raffraichissement des Fragments depuis l'activity
    Par stelincat dans le forum Android
    Réponses: 8
    Dernier message: 15/04/2014, 19h23
  4. Réponses: 7
    Dernier message: 12/07/2012, 11h58
  5. Compréhension d'un fragment de code inconnu
    Par Cyberstein dans le forum Langage
    Réponses: 5
    Dernier message: 22/01/2008, 18h43

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo