Skip to content

Android Studio error: "Method getText() must be called from the UI Thread, currently inferred thread is worker #47

Description

@8150133

Hi. I'm doing a Android Studio Project, with some AsyncTasks, and at some point it gives me the error at the Title. I know that i really can't use method getText it in doInBackground, but i've tried to do the recommended changes from another questions, to the code and nothing seems to work. This is my code:

`public class LoginActivity extends AppCompatActivity {

Button btSignIn;
Button btSignUp;
EditText edtEmail;
EditText edtPassword;
String CHANNEL_ID = "personal_notification";
int NOTIFICATION_ID = 001;

UserDatabase database;
UserDao userDao;
User userTemp = new User();

SharedPreferences emailSharedPref;

ProgressDialog progressDialog, progressDialogDownload;

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

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    Log.d("MYTAG", "inicio ");
    progressDialog = new ProgressDialog(this);
    progressDialog.setCancelable(false);
    progressDialog.setMessage("A verificar utilizador...");
    progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    progressDialog.setProgress(0);


    database = Room.databaseBuilder(getApplicationContext(), UserDatabase.class, "mi-database.db").build();


    userDao = database.getUserDao();


    btSignIn = findViewById(R.id.btSignIn);
    btSignUp = findViewById(R.id.btSignUp);

    edtEmail = findViewById(R.id.email);
    edtPassword = findViewById(R.id.password);


    btSignUp.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(LoginActivity.this, SignUpActivity.class));
        }
    });
    btSignIn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            LoadAsyncTaskLogin loadAsyncTaskLogin = new LoadAsyncTaskLogin();
            loadAsyncTaskLogin.execute(userTemp);
        }
    });

}

public class LoadAsyncTaskLogin extends AsyncTask<User, Void, User> {

    @Override
    protected User doInBackground(User... users) {
        String email = edtEmail.getText().toString();
        String password = edtPassword.getText().toString();

        User user2 = new User();
        user2 = database.getUserDao().getUser(email,password);

        return user2;
    }

    @Override
    protected void onPostExecute(User user2){
        Intent intentToWhatToChoose = new Intent(LoginActivity.this,WhatToChoose.class);

        if(user2 != null){
            if(user2.password.equals(edtPassword.getText().toString())){
            emailSharedPref = getApplicationContext().getSharedPreferences("email",0);
            SharedPreferences.Editor editor = emailSharedPref.edit();
            editor.putString("User Successfully Logged In",user2.email);
            editor.commit();

            startActivity(intentToWhatToChoose);
                Toast.makeText(LoginActivity.this, "Welcome to Our App", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(LoginActivity.this, "Incorrect Credentials", Toast.LENGTH_LONG).show();
            }
        } else {
            Toast.makeText(LoginActivity.this, "Incorrect Credentials", Toast.LENGTH_LONG).show();
        }
    }
}`

The error is on "doInBackground". What changes do i have to do?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions