Reputation: 69
I have a navigation drawer in my android application. I have transferred content from an activity to the activity that has the navigation drawer. When I want to set the Textviews in the header to the values transferred, the application crushes. Below is the header the java code. Please help since I am new to android application development.
NavigationView navigationView=(NavigationView) findViewById(R.id.navbar);
navigationView.setNavigationItemSelectedListener(this);
//RECEIVING THE INTENT
//Get values from Intent
Intent intent = getIntent();
String myname = intent.getStringExtra("dname");
String myemail = intent.getStringExtra("demail");
String mycontact = intent.getStringExtra("dcontact");
name.setText(myname);
email.setText(myemail);
contact.setText(mycontact);
builder.setTitle("Log In Successful");
builder.setMessage("Welcome, "+myname+"/"+myemail+"/"+mycontact);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Toast.makeText(getApplicationContext(),"Clock Out Cancelled",Toast.LENGTH_SHORT).show();
}
});
AlertDialog alertDialog=builder.create();
alertDialog.show();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(actionBarDrawerToggle.onOptionsItemSelected(item)){
return true;
}
return super.onOptionsItemSelected(item);
}
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.cin){
// Handle the camera action
builder.setTitle("Log In Successful");
builder.setMessage("Welcome, ");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Toast.makeText(getApplicationContext(),"Clock Out Cancelled",Toast.LENGTH_SHORT).show();
}
});
AlertDialog alertDialog=builder.create();
alertDialog.show();
} else if (id == R.id.db) {
builder.setTitle("Log In Successful");
builder.setMessage("Welcome, ");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Toast.makeText(getApplicationContext(),"Clock Out Cancelled",Toast.LENGTH_SHORT).show();
}
});
AlertDialog alertDialog=builder.create();
alertDialog.show();
} else if (id == R.id.permit) {
builder.setTitle("Log In Successful");
builder.setMessage("Welcome, ");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Toast.makeText(getApplicationContext(),"Clock Out Cancelled",Toast.LENGTH_SHORT).show();
}
});
AlertDialog alertDialog=builder.create();
alertDialog.show();
} else if (id == R.id.cout) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
Upvotes: 1
Views: 4518
Reputation: 1827
Make sure you initialize all View
's properly before setting values. For example:
NavigationView navigationView = (NavigationView) findViewById(R.id.navbar);
View header = navigationView.getHeaderView(0);
TextView name = header.findViewById(R.id.name_text_view);
TextView email = header.findViewById(R.id.email_text_view);
TextView contact = header.findViewById(R.id.contact_text_view);
Then you can set your values.
name.setText(myname);
email.setText(myemail);
contact.setText(mycontact);
Upvotes: 5