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 :

Problème Animations dans Fragments


Sujet :

Android

  1. #1
    Membre régulier
    Homme Profil pro
    Inscrit en
    Février 2011
    Messages
    59
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Février 2011
    Messages : 59
    Points : 79
    Points
    79
    Par défaut Problème Animations dans Fragments
    J'utilise un ViewPager avec android.support.v4 et des Fragments.
    Je planche depuis un moment sur le problème mais ne trouve aucune solutions même sur le net. Le but du jeu est qu'une animation se lance quand le Fragment est visible. Et pour déterminer quelle animation appliquer j'utilise pager.getCurrentItem(); puis un switch case. Ceci fonctionne mais je me retrouve avec un NullPointerException. Est-ce une question de Context?
    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
    160
    161
    162
    163
    package com.desiGn.space;
     
    import java.util.List;
    import java.util.Vector;
    import android.graphics.drawable.AnimationDrawable;
    import android.os.Bundle;
    import android.os.Handler;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentActivity;
    import android.support.v4.view.PagerAdapter;
    import android.support.v4.view.ViewPager;
    import android.support.v4.view.ViewPager.OnPageChangeListener;
    import android.view.animation.Animation;
    import android.view.animation.AnimationUtils;
    import android.widget.ImageView;
    import com.fragment.space.Fragment00;
    import com.fragment.space.Fragment01;
    import com.fragment.space.Fragment02;
    import com.fragment.space.Fragment03;
    import com.fragment.space.Fragment04;
    import com.fragment.space.Fragment05;
    import com.fragment.space.MyPagerAdapter;
     
    public class TexturesActivity extends FragmentActivity {
     
    	private PagerAdapter mPagerAdapter;
    	public ViewPager pager;
    	Fragment fragment0 = null;
    	Fragment fragment01 = null;
    	Fragment fragment02 = null;
    	Fragment fragment03 = null;
    	Fragment fragment04 = null;
    	Fragment fragment05 = null;
     
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		super.setContentView(R.layout.viewpager_layout);
     
    		// initialize the pager
    		this.initialisePaging();
    	}
     
    	/**
             * Initialize the fragments to be paged
             */
     
    	private void initialisePaging() {
     
    		List<Fragment> fragments = new Vector<Fragment>();
    		Fragment fragment0 = Fragment.instantiate(this,
    				Fragment00.class.getName());
    		Fragment fragment01 = Fragment.instantiate(this,
    				Fragment01.class.getName());
    		Fragment fragment02 = Fragment.instantiate(this,
    				Fragment02.class.getName());
    		Fragment fragment03 = Fragment.instantiate(this,
    				Fragment03.class.getName());
    		Fragment fragment04 = Fragment.instantiate(this,
    				Fragment04.class.getName());
    		Fragment fragment05 = Fragment.instantiate(this,
    				Fragment05.class.getName());
    		fragments.add(fragment0);
    		fragments.add(fragment01);
    		fragments.add(fragment02);
    		fragments.add(fragment03);
    		fragments.add(fragment04);
    		fragments.add(fragment05);
    		this.mPagerAdapter = new MyPagerAdapter(
    				super.getSupportFragmentManager(), fragments);
    		pager = (ViewPager) super.findViewById(R.id.viewpager);
    		pager.setAdapter(this.mPagerAdapter);
    		// pager.setFocusable(false);
    		pager.setOnPageChangeListener(new OnPageChangeListener() {
    			@Override
    			public void onPageScrollStateChanged(int arg0) {
    				// lostFocus();
    			}
     
    			@Override
    			public void onPageScrolled(int arg0, float arg1, int arg2) {
    				// do nothing
     
    			}
     
    			@Override
    			public void onPageSelected(int arg0) {
    				isSelected();
    			}
    		});
     
    	}
     
    	private void isSelected() {
    		Handler handler = new Handler();
    		Runnable r = new Runnable() {
    			public void run() {
    				startAnimation(pager.getCurrentItem());
    			}
    		};
    		handler.postDelayed(r, 2000);
     
    	}
     
     
    	public void lostFocus() {
    		//ICI ON ARRETE L'ANIMATION EN COURS
     
    		//Test :
    		// Context context = getApplicationContext();
    		// CharSequence text = "lost focus";
    		// int duration = 1500;
    		// Toast toast = Toast.makeText(context, text, duration);
    		// toast.show();
    	}
     
    	private void startAnimation(int id_frag) {
    		switch (id_frag) {
    		case 0: {
     
    			ImageView anim_view = (ImageView) findViewById(R.id.image_view_asphalt_01); // tween animation
    			Animation fader = AnimationUtils.loadAnimation(
    					getApplicationContext(), R.anim.fade);
    			anim_view.startAnimation(fader);  //Celle-ci est ignorée au lancement
    		}
    		case 1: {
    			ImageView anim_view_01 = (ImageView) findViewById(R.id.image_view_asphalt_02); // image par image
    			anim_view_01.setBackgroundResource(R.drawable.anim_asphalt_02);
    			AnimationDrawable animate = (AnimationDrawable) anim_view_01
    					.getBackground();
    			animate.start();     // ICI JE ME RETROUVE AVEC UN NullPointerException
    		}
    		case 2: {
    			ImageView myImageView = (ImageView) findViewById(R.id.image_view_asphalt_03);// tween animation
    			myImageView.setImageResource(R.drawable.anim_asphalt_turquoise); 
    			Animation myFadeInAnimation = AnimationUtils.loadAnimation(this,
    					R.anim.anim_asphalt_03);
    			myImageView.startAnimation(myFadeInAnimation); 
    			myImageView.startAnimation(myFadeInAnimation);
    		}
    		case 3: {
    			ImageView anim_view_04 = (ImageView) findViewById(R.id.image_view_asphalt_04); // tween animation
    			Animation circle = AnimationUtils.loadAnimation(
    					getApplicationContext(), R.anim.anim_asphalt_04);
    			anim_view_04.startAnimation(circle);
    		}
    		case 4: {
    			ImageView anim_view_05 = (ImageView) findViewById(R.id.image_view_asphalt_05); // image par image
    			anim_view_05.setBackgroundResource(R.drawable.anim_asphalt_05);
    			AnimationDrawable animate = (AnimationDrawable) anim_view_05
    					.getBackground();
    			animate.start();
    		}
    		case 5: {
    			ImageView anim_view_06 = (ImageView) findViewById(R.id.image_view_asphalt_06);// tween animation
    			Animation fader = AnimationUtils.loadAnimation(
    					getApplicationContext(), R.anim.anim_asphalt_04);
    			anim_view_06.startAnimation(fader);
    		}
     
    		}
    	}
    }
    Ceci est une ébauche, merci de vos conseils.

  2. #2
    Modérateur
    Avatar de Hizin
    Homme Profil pro
    Développeur mobile
    Inscrit en
    Février 2010
    Messages
    2 180
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France

    Informations professionnelles :
    Activité : Développeur mobile

    Informations forums :
    Inscription : Février 2010
    Messages : 2 180
    Points : 5 072
    Points
    5 072
    Par défaut
    Stacktrace de l'erreur SVP.
    C'est Android, PAS Androïd, ou Androïde didiou !
    Le premier est un OS, le second est la mauvaise orthographe du troisième, un mot français désignant un robot à forme humaine.

    Membre du comité contre la phrase "ça marche PAS" en titre et/ou explication de problème.

    N'oubliez pas de consulter les FAQ Android et les cours et tutoriels Android

  3. #3
    Membre régulier
    Homme Profil pro
    Inscrit en
    Février 2011
    Messages
    59
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Février 2011
    Messages : 59
    Points : 79
    Points
    79
    Par défaut Stacktrace
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    10-12 18:30:05.682: E/AndroidRuntime(6212): FATAL EXCEPTION: main
    10-12 18:30:05.682: E/AndroidRuntime(6212): java.lang.NullPointerException
    10-12 18:30:05.682: E/AndroidRuntime(6212): 	at com.desiGn.space.TexturesActivity.startAnimation(TexturesActivity.java:145)
    10-12 18:30:05.682: E/AndroidRuntime(6212): 	at com.desiGn.space.TexturesActivity.access$1(TexturesActivity.java:117)
    10-12 18:30:05.682: E/AndroidRuntime(6212): 	at com.desiGn.space.TexturesActivity$2.run(TexturesActivity.java:98)
    10-12 18:30:05.682: E/AndroidRuntime(6212): 	at android.os.Handler.handleCallback(Handler.java:615)
    10-12 18:30:05.682: E/AndroidRuntime(6212): 	at android.os.Handler.dispatchMessage(Handler.java:92)
    10-12 18:30:05.682: E/AndroidRuntime(6212): 	at android.os.Looper.loop(Looper.java:137)
    10-12 18:30:05.682: E/AndroidRuntime(6212): 	at android.app.ActivityThread.main(ActivityThread.java:4745)
    10-12 18:30:05.682: E/AndroidRuntime(6212): 	at java.lang.reflect.Method.invokeNative(Native Method)
    10-12 18:30:05.682: E/AndroidRuntime(6212): 	at java.lang.reflect.Method.invoke(Method.java:511)
    10-12 18:30:05.682: E/AndroidRuntime(6212): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
    10-12 18:30:05.682: E/AndroidRuntime(6212): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    10-12 18:30:05.682: E/AndroidRuntime(6212): 	at dalvik.system.NativeStart.main(Native Method)

  4. #4
    Modérateur
    Avatar de Hizin
    Homme Profil pro
    Développeur mobile
    Inscrit en
    Février 2010
    Messages
    2 180
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France

    Informations professionnelles :
    Activité : Développeur mobile

    Informations forums :
    Inscription : Février 2010
    Messages : 2 180
    Points : 5 072
    Points
    5 072
    Par défaut
    Une NPE, l'erreur la plus commune et la plus rencontrée.

    Je pars du postulat que la ligne 145 de TextureActivity correspond à animate.start(); // ICI JE ME RETROUVE AVEC UN NullPointerException. Ce n'est pas indiqué, donc si le postulat est faux, la suite le sera

    Ta variable "animate" est null, donc (AnimationDrawable) anim_view_01.getBackground(); renvoi null.

    Passe un coup de débogueur et regarde l'état des variables grâce à des breakpoints.
    C'est Android, PAS Androïd, ou Androïde didiou !
    Le premier est un OS, le second est la mauvaise orthographe du troisième, un mot français désignant un robot à forme humaine.

    Membre du comité contre la phrase "ça marche PAS" en titre et/ou explication de problème.

    N'oubliez pas de consulter les FAQ Android et les cours et tutoriels Android

  5. #5
    Membre régulier
    Homme Profil pro
    Inscrit en
    Février 2011
    Messages
    59
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Février 2011
    Messages : 59
    Points : 79
    Points
    79
    Par défaut résolu
    Merci pour le conseil. J'utilise le debugger et les breakpoints...En fait j'avais écrit les animations alors que je débutais et je ne les ais pas vérifiées donc certaines ne fonctionnaient pas d'où l'erreur.
    Par contre j'ai trouvé une façon bien plus simple de lancer l'animation à partir du Fragment. Je ne connaissais pas la méthode monFragment.getActivity(); qui retourne l'instance de l'Activity "mère" et ne pouvais donc utiliser findViewById(R.id.mon_image_view).
    Voici le code (bien plus simple) si cela peut être utile à quelqu'un:
    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
    package com.fragment.space;
     
    import android.app.Activity;
    import android.graphics.drawable.AnimationDrawable;
    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.ImageView;
    import android.widget.LinearLayout;
    import com.desiGn.space.R;
     
     
    public class Fragment01 extends Fragment {
     
    	private LinearLayout ll;
     
    	@Override
    	public View onCreateView(LayoutInflater inflater, ViewGroup container,
    			Bundle savedInstanceState) {
    		if (container == null) {
    			return null;
    		}
     
    		ll = (LinearLayout) inflater.inflate(R.layout.lay_asphalt_02,
    				container, false);
     
    		return ll;
     
    	}
     
    	@Override
    	public void onStart(){
    		super.onStart();
    		 Activity activity = this.getActivity();  //Le fragment accede à l'instance de l'activité parente
    		 ImageView anim_view_01=
    		 (ImageView)activity.findViewById(R.id.image_view_asphalt_02); //On récupère la vue grâce à l'Activity		 
    		 anim_view_01.setBackgroundResource(R.drawable.anim_asphalt_02);
    		 AnimationDrawable animate = (AnimationDrawable) anim_view_01
    		 .getBackground();
    		 animate.start();	
    	}
    }

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Problème UDP dans un fragment
    Par NeoSyder dans le forum Android
    Réponses: 4
    Dernier message: 24/03/2015, 15h52
  2. j'ai un problème animation de photo dans flash mx
    Par freshman dans le forum Flash
    Réponses: 1
    Dernier message: 31/08/2007, 10h36
  3. Problème Préload dans animation Flash
    Par papillonmillenium dans le forum Flash
    Réponses: 2
    Dernier message: 29/08/2007, 14h48
  4. [Director] Problème d'animation dans navigateur
    Par phendile dans le forum Autres langages
    Réponses: 1
    Dernier message: 01/12/2004, 20h36
  5. Plusieurs animations dans un fichier .X
    Par heidi79 dans le forum DirectX
    Réponses: 6
    Dernier message: 25/08/2003, 02h26

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