Android Studio error: "Method getText() must be called from the UI Thread, currently inferred thread is worker
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?
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
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));