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 :

Problem Listener DialogFragment


Sujet :

Android

  1. #1
    Membre régulier
    Homme Profil pro
    Architecte de base de données
    Inscrit en
    Février 2013
    Messages
    53
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 46
    Localisation : Norvège

    Informations professionnelles :
    Activité : Architecte de base de données
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Février 2013
    Messages : 53
    Points : 78
    Points
    78
    Par défaut Problem Listener DialogFragment
    j essai de position de l enregistrement modifie pour le fragment parent et notify l adapter dans ce dernier

    voila le DialogFragment

    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
     
     
    public class DialogEditVerb extends DialogFragment {
     
    	private static final String TAG = DialogEditVerb.class.getSimpleName();
     
     
    	   public  OnHeadlineSelectedListener mCallback ;
    	   public  DataSources mydata;
     
    	   public String getidverb;
    	   public String getmorsmal;
    	   public String geteksempel;
     
    	   public DialogEditVerb() {
    	        // Empty constructor required for DialogFragment
    	    }
     
    	   public interface OnHeadlineSelectedListener {
    	       /** Called by HeadlinesFragment when a list item is selected */
    	       public void onArticleSelected(int position);
    	   } 
     
    	   public static DialogEditVerb newInstance() {
    			 DialogEditVerb frag = new DialogEditVerb();
    			return frag; 
     
    	   }
     
    	   @Override
    		public void onAttach(Activity activity) {
    			super.onAttach(activity);
    			Fragment parentFragment = getParentFragment();
     
     
    			// Check if parent fragment (if there is one) implements the image
    			// selection interface
    			if (parentFragment != null && parentFragment instanceof OnHeadlineSelectedListener) {
    				mCallback = (OnHeadlineSelectedListener) parentFragment;
    			}
    			// Otherwise, check if parent activity implements the image
    			// selection interface
    			else if (activity != null && activity instanceof OnHeadlineSelectedListener) {
    				mCallback = (OnHeadlineSelectedListener) activity;
    			}
    			// If neither implements the image selection callback, warn that
    			// selections are being missed
    			else if (mCallback == null) {
    				Log.w(TAG, "onAttach: niether the parent fragment or parent activity implement OnImageSelectedListener, "
    						+ "image selections will not be communicated to other components");
    			}
    		}  
     
    	 public static DialogEditVerb newInstance(String trad,String expl,String pos) {
    		 DialogEditVerb frag = new DialogEditVerb();
    	        Bundle args = new Bundle();
    	        args.putString("keyidverb", pos);
    	        args.putString("keymorsmal", trad);
                args.putString("keyeksempel", expl);
     
     
    	        frag.setArguments(args);
    	        return frag;
    	    }
     
    	 @Override
    	public void onCreate(Bundle savedInstanceState) {
    		 super.onCreate(savedInstanceState); 
     
    		 mydata = new DataSources(getActivity().getBaseContext());
     
    		 mydata.open();
     
    		 Bundle extras = getArguments();
     
    		 if (extras != null) {
    		 //    
    			 this.getidverb = extras.getString("keyidverb");
    			 this.getmorsmal = extras.getString("keymorsmal");
     
    			 this.geteksempel = extras.getString("keyeksempel");
     
    		 } 
    	 }
     
    	 @Override
    		public Dialog onCreateDialog(Bundle savedInstanceState) {
     
     
     
     
    			AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity());	
     
     
    		final View view = getActivity().getLayoutInflater().inflate(R.layout.edit_verb,null);
     
    		 final EditText txverb = (EditText) view.findViewById(R.id.editTextMorsmal);
       		 final EditText txeksempel = (EditText) view.findViewById(R.id.editTextEksempel);
     		 txverb.setText(getmorsmal);
             txeksempel.setText(geteksempel);
     
    	 dialogBuilder.setView(view);	
     
            dialogBuilder.setTitle("Edit Verb");
         dialogBuilder.setCancelable(true);
     
     
                                    dialogBuilder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                                              @Override
                                                public void onClick(DialogInterface dialog, int which) {
     
     
     
                                         		 final String m = txverb.getText().toString();
                                           	     final String e = txeksempel.getText().toString(); 
     
     
     
                                                        //validation code
                                            	 if(getidverb!="")
     
                             				 {
     
                             					mydata.updateVerb(Integer.valueOf(getidverb), m, e);
     
                             					Log.e("IDVERB", getidverb+m+e);
     
                             					}
     
                             	FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
                              ImageListFragment imageListFragment = (ImageListFragment)          fragmentManager.findFragmentByTag(ImageListFragment.class.getSimpleName());
     
                             					if (imageListFragment != null) {
                             					imageListFragment.onArticleSelected(Integer.valueOf(getidverb));
                             					}
     
     
                             					//if (mCallback	!= null) {
     
                                               // mCallback.onArticleSelected(Integer.valueOf(getidverb));
     
                             					//}
     
     
                                              }
                                      });
     
                                    dialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                                    	                                    @Override
                                                                         public void onClick(DialogInterface dialog, int which) {
                                    	                                             dialog.cancel();
                                    	                                     }
                                    	                            });  
     
                          return dialogBuilder.create();
     
    	}	
     
    }
    voila le logcat


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
     
    03-30 22:00:40.218: E/AndroidRuntime(9113): FATAL EXCEPTION: main
    03-30 22:00:40.218: E/AndroidRuntime(9113): java.lang.NullPointerException
    03-30 22:00:40.218: E/AndroidRuntime(9113): 	at com.android.norsksprak.verbs.DialogEditVerb$1.onClick(DialogEditVerb.java:169)
    03-30 22:00:40.218: E/AndroidRuntime(9113): 	at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
    03-30 22:00:40.218: E/AndroidRuntime(9113): 	at android.os.Handler.dispatchMessage(Handler.java:99)
    03-30 22:00:40.218: E/AndroidRuntime(9113): 	at android.os.Looper.loop(Looper.java:213)
    03-30 22:00:40.218: E/AndroidRuntime(9113): 	at android.app.ActivityThread.main(ActivityThread.java:4787)
    03-30 22:00:40.218: E/AndroidRuntime(9113): 	at java.lang.reflect.Method.invokeNative(Native Method)
    03-30 22:00:40.218: E/AndroidRuntime(9113): 	at java.lang.reflect.Method.invoke(Method.java:511)
    03-30 22:00:40.218: E/AndroidRuntime(9113): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
    03-30 22:00:40.218: E/AndroidRuntime(9113): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
    03-30 22:00:40.218: E/AndroidRuntime(9113): 	at dalvik.system.NativeStart.main(Native Method)

  2. #2
    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
    Bonjour,

    Comme indiqué dans les logs, il y a une référence (appel de fonction / utilisation membre) à un objet "null" à la ligne 169 de DialogEditVerb.java (dans une fonction "onClick" d'une classe anonyme de com.android.norsksprak.verbs.DialogEditVerb
    => NullPointerException



    La ligne 169 ne serait-elle pas juste après: ?

    Ce code est ignoble pour plein de raisons...
    1. getidverb peut être null (et null != "" puisque "" est une chaîne vide ayant une référence non nulle) => Integer.parseInt(getidverb) va de ce fait envoyer un NullPointerException

    2. il y a de toutes manière très peu de chances pour que getidverb ait exactement la même référence (soit le même objet) que "" ce que teste "!=" ou "=="

    Je pense que ce que tu voulais écrire est : if (getidverb != null && getidvers.length()>0)
    Ou (il me semble que cela exsite): if (StringUtils.isEmpty(getidverb))
    N'oubliez pas de cliquer sur mais aussi sur si un commentaire vous a été utile !
    Et surtout

  3. #3
    Membre régulier
    Homme Profil pro
    Architecte de base de données
    Inscrit en
    Février 2013
    Messages
    53
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 46
    Localisation : Norvège

    Informations professionnelles :
    Activité : Architecte de base de données
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Février 2013
    Messages : 53
    Points : 78
    Points
    78
    Par défaut
    Tout a fait raison la valeure de getidverb etait null

    J ai change le code comme suite
    J ai cree deux interface
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
     
    public interface OnDialogItemEditedListener {
     
    	public void OnItemEdited(int position);
     
    }
    Et
    public interface ItemEditor {
     
    	public void setItemEdited(int position);
     
    }
    Avec un Handling Fragment Communication qui implement OnDialogItemEditedListener

    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
     
     
    @Override
    	public void OnItemEdited(final int position) {
    		// Only inform the other fragments if the selected position is new
    				if (mCurImagePosition != position) {
     
    					Log.d(TAG, "onImageSelected: title = " + position + " position = " + position);
     
    					// Keep track of the selected image
    					//mCurImageResourceId = verbitem.getV_image();
     
     
    					mCurImagePosition = position;
     
    					// Get the fragment manager for this fragment's children
    					FragmentManager fragmentManager = getChildFragmentManager();
    					final ImageListFragment imageListFragment = (ImageListFragment) fragmentManager.findFragmentByTag(ImageListFragment.class.getName());
     
     
    					final ImagePagerFragment imagePagerFragment = (ImagePagerFragment) fragmentManager.findFragmentByTag(ImagePagerFragment.class.getName());
     
    					getActivity().runOnUiThread(new Runnable() {
    						public void run() {
     
    					// If the fragments are in the current layout, have them select the
    					// current image
    					if (imageListFragment != null) {
    						imageListFragment.setItemEdited( position);
    					}
    					if (imagePagerFragment != null) {
    						imagePagerFragment.setItemEdited( position);
    					}
     
    					// Notify the parent listener that an image was selected
    					if (mOnDialogItemEditedListener != null) {
    						mOnDialogItemEditedListener.OnItemEdited(position);
    					}
     
     
    						}
    						});
     
    				}		
    	}
    }
    Puis l interface ItemEditor dans les fragments parents


    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
     
    @Override
    	public void setItemEdited(int position) {
    		// TODO Auto-generated method stub
     
    		if (isResumed()) {
    			// If the selected position is valid, and different than what is
    			// currently selected, highlight that row in the list and
    			// scroll to it
    			if (position >= 0 && position < mImageArrayAdapter.getCount()) {
    				Log.d(TAG, "setImageSelected: title = " + position + " position = " + position);
     
     
    				 verbitemm =  mydata.getSide(Integer.valueOf(currentSide));
     
     
     
     
    		        	mImageArrayAdapter.setArrayList(verbitemm);
     
    					mListView.setAdapter(mImageArrayAdapter);
     
     
     
    				// Highlight the selected row and scroll to it
    				mListView.setItemChecked(position, true);
    				mListView.smoothScrollToPosition(position);
    				mImageArrayAdapter.getItem(position);
     
    			}
    		}
     
    	}

    puis le OnAttach() est valid pour tout fragments

    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
     
    @Override
    	public void onAttach(Activity activity) {
    		super.onAttach(activity);
    		Log.v(TAG, "onAttach");
     
    		// Check if parent fragment (if there is one) implements the image
    		// selection interface
    		Fragment parentFragment = getParentFragment();
     
    		if (parentFragment != null && parentFragment instanceof OnDialogItemEditedListener) {
    			mOnDialogItemEditedListener = (OnDialogItemEditedListener) parentFragment;
     
    		}
    		// Otherwise, check if parent activity implements the image
    		// selection interface
    		else if (activity != null && activity instanceof OnDialogItemEditedListener) {
    			mOnDialogItemEditedListener = (OnDialogItemEditedListener) activity;
    		}
    		// If neither implements the image selection callback, warn that
    		// selections are being missed
    		else if (mOnDialogItemEditedListener == null) {
    			Log.w(TAG, "onAttach: niether the parent fragment or parent activity implement mOnDialogItemEditedListener, "
    					+ "image selections will not be communicated to other components");
    		}
    }
    Puis OnClickListener dans le DialogFragment


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
     
    dialogBuilder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                                              @Override
                                                public void onClick(DialogInterface dialog, int which) {
     
     
    if (mOnDialogItemEditedListener	!= null) {
     
                             						//OnHeadlineSelectedListener	mCallback= 	(OnHeadlineSelectedListener) getActivity().getSupportFragmentManager();
                             						mOnDialogItemEditedListener.OnItemEdited(Integer.valueOf(getposverb));
     
                             				  }
     
    });

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

Discussions similaires

  1. Problem Listener JComboBox
    Par inconnnnnu dans le forum AWT/Swing
    Réponses: 2
    Dernier message: 26/10/2009, 08h12
  2. Oracle 10g ex - Probleme Listener
    Par Dexter_Morgan dans le forum Connexions aux bases de données
    Réponses: 1
    Dernier message: 10/02/2009, 11h09
  3. probleme listener oracle 10.2.0
    Par lestat02 dans le forum Connexions aux bases de données
    Réponses: 11
    Dernier message: 14/02/2008, 14h48
  4. Probleme Listener sur JSpinner ou dans class
    Par croc14 dans le forum AWT/Swing
    Réponses: 7
    Dernier message: 28/03/2007, 09h37
  5. Probleme listener 12518 / 12564
    Par genio dans le forum Connexions aux bases de données
    Réponses: 1
    Dernier message: 13/07/2006, 10h56

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