Home Ask Login Register

Developers Planet

Your answer is one click away!

Team KeeCode February 2016

Change TextView value on Activity creation

I want to change a TextViews value when my activity is created.

On the LoginActivity, I start my activity using:

startActivity(new Intent(LoginActivity.this, MainActivity.class));

But I dont know where to place the code for changing my textviews value.

When I place it in onCreate, my app just crashes...

my logcat is not working so I cant see my exceptions...

But it crashes when doing

    TextView t = (TextView) findViewById(R.id.zanuka_username);
    t.setText("lllll");

onCreate

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

        TextView t = (TextView) findViewById(R.id.zanuka_username);
        t.setText("lllll");

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

       /** FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
        **/

        //ImageView profilePicture = (ImageView)findViewById(R.id.zanuka_profilepicture);
        //new ImageLoadTask("http://chat.keecode.net/account/picture/1", profilePicture).execute();

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigation        

Answers


IgorB February 2016

TextView mTextView;
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);
        mTextView = (TextView)findViewById(R.id.your_text_view);
        mTextView.setText("Main activity created");
    }


v4_adi February 2016

Please post your MainActivity oncreate code and exception you are getting

This should work

TextView tv = (TextView)findViewById(R.id.tv);
tv.setText("Hello World");

in your onCreate.


Melquiades February 2016

You're getting this exception:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference

which tells you that your TextView t is NULL. The reason of this is a missing TextView with id="zanuka_username" in your activity_main.xml.

Post Status

Asked in February 2016
Viewed 1,597 times
Voted 10
Answered 3 times

Search




Leave an answer


Quote of the day: live life