簡體   English   中英

吐司內部片段不起作用

[英]Toast Inside Fragment Not working

我在該片段中有一個片段,當用戶輸入錯誤的登錄名和密碼時,我嘗試調用Toast,但是當按下登錄按鈕並且在Toast上方的日志正在調用時,Toast不可見,但仍然看不見Toast

這是我的片段

這是我的片段

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;
    }
}

日志記錄

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

我已經嘗試過

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();

幾乎所有我可以用來顯示烤面包的方法,最后我嘗試了runOnUiThread但仍然沒有顯示。

編輯1:添加了日志以顯示我正在從服務器“ FAIL”獲得響應

編輯2:我嘗試了所有得到的答案,但我的Toast仍然沒有顯示。那么,我的清單,布局是否有可能阻止Toast出現? 如果是,請讓我知道,以便我更新問題中的布局,樣式,清單

如果在onAttach(Activity)之前運行,則getActivity()將返回null。 相反,您應該直接在onPreExecute和onPostExecute方法中調用getActivity()或在onAttach中獲取對其的引用:

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

最后或在應用活動中添加此方法,然后在您希望其工作的地方調用

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

實際上,您的代碼對我來說很好。 我已經在doInBackground中對登錄失敗進行了硬編碼,因此它應該放在onPostExecute()登錄失敗塊中。 這是代碼

 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();

您應該在UI Thread上調用show Toast,所以就可以這樣嘗試:

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");

        }
    });

我建議您像這樣接受Asynctask通話

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");

            }


        }
    }

 }

您可以通過以下代碼實現Toast

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM