TAGS :Viewed: 9 - Published at: a few seconds ago

[ Trying to change TextView String ]

I´ve been looking for answers, and have tried a lot of things, but nothing is working...

So, Im doing what I think is a simple app (I just started learning)

So I have an activity sending a couple of string to another where a couple of TextViews should chnage ther values to the new String, but this is not happening. Here you have the code:

public class LoginActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);


        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }

        Intent intent = getIntent();
        String mail = intent.getStringExtra(MainActivity.MAIL);
        String password = intent.getStringExtra(MainActivity.PASSWORD);

        setContentView(R.layout.fragment_login);
        TextView displayMailSetter = (TextView) findViewById(R.id.mailView);
        displayMailSetter.setText(mail);
        displayMailSetter.postInvalidate();

        TextView displayPasswordSetter = (TextView)findViewById(R.id.passwordView);
        displayPasswordSetter.setText(password);
        displayPasswordSetter.postInvalidate();

        displayMailSetter.postInvalidate();

        setContentView(R.layout.activity_login);
    }
}

Answer 1


Try taking out these lines of code:

displayMailSetter.postInvalidate();
// Don't take out your displayPasswordSetter initilization and setText

displayPasswordSetter.postInvalidate();

displayMailSetter.postInvalidate();

setContentView(R.layout.activity_login);

Change these lines of code:

        super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

to

        super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_login);

and take out the origional (right under the intents).

setContentView(R.layout.fragment_login);

The problem was that you were setting the layout to 3 different layouts. The first and third were the same layout, but didn't have the TextViews. The first and third are unnessecary, you should only set the layout once. Once you set it to fragment_login, the layout had the TextViews, set the text of them, but the layout was then changed to a layout without the TextViews.