Reputation:
I was trying to display user input and password in second activity but I got an error unfortunatly login has stopped... I am getting an error java.lang.nullpointerException : attempt to invoke virtual method'void android.widget.TextView.setText(java.lang.ChaeSequence)' on a null object reference.
Main class.
public class MainActivity extends Activity implements View.OnClickListener
{
EditText userName;
EditText password;
Button loginButton;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
userName = (EditText) findViewById(R.id.editText);
password = (EditText) findViewById(R.id.editText2);
loginButton = (Button) findViewById(R.id.button);
loginButton.setOnClickListener(this);
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();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings)
{
return true;
}
return super.onOptionsItemSelected(item);
}
public void onClick(View view)
{
String userStr = userName.getText().toString();
String passStr = password.getText().toString();
Intent intent = new Intent(MainActivity.this, SecondAcivity.class);
intent.putExtra("userName", userStr);
intent.putExtra("password", passStr);
startActivity(intent);
}
Second Class.
public class SecondAcivity extends MainActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second_acivity);
TextView textView = (TextView) findViewById(R.id.textView);
Intent intent = getIntent();
String userName = intent.getStringExtra("userName");
String password = intent.getStringExtra("password");
textView.setText("User Name: " + userName + " Password: " + password);
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();
}
});
}
Upvotes: 1
Views: 264
Reputation: 180
Bundle extras = getIntent().getExtras();
String userName,password;
if (getIntent().hasExtra("userName")) {
userName = extras.getString("userName");
password = extras.getString("password");
}
Upvotes: 0
Reputation: 9395
You can use this to get string from activity
String userName,password;
userName = getIntent().getExtras().getString("userName");
password = getIntent().getExtras().getString("passowrd");
Upvotes: 0
Reputation: 33438
You dont pull the strings from the instance of Intent
itself. You need to fetch the Bundle
of extras from the Intent and then get strings from the Bundle
:
Use this:
Intent intent = getIntent();
Bundle extras = intent.getExtras();
String userName = "";
String password = "";
if (extras != null) {
userName = extras.getString("userName");
password = extras.getString("password");
}
Upvotes: 2
Reputation: 2003
Your attempting to set text to null TextView
check if second activity layout file activity_second_acivity
contains textView
Upvotes: 0
Reputation: 246
kindly check in your R.layout.activity_second_acivity if the id textView exists. The texview variable will become null if the R.id.textView does not exist in your R.layout.activity_second_acivity.
Upvotes: 0
Reputation: 404
This will handle the NullPointerException when empty values are passed
Try :
Bundle extras = getIntent().getExtras();
String userName,password;
if (extras != null) {
userName = extras.getString("userName");
password = extras.getString("password");
}
Upvotes: 1