Reputation: 49
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.setNavigationItemSelectedListener(this);
}
Upvotes: 1
Views: 1010
Reputation: 8598
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.
Upvotes: 1
Reputation: 1533
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.
Upvotes: 0
Reputation: 1938
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");
}
Upvotes: 0