Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
8150133 opened this issue Sep 11, 2019 · 2 comments

Comments

@8150133
Copy link

8150133 commented Sep 11, 2019

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?

@santoshsali
Copy link

santoshsali commented Jan 28, 2022

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

@Override
protected User doInBackground(Strings... params) {
    String email = params[0];                        //edtEmail.getText().toString();
    String password = params[1];                //edtPassword.getText().toString();

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

    return user2;
}

Dont try to access edtEmail.getText().toString() in doInBackground()
doInBackground() this method is execute on worker thread not on main thread so we cannot access view inside this

@ChinmaySahu1357
Copy link

String username = uname.getText().toString();
String pass = password.getText().toString();

        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("u",username));
        params.add(new BasicNameValuePair("p",pass));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants