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

Composants graphiques Android Discussion :

ViewPager Mise à jour des fragments


Sujet :

Composants graphiques Android

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Homme Profil pro
    Inscrit en
    Décembre 2013
    Messages
    26
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Décembre 2013
    Messages : 26
    Par défaut ViewPager Mise à jour des fragments
    Bonjour,

    Comme l'indique l'intitulé, j'ai un ViewPager avec 3 fragments et je veux mettre à jour le 3ème (TextView et ImageView) selon la valeur du checkID d'un radioButton dans le 2ème. J'ai donc implémenté un interface pour mettre à jour ces fragments. Mais j'obtiens un null pointer exception lorsque que j'appelle cette interface.

    Les codes ci dessous :

    FragmentActivity :

    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
    public class Commander extends FragmentActivity implements OnPaiementSelectedListener  {
    	ViewPager Tab;
        TabPagerAdapter TabAdapter;
    	ActionBar actionBar;
     
     
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.etapescommande);
     
            TabAdapter = new TabPagerAdapter(getSupportFragmentManager());
     
            Tab = (ViewPager)findViewById(R.id.pager);
            Tab.setOnPageChangeListener(
                    new ViewPager.SimpleOnPageChangeListener() {
                        @Override
                        public void onPageSelected(int position) {
     
                        	actionBar = getActionBar();
                        	actionBar.setSelectedNavigationItem(position);                  
                        	}
                    });
            Tab.setAdapter(TabAdapter);
     
            actionBar = getActionBar();
            //Enable Tabs on Action Bar
            actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
            ActionBar.TabListener tabListener = new ActionBar.TabListener(){
     
    			@Override
    			public void onTabReselected(android.app.ActionBar.Tab tab,
    					FragmentTransaction ft) {
    				// TODO Auto-generated method stub
     
    			}
     
    			@Override
    			 public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
     
    	            Tab.setCurrentItem(tab.getPosition());
    	        }
     
    			@Override
    			public void onTabUnselected(android.app.ActionBar.Tab tab,
    					FragmentTransaction ft) {
    				// TODO Auto-generated method stub
     
    			}};
    			//Add New Tab
    			actionBar.addTab(actionBar.newTab().setText("Ma commande").setTabListener(tabListener));
    			actionBar.addTab(actionBar.newTab().setText("Paiement").setTabListener(tabListener));
    			actionBar.addTab(actionBar.newTab().setText("Validation").setTabListener(tabListener));
     
        }
     
    	@Override
    	public void onPaiementSelected(String Paiement) {
    		// TODO Auto-generated method stub
    		if(Paiement != null){
    		SharedPreferences app_preferencesP = PreferenceManager.getDefaultSharedPreferences(Commander.this);
            SharedPreferences.Editor editorP = app_preferencesP.edit();
            editorP.putString("SavePaiement", Paiement);
            editorP.commit();  
    		}
    	}
     
    }
    Fragment de droite :

    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
    public class ValidationCommande extends Fragment  {
     
    	Button ValidationCommande;
    	TextView ModeDePaiement;
    	ImageView ImagePaiement;
    	String Paiement,mail;
     
    	 	OnPaiementSelectedListener mListener;
     
    	    @Override
    	    public void onAttach(Activity activity) {
    	        super.onAttach(activity);
    	        try {
    	            mListener = (OnPaiementSelectedListener) activity;
     
    	            SharedPreferences app_preferences2 = PreferenceManager.getDefaultSharedPreferences(getActivity());
        			mail = app_preferences2.getString("key", "null");
     
        	        if(Paiement != null) {
        	        Log.i("SavePaiementV",Paiement);
        	        }
     
     
    	        } catch (ClassCastException e) {
    	            throw new ClassCastException(activity.toString() + " must implement OnPaiementSelectedListener");
    	        }
    	    }
     
     
     
    		 @Override
    		 public View onCreateView(LayoutInflater inflater, ViewGroup container,
    		            Bundle savedInstanceState) {			 
    		        View validationcommande =  inflater.inflate(R.layout.validationcommande, container, false);	
     
    		        ModeDePaiement = (TextView)validationcommande.findViewById(R.id.RecapPaiement);
     
    				ImagePaiement = (ImageView)validationcommande.findViewById(R.id.RecapPaiementImage);
     
        			if(Paiement == null) {
    					ModeDePaiement.setText(null);
    		        	ImagePaiement.setImageResource(0);	
    				}			
    				else if(Paiement.equals("0")) {
    		        	ModeDePaiement.setText("Carte bancaire");
    		        	ImagePaiement.setImageResource(R.drawable.modedepaiement);
    		        }
    				else if(Paiement.equals("1")) {
    		        	ModeDePaiement.setText("Compte Paypal");
    		        	ImagePaiement.setImageResource(R.drawable.paypal);
    		        }
    		        else if(Paiement.equals("2")){
    		        	ModeDePaiement.setText("Chèque");
    		        	ImagePaiement.setImageResource(R.drawable.cheque);
    		        } 
     
     
    		        return validationcommande;
    		 }
     
    		 	@Override
    		 	public void onActivityCreated(Bundle savedInstanceState) {
    				super.onActivityCreated(savedInstanceState);
    				View v= getView();
           ...........................
    Fragment du milieu (où se trouve les radioButton) :

    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
    public class Paiement extends Fragment {
     
    	private RadioGroup radioPaiement;
     
    	OnPaiementSelectedListener mListener;
     
    	String Paiement;
     
    	 @Override
    	    public View onCreateView(LayoutInflater inflater, ViewGroup container,
    	            Bundle savedInstanceState) {	 
    		    View paiement = inflater.inflate(R.layout.paiement, container, false);
     
    	        return paiement;
    	 	}
     
    	 @Override
    	 public void onActivityCreated(Bundle savedInstanceState) {
    			super.onActivityCreated(savedInstanceState);
    			View v= getView();
     
    			radioPaiement = (RadioGroup) v.findViewById(R.id.radioPaiement);
     
    			radioPaiement.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    				    @Override  
    				    public void onCheckedChanged(RadioGroup group, int checkedId) {
     
    				     RadioButton checkedRadioButton = (RadioButton)radioPaiement.findViewById(checkedId);
    				     int checkedIndex = radioPaiement.indexOfChild(checkedRadioButton);
     
    				     SharedPreferences app_preferencesP = PreferenceManager.getDefaultSharedPreferences(getActivity());
    			         SharedPreferences.Editor editorP = app_preferencesP.edit();
     
    			            Paiement = String.valueOf(checkedIndex);
    			            Log.i("Valeur radiobutton Paiement",Paiement);
    			            editorP.putString("SavePaiement", Paiement);
    			            editorP.commit();                 
    			            mListener.onPaiementSelected(Paiement);
    				    }
    				   });	
     
    	 }	 
     
    }
    Interface :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    package com.example.test;
     
    public interface OnPaiementSelectedListener {
            public void onPaiementSelected(String Paiement);
     
    }
    Trace :

    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
    02-22 06:13:24.412: E/AndroidRuntime(1111): FATAL EXCEPTION: main
    02-22 06:13:24.412: E/AndroidRuntime(1111): Process: com.example.test, PID: 1111
    02-22 06:13:24.412: E/AndroidRuntime(1111): java.lang.NullPointerException
    02-22 06:13:24.412: E/AndroidRuntime(1111): 	at com.example.test.Paiement$1.onCheckedChanged(Paiement.java:54)
    02-22 06:13:24.412: E/AndroidRuntime(1111): 	at android.widget.RadioGroup.setCheckedId(RadioGroup.java:174)
    02-22 06:13:24.412: E/AndroidRuntime(1111): 	at android.widget.RadioGroup.access$600(RadioGroup.java:54)
    02-22 06:13:24.412: E/AndroidRuntime(1111): 	at android.widget.RadioGroup$CheckedStateTracker.onCheckedChanged(RadioGroup.java:358)
    02-22 06:13:24.412: E/AndroidRuntime(1111): 	at android.widget.CompoundButton.setChecked(CompoundButton.java:130)
    02-22 06:13:24.412: E/AndroidRuntime(1111): 	at android.widget.CompoundButton.toggle(CompoundButton.java:87)
    02-22 06:13:24.412: E/AndroidRuntime(1111): 	at android.widget.RadioButton.toggle(RadioButton.java:76)
    02-22 06:13:24.412: E/AndroidRuntime(1111): 	at android.widget.CompoundButton.performClick(CompoundButton.java:99)
    02-22 06:13:24.412: E/AndroidRuntime(1111): 	at android.view.View$PerformClick.run(View.java:18422)
    02-22 06:13:24.412: E/AndroidRuntime(1111): 	at android.os.Handler.handleCallback(Handler.java:733)
    02-22 06:13:24.412: E/AndroidRuntime(1111): 	at android.os.Handler.dispatchMessage(Handler.java:95)
    02-22 06:13:24.412: E/AndroidRuntime(1111): 	at android.os.Looper.loop(Looper.java:136)
    02-22 06:13:24.412: E/AndroidRuntime(1111): 	at android.app.ActivityThread.main(ActivityThread.java:5017)
    02-22 06:13:24.412: E/AndroidRuntime(1111): 	at java.lang.reflect.Method.invokeNative(Native Method)
    02-22 06:13:24.412: E/AndroidRuntime(1111): 	at java.lang.reflect.Method.invoke(Method.java:515)
    02-22 06:13:24.412: E/AndroidRuntime(1111): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
    02-22 06:13:24.412: E/AndroidRuntime(1111): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
    02-22 06:13:24.412: E/AndroidRuntime(1111): 	at dalvik.system.NativeStart.main(Native Method)

    Cela fait des semaines que je bloque sur ce problème, je ne comprends vraiment pas comment mettre à jour les fragments dans un viewpager ><

    Merci d'avance,

    GJuubz

  2. #2
    Membre averti
    Profil pro
    Inscrit en
    Mars 2006
    Messages
    17
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2006
    Messages : 17
    Par défaut
    Hello GJuubz,

    Tu as mis [résolu] dans ton titre mais tu n'as pas écris comment tu as résolu ton problème.

    C'est dommage car je butte sur le même problème, j'ai un ViewPager avec un FragmentPagerAdapeter et 3 Fragments avec un TextView et je n'arrive pas a comprendre comment je peux mettre a jour les textes des TextView sur les 3 fragments de manières dynamique ?

    Si tu pouvais donner des indications sur la manière dont tu as résolu ton pb ce serait super cool.

    Merci d'avance.

    spicote

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

Discussions similaires

  1. Réponses: 10
    Dernier message: 03/03/2009, 11h46
  2. [JTable] mise à jour des données
    Par tripop dans le forum Composants
    Réponses: 3
    Dernier message: 04/02/2009, 18h52
  3. Réponses: 3
    Dernier message: 15/11/2005, 18h50
  4. [HTML] Problème mise à jour des fichiers en cache
    Par El Riiico dans le forum Balisage (X)HTML et validation W3C
    Réponses: 5
    Dernier message: 05/09/2005, 17h00
  5. Mise à jour des tables liées + TIMESTAMP
    Par Homegrown dans le forum Access
    Réponses: 11
    Dernier message: 25/04/2005, 21h52

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