Hiba Habhouba
Hiba Habhouba

Reputation: 3

Method getText() must be call from UI thread

I can't resolve this error. Please help me. I have this two methods:

public boolean isEmailValid(String email) {

    boolean flag;

    CharSequence inputStr = email.trim();
    Pattern pattern = Pattern.compile(EXPRESSION,
            Pattern.CASE_INSENSITIVE);
    Matcher matcher = pattern.matcher(inputStr);

    if (matcher.matches())
        flag = true;
    else {

        flag = false;
    }
    return flag;

}

public boolean isFieldsEmpty(String login, String mdp) {
    Boolean result = true;
    if (login.equals("") || mdp.equals("")) {
        result = true;
    } else {
        result = false;
    }

    return result;
}

class LoginUser extends AsyncTask<String, String, String> {

        @Override
        protected void onPreExecute(){

            super.onPreExecute();
            progressBar.setVisibility(View.VISIBLE);
            prefs = getSharedPreferences("PFE_Prefs",MODE_PRIVATE);
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            progressBar.setVisibility(View.GONE);
            switch (s) {
                case "Champs_vides":
                    Toast.makeText(getApplicationContext(), "Merci de bien vouloir remplir tous les champs, svp", Toast.LENGTH_SHORT).show();
                    EmailEditText.setText("");
                    MdpEditText.setText("");
                    break;
                case "Email_incorrect":
                    Toast.makeText(getApplicationContext(),"Merci de bien vouloir verifier votre adresse email, svp", Toast.LENGTH_SHORT ).show();
                    EmailEditText.setText("");
                    MdpEditText.setText("");
                    break;
                case "Success": //Appel a l'activité Tableau de bord
                    Intent toDashBord = new Intent(getApplicationContext(), DashBoardActivity.class);
                    startActivity(toDashBord);
                    LoginActivity.this.finish();
                    break;
                case "Echoué" :
                    Toast.makeText(getApplicationContext(),"Merci de bien vouloir verifier votre connexion, svp", Toast.LENGTH_SHORT ).show();
                    EmailEditText.setText("");
                    MdpEditText.setText("");
                    break;
            }

        }

And in the method doInBackground:

protected String doInBackground(String... params) {

        int success = 0;
        String result = "";
        JSONObject json ;
        WebCalls webCalls = new WebCalls();
        JSONObject jsonUser;
        User tempUser ;

        if (isFieldsEmpty(EmailEditText.getText().toString(),MdpEditText.getText().toString())){
            result = "Empty_Field";
            return  result ;
        }else if (!isEmailValid(EmailEditText.getText().toString().trim())){
            result = "Wrong_mail";
            return  result ;
        }
        return null;
    }

My problem is in EmailEditText.getText().toString(), I'm getting the following error:

Method getText() must called from UI thread, currently inferred thread is worker

Upvotes: 0

Views: 311

Answers (4)

orkundzgn
orkundzgn

Reputation: 52

Call all your getText() functions in onCreate or somewhere before AsyncTask and assign the return values to the public values. So you can reach them.

public String MyeMail;
public String MyMDP;
onCreate()...
{
    MyeMail= EmailEditText.getText().toString();
    MyMDP = MdpEditText.getText().toString()
    .
    .
    .
}

It doesn't have to be in onCreate but it must be after the user entered the EditText fields. You can do these after a button click maybe. Anyways, now in your AsyncTask's doInBackground:

doInBackground...
{
    if (isFieldsEmpty(MyeMail,)) {
        result = "Empty_Field";
        return  result ;
    .
    .
    .
}

So hope it helps! Let me know if it works.

Upvotes: 1

Aman Tugnawat
Aman Tugnawat

Reputation: 92

Your error lies in the method doInBackground()

You are calling 2 objects EmailEditText and MdpEditText which are part of the UI Thread inside another thread. And hence you are being asked to call "EmailEditText.getText().toString()" from UI thread.

What you are trying to do is'nt exactly clear form the snippets you have posted.

But this example might help: In your OnCreate() have something like this

protected void onCreate(Bundle savedInstanceState) {
...
String parameters= new String[2];//make this global if you aren't calling your AsyncTask in onCreate()
parameters[1]=EmailEditText.getText().toString();
parameters[2]=MdpEditText.getText().toString();
...
}

and call your AsyncTask like this:

new YourTask().execute(parameters);

while declaration for your AsyncTask would be like this:

private class YourTask extends AsyncTask<String, Void, String> {

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

        String email = params[0];
        String mdp= params[1];

        int success = 0;
        String result = "";
        JSONObject json ;
        WebCalls webCalls = new WebCalls();
        JSONObject jsonUser;
        User tempUser ;

        if (isFieldsEmpty(email,mdp)){
            result = "Empty_Field";
            return  result ;
        }else if (!isEmailValid(email.trim())){
            result = "Wrong_mail";
            return  result ;
        }

        return null;
    }
    ...
}

Hope this helps!!

Upvotes: 0

Shadab Ansari
Shadab Ansari

Reputation: 7070

AsyncTask's doInBackground() always runs in a non-UI worker thread which means that you cannot access UI elements like EditText,TextView in doInBackground(). SO, if you want to access these in doInBackground(), then you can either pass those in your AsyncTask's constructor and use them or the best approach would be to pass EmailEditText.getText().toString() and MdpEditText.getText().toString() as params in the execute() method of AsyncTask. So, let's say if your AsyncTask name is "ProcessTask", then while you start your AsyncTask, write this :

new ProcessTask().execute(EmailEditText.getText().toString(), MdpEditText.getText().toString());

In this case your AsyncTask should change to look like :

class ProcessTask extends AsyncTask<String,Void,Void >{
      @Override
      protected File doInBackground(String... params) {
        String emailEditText = params[0];
        String mdpEditText = params[1];

        int success = 0;
        String result = "";
        JSONObject json ;
        WebCalls webCalls = new WebCalls();
        JSONObject jsonUser;
        User tempUser ;

        if (isFieldsEmpty(emailEditText,mdpEditText)){
            result = "Empty_Field";
            return  result ;
        }else if (!isEmailValid(emailEditText.trim())){
            result = "Wrong_mail";
            return  result ;
        }



        return null;
}

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1006734

getText() must be called on the main application thread. doInBackground() is being called on a background thread.

Get the values out of the EditText and provide them to the AsyncTask (e.g., via a constructor) before executing the task.

Upvotes: 1

Related Questions