Please tell me what the problem may be: the text in TextView is not replaced (checked, resource IDs do not overlap):

public void updateUserName(TextView textView) { String str = textView.getText().toString(); System.out.println("before_ " + str); textView.setText("my_name"); str = textView.getText().toString(); System.out.println("after_ " + str); } 

By outputting texts to the console, I found that the textView changes its value (i.e. getText returns "my_name"). However, no changes occur in the application itself. Perhaps the wrong resource id TextView I pass?

I get it in the LoginActivity in the onCreate method (src_name, src_email and src_avatar are declared at the beginning of the LoginActivity class as private TextView and private ViewImage):

 NavigationView navigationView = findViewById(R.id.navigation_view); // получаем ID ресурса src_name = navigationView.getHeaderView(0).findViewById(R.id.nav_drawer_header_username); // получаем ID имени src_email = navigationView.getHeaderView(0).findViewById(R.id.nav_drawer_header_useremail); // получаем ID почты src_avatar = navigationView.getHeaderView(0).findViewById(R.id.nav_drawer_header_useravatar); // получаем ID аватара 

Then I pass it to the user creation function (from there it’s already called the original updateUserName with the src_name, src_email and src_avatar arguments that are still passed).

  • Is a user accidentally created in another thread? - Kirill Vlasov
  • Excuse me, how can I find out? I can only say that in a separate class - odosenok
  • @odosenok and where is this method? - danilshik
  • The class update method belonging to UseProfile - odosenok is called from the LoginActivity class
  • one
    If this method goes to the database or to the network, then most likely it is executed in another thread. The code is usually visible. Alternatively, you can try textView.post (new Runnable () {textView.setText ("my_name");}) - Kirill Vlasov

1 answer 1

One of the reasons for this behavior is an attempt to change the interface from another thread. To work correctly with some elements and update the content, you can use the following code:

 public void UpdateUI() { try { ((Activity) this).runOnUiThread(new Runnable() { @Override public void run() { /// code here... } }); } catch (Exception ex) { Log.d("UI ERROR", ex.getMessage()); } } 

If the method is outside the activation class, pass the link to it as well. Below is the code of your function with changes.

 public void updateUserName(final TextView textView, Activity activity) { try { activity.runOnUiThread(new Runnable() { @Override public void run() { String str = textView.getText().toString(); System.out.println("before_ " + str); textView.setText("my_name"); str = textView.getText().toString(); System.out.println("after_ " + str); } }); } catch (Exception ex) { Log.d("UI ERROR", ex.getMessage()); } } 
  • Unfortunately, UIContext (Expression expected) is thus underlined. I tried to change to my activity, but to no avail. Or does it need to be inserted into the class where my TextView is stored? - odosenok
  • one
    UIContext is a symbol. If the updateUserName method is in the code of the active one that you update, just write this or LoginActivity.this; if the method is in another class, send another link to the activation text with the link to the textview. Above gave an example (corrected the answer) - Gennady Kurbesov