2

J'ai un fragment dans ce fragment J'essaie d'appeler Toast lorsque l'utilisateur entre mal Identifiant et Mot de Passe mais Toast n'est pas visible lorsque le bouton de connexion est pressé et le log Toast appelle mais Toast n'est pas visibleToast Inside Fragment Ne fonctionne pas

This is my Fragment

Voici mon fragment

public class Login_Fragment extends Fragment { 

    EditText LoginUname,LoginPass; 
    ImageButton SignIn; 
    Context context; 
    public static final String TAG="Login Fragment"; 


    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View view= inflater.inflate(R.layout.login_fragment,container,false); 
     LoginUname= (EditText) view.findViewById(R.id.Login_Box); 
     LoginPass= (EditText) view.findViewById(R.id.Pass_Box); 
     SignIn= (ImageButton) view.findViewById(R.id.LoginButton); 

     SignIn.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       String Phone=LoginUname.getText().toString(); 
       String Password=LoginPass.getText().toString(); 

       new AsyncTask(){ 
        @Override 
        protected void onPostExecute(String result) { 
         super.onPostExecute(result); 
         Log.d("LOGIN FRAGMENT","Result: "+result); //GETTING RESULT FAIL HERE 

         if (result.equals("FAIL")){ 
          Log.d("LOGIN FRAGMENT","Result is FAIL"); //THIS LOG SHOWING IN LOGCAT BUT TOAST IS NOT VISIBLE 
          Toast.makeText(getActivity(), "Invalid Login And Password", Toast.LENGTH_LONG).show(); 

         } 
         else if (result.equals("SUCCESS")){ 
          Log.d("LOGIN FRAGMENT","Result is Success"); 

         } 

        } 
       }.execute(); 
       Log.d("LOGIN FRAGMENT","----LOGIN AND PASSWORD SENT"); 

      } 
     }); 
     Registration.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Registration_Fragment registrationFragment=new Registration_Fragment(); 
       FragmentManager fragmentManager=getFragmentManager(); 
       FragmentTransaction transaction=fragmentManager.beginTransaction(); 
       transaction.replace(R.id.FragmentLoginRegistration,registrationFragment); 
       transaction.commit(); 
      } 
     }); 

     return view; 
    } 

    @Override 
    public void onAttach(Context context) { 
     super.onAttach(context); 
     this.context=context; 
    } 
} 

Log

08-23 14:04:38.115 29053-29053/com.boysjoys.com.pro_working1 D/LOGIN FRAGMENT: Result: FAIL 
08-23 14:04:38.115 29053-29053/com.boysjoys.com.pro_working1 D/LOGIN FRAGMENT: Result is FAIL 

je l'ai déjà essayé

Toast.makeText(getActivity(), "Invalid Login And Password", Toast.LENGTH_LONG).show(); 


Toast.makeText(getActivity().getApplicationContext(), "Invalid Login And Password", Toast.LENGTH_LONG).show(); 


Toast.makeText(Login_Fragment.this.getActivity(), "Invalid Login And Password", Toast.LENGTH_LONG).show(); 

et presque toutes les méthodes que je peux utiliser pour afficher des toasts visible et enfin j'ai essayé runOnUiThread mais son toujours pas montrer.

EDIT 1: Ajouté Connectez-vous pour montrer que je reçois une réponse de Serveur- « FAIL »

Edit 2: J'ai essayé chaque réponse que je suis arrivé, mais mon Toast toujours pas montrer up.So est là Y a-t-il une chance que ma mise en page bloque Toast? Si oui, s'il vous plaît faites le moi savoir, je vais mettre à jour ma mise en page, Style, Manifeste dans ma question

+2

Comme je vois votre code, vous n'initialisez pas le bouton 'SignIn'' '. – Ironman

+1

ajoutez vos journaux aussi afin que nous puissions suivre le problème. –

+0

Est-ce que votre tâche asynchrone donne un résultat comme ÉCHEC ou SUCCÈS? – Stallion

Répondre

0

getActivity() retournera null s'il est exécuté avant onAttach (Activité). Au lieu de cela, vous devez appeler getActivity() directement dans vos méthodes OnPreExecute et onPostExecute ou obtenir une référence dans onAttach:

public void onAttach (Activity attachedActivity) { 
    activity = attachedActivity; 
} 
+0

Ne fonctionne pas essayé comme vous pouvez le voir Code mis à jour – androidXP

0

Ajouter cette méthode à la fin ou l'activité de l'application et appeler où vous voulez son travail

public void showToast(@NonNull String msg) { 
    Toast.makeText(getActivity(), msg, Toast.LENGTH_LONG).show(); 
} 
+0

Non. Ce n'est pas lié avec la question de l'OP – Piyush

-1

Vous pouvez obtenir Toast par code ci-dessous

Toast.makeText(view.getContext(), "Invalid Login Or Password", Toast.LENGTH_LONG).show(); 
0

en fait, votre code fonctionne bien pour moi. J'ai codé en dur le login Fail dans doInBackground de sorte qu'il devrait aller dans le bloc d'échec de connexion onPostExecute(). Voici le code

