Click here to Skip to main content
15,879,490 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I want to put logged username in text view in navigationview header in MainActivity but the app crachs i did in layout and it works but in the header of navigationview it crachs how to solve the problem?

What I have tried:

I tried this code:


LoginActivity:
//Login button 
public void openMainActivity() {
		Intent intent = new Intent(this, MainActivity.class);
		 intent.putExtra("username", txt_username.getText().toString());
		startActivity(intent);
		txt_password.setText(null);
		txt_username.setText(null);
	}



MainActivity :
// Hello user textview
    txt_welcomeUser = (TextView) findViewById(R.id.txt_welcomeuser);
    Bundle extras = getIntent().getExtras();
    String username = null;
    if(extras != null){
        username = extras.getString("username");
        txt_welcomeUser.setText("WELCOME: " + username);
    }
Posted
Updated 21-May-23 15:34pm
v3
Comments
Richard MacCutchan 12-May-23 4:16am    
The same code in both parts of your question but no details of what actually happens in the main activity. You need to use the debugger to see what results you are getting in both sections of code.
brahim farhat (AAD) 16-May-23 20:02pm    
What happend the App crachs
brahim farhat (AAD) 16-May-23 20:03pm    
It works in layout but in header of navigationview it crachs
brahim farhat (AAD) 16-May-23 20:08pm    
I edited the question
Richard MacCutchan 17-May-23 4:02am    
That is the same code as in the original question, and we still have no idea what happens when you riun it. As I suggested four days ago, you need to use the debugger to see what is going on. At a wild guess it is possible that txt_username has never been initialised.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900