2013-05-09 8 views
0

J'ai créé une classe AlertDialogFragment et j'essaie de l'afficher à partir d'une autre classe avec le code suivant, mais je continue d'obtenir une erreur pour changer le type de FragmentTranscation en FragmentManager. Si je change à FragmentManager, je reçois un message pour passer à FragmentTranscation, chaque fois que je change de FragmentTranscation, je reçois un message pour changer FragmentManager:La boîte de dialogue ne s'affiche pas

Voici le code pour montrer la AlertDialog:

FragmentTransaction ft= getFragmentManager().beginTransaction(); 
AlertDialogFragment newFragment= new AlertDialogFragment(); 
newFragment.show(ft, "alertDialog"); 

Voici le code de la classe:

public class AlertDialogFragment extends android.support.v4.app.DialogFragment { 
@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 
    AlertDialog.Builder builder 
    = new AlertDialog.Builder(getActivity()); 
    builder.setMessage("Staying in Touch With The Ones You Love"); 
    builder.setTitle("Togetherness"); 
    builder.setCancelable(false); 
    builder.setPositiveButton("yes", new DialogInterface.OnClickListener() { 

     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      // TODO Auto-generated method stub 

     } 
    }); 
    builder.setNegativeButton("No", new DialogInterface.OnClickListener() { 

     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      dialog.cancel(); 

     } 
    }); 
    return builder.create(); 
} 
} 
+0

check ce tutoriel -> http://vinaygopinath.wordpress.com/2012/11/10/alertdialog-using-fragment/ –

Répondre

0

Pour afficher un fragment, vous devez soit remplacer un fragment existant, soit en ajouter un nouveau à une vue existante. Editer: Désolé, je n'ai pas remarqué qu'il s'agissait d'un fragment de dialogue. Utilisez ceci:

// DialogFragment.show() will take care of adding the fragment 
// in a transaction. We also want to remove any currently showing 
// dialog, so make our own transaction and take care of that here. 
FragmentTransaction ft = getFragmentManager().beginTransaction(); 
Fragment prev = getFragmentManager().findFragmentByTag("alertDialog"); 
if (prev != null) { 
    ft.remove(prev); 
} 
ft.addToBackStack(null); 

// Create and show the dialog. 
newFragment.show(ft, "alertDialog"); 

Et jeter un oeil à des exemples ici: http://developer.android.com/reference/android/app/DialogFragment.html

Rappelez-vous que des fragments ont été introduits dans le niveau de l'API 11. Si vous utilisez un niveau d'API plus, suivez les instructions ici pour utiliser la bibliothèque de soutien pour tous vos trucs de fragments (je vois votre DialogFragment hérite déjà de la bibliothèque de soutien FragmentDialog)

http://developer.android.com/training/basics/fragments/support-lib.html

+0

Merci beaucoup. Cela a fonctionné parfaitement !!!! – user2348040

+0

C'était exactement ce que je cherchais. – user2348040

0

Essayez utilisation

FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 
+0

Merci beaucoup. Cela a fonctionné parfaitement !!!! – user2348040

+0

Veuillez marquer ceci comme la réponse précise :) – Neoh

Questions connexes