new AsyncTask(){ 
       @Override 
       protected Object doInBackground(Object[] params) { 
        return "FAIL"; // Hardcoded to make login fail 
       } 

       @Override 
      protected void onPostExecute(Object result) { 
       super.onPostExecute(result); 
       Log.d("LOGIN FRAGMENT","Result: "+result); 

       if (result.equals("FAIL")){ 
        Log.d("LOGIN FRAGMENT","Result is FAIL"); 
        getActivity().runOnUiThread(new Runnable() { 
         @Override 
         public void run() { 
          Log.d("LOGIN FRAGMENT","Result is FAIL 2"); 
          Toast.makeText(getActivity(), "Invalid Login Or Password toast", Toast.LENGTH_LONG).show(); 
         } 
        }); 

       } 
       else if (result.equals("SUCCESS")){ 
        Log.d("LOGIN FRAGMENT","Result is Success"); 

       } 

      } 
     }.execute(); 
+0

Comme vous pouvez voir la question mise à jour je poste aussi capture d'écran et aussi je poste logcat mais pour moi son ne fonctionne pas du tout je ne sais pas ce que je fais mal – androidXP

0

Vous devez appeler show Toast sur Thread UI, donc juste essayer comme ceci:

SignIn.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      String Phone=LoginUname.getText().toString(); 
      String Password=LoginPass.getText().toString(); 

      new AsyncTask(){ 
       @Override 
       protected void onPostExecute(String result) { 
        super.onPostExecute(result); 
        Log.d("LOGIN FRAGMENT","Result: "+result); //GETTING RESULT FAIL HERE 

        if (result.equals("FAIL")){ 
         Log.d("LOGIN FRAGMENT","Result is FAIL"); //THIS LOG SHOWING IN LOGCAT BUT TOAST IS NOT VISIBLE 
         new Handler(Looper.getMainLooper()).post(new Runnable() { 
          @Override 
          public void run() { 
           Toast.makeText(getActivity(), "Invalid Login And Password", Toast.LENGTH_LONG).show(); 
          } 
         }); 

        } 
        else if (result.equals("SUCCESS")){ 
         Log.d("LOGIN FRAGMENT","Result is Success"); 

        } 

       } 
      }.execute(); 
      Log.d("LOGIN FRAGMENT","----LOGIN AND PASSWORD SENT"); 

     } 
    }); 
+0

j'ai déjà essayé par 'runOnuithread' mais pas de chance et aussi par votre méthode – androidXP

+0

@androidXP J'ai testé cela dans ma démo locale, ça a marché. Donc, si vous pouvez afficher le code entier? – Cobain

+0

Bien sûr, je vais essayer à nouveau et revenir à vous bientôt – androidXP

0

Je vous recommande de prendre l'appel AsyncTask comme ça

public class Login_Fragment extends Fragment { 

EditText LoginUname,LoginPass; 
ImageButton SignIn; 
Context context; 
public static final String TAG="Login Fragment"; 


@Nullable 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View view= inflater.inflate(R.layout.login_fragment,container,false); 
    LoginUname= (EditText) view.findViewById(R.id.Login_Box); 
    LoginPass= (EditText) view.findViewById(R.id.Pass_Box); 
    SignIn= (ImageButton) view.findViewById(R.id.LoginButton); 
SignIn.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      String Phone=LoginUname.getText().toString(); 
      String Password=LoginPass.getText().toString(); 

       new Asynctask(Phone,Password).execute(); 

     } 
    }); 

    Registration.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Registration_Fragment registrationFragment=new Registration_Fragment(); 
      FragmentManager fragmentManager=getFragmentManager(); 
      FragmentTransaction transaction=fragmentManager.beginTransaction(); 
       transaction.replace(R.id.FragmentLoginRegistration,registrationFragment); 
      transaction.commit(); 
     } 
    }); 

    return view; 
} 

@Override 
public void onAttach(Context context) { 
    super.onAttach(context); 
    this.context=context; 
} 




private class Asynctask extends AsyncTask<String, Integer, String> { 


String password,phone; 

     public Asynctask(String phone, String password) { 
      password=password; 
      phone=phone; 

     } 
     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 

     } 

     @Override 
     protected String doInBackground(String... params) 
     { 

     // you need to take doInbackground to return result 
      return "SUCCESS"; 
     } 

     @Override 
     protected void onPostExecute(String result) { 
      super.onPostExecute(result); 

      Log.d("LOGIN FRAGMENT","Result: "+result); 

      if (result.equals("FAIL")){ 
       Log.d("LOGIN FRAGMENT","Result is FAIL"); 
       getActivity().runOnUiThread(new Runnable() { 
        @Override 
        public void run() { 
         Log.d("LOGIN FRAGMENT","Result is FAIL 2"); 
         Toast.makeText(getActivity(), "Invalid Login Or Password toast", Toast.LENGTH_LONG).show(); 
        } 
       }); 

      } 
      else if (result.equals("SUCCESS")){ 
       Log.d("LOGIN FRAGMENT","Result is Success"); 

      } 


     } 
    } 

} 
+0

Sa classe distincte mais j'appelle surPostexecute en fragment parce que je voulais chercher le résultat et quand le succès je reçois les détails de l'utilisateur, donc je dois l'utiliser comme ça – androidXP

+0

it'separte mais vous prenez dans le fragment .... au-dessus de l'onattacth ... –

+0

ce code est juste pour tester la réponse ci-dessous je l'enlève déjà désolé pour la confusion – androidXP