[英]Set Text in navigation drawer header
I have a navigation drawer in my android application. 我的Android应用程序中有一个导航抽屉。 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. 当我想将标题中的Textviews设置为传输的值时,应用程序会崩溃。 Below is the header the java code. 下面是java代码的标题。 Please help since I am new to android application development. 请帮助,因为我是Android应用程序开发的新手。
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;
}
} }
Make sure you initialize all View
's properly before setting values. 确保在设置值之前正确初始化所有View
。 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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.