-2

I know about this and it works fine

    Intent i1 = new Intent(Login.this, Welcome.class);
    i1.putExtra("username","the name");
    startActivity(i1);

and this in the next layout:

    String username = getIntent().getStringExtra("username");

but i need to tranfer a final string, I tried this but it doesnt work:

    public static final String ADMIN_USERNAME= "user";

    Intent intent = new Intent(Login.this,SignUp.class);
    intent.putExtra("admin_username",ADMIN_USERNAME);
    startActivity(intent);

and this in the next layout:

    public final String ADMIN_USERNAME= getIntent().getStringExtra("admin_username");

im getting this error:

    Attempt to invoke virtual method 'java.lang.String android.content.Intent.getStringExtra(java.lang.String)' on a null object reference

2 Answers2

1

When you declare a string constant as static like this:

public static final String ADMIN_USERNAME= "user";

you don't need to use intent extras to access to the value in another activity, just access the ADMIN_USERNAME string constant directly:

if (someString.equals(MyActivity.ADMIN_USERNAME)) {
    //Do something....
}
Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137
Carmen
  • 181
  • 7
0

Your intent is null. İt's a early call in lifecycle

String username = getIntent().getStringExtra("admin_username");

Make sure you call this line in onCreate if you are calling inside onAttach

Also make sure your key is correct. Please notice I fixed it also as "admin_username"

Good luck

Emre

Emre Aktürk
  • 3,306
  • 2
  • 18
  • 30