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.