-1

This is my main activity and im passing a string strname

Intent i = new Intent(getApplicationContext(),newpage.class);
            i.putExtra("firstname", strname);
            startActivity(i);

this is the new page where it will show this activity

TextView uname1 = (TextView) findViewById(R.id.textView15);
        uname1.setText(getIntent().getExtras().getString("firstname"));

my application closes when i press submit..

Ahsan Ajmal
  • 115
  • 7

4 Answers4

0

NullPointerException happens when you attempt to invoke a method in a null reference. For example getIntent().getExtras() in case getIntent() is null --> null.getExtras(), this will invoke a NPE.

Check if the intent is non-null before using it to avoid NullPointerException.

Intent intent = getIntent();
String firstName = "";
if (intent != null) {
    firstName = intent.getStringExtra("firstname");
}
uname1.setText(firstName);
Soon Santos
  • 2,107
  • 22
  • 43
0

Check out the answer of this post: How do I pass data between Activities in Android application?

And try not to use ApplicationContext, but instead use BaseContext (see: difference and when to use getApplication(), getApplicationContext(), getBaseContext() and someClass.this)

Javatar
  • 2,518
  • 1
  • 31
  • 43
0

NullPointerException happens when you attempt to invoke a method in a null reference.

getString like as

TextView uname1 = (TextView) findViewById(R.id.textView15);
if (intent.getStringExtra("firstname")!=null) {
uname1.setText(getIntent().getStringExtras("firstname"));
}
0

Try:

Activity1:

Intent myIntent = new Intent(Activity1.this, Activity2.class); Bundle b = new Bundle(); b.putString("firstname", strname); myIntent.putExtras(b);

startActivity(myIntent);

Activity2:

Bundle b = getIntent().getExtras(); String value = b.getString("key"); uname1.setText(value);

alexsgi
  • 150
  • 9