当我将文本设置为nav_header_main
当我将文本设置为nav_header_main
中的文本视图时,我的应用程序崩溃了,而我无法将文本设置为textView
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
navigationView.setNavigationItemSelectedListener(this);
db = new SQLiteHandler(getApplicationContext());
// session manager
session = new SessionManager(getApplicationContext());
if (!session.isLoggedIn()) {
logoutStudent();
}
// Fetching Student details from SQLite
HashMap<String, String> student = db.getStudentDetails();
String name = student.get("name");
// String section = student.get("section");
TextView s_name = (TextView) findViewById(R.id.student_name);
s_name.setText("please work!");
这是布局
<TextView
android:id="@+id/student_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/nav_header_vertical_spacing"
android:text="@string/app_name"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:textColor="@color/bg_register" />
I expect the output to set the text, but the app is crashing.
答案 0 :(得分:-1)
NavigationView navView = findViewById(R.id.nav_view);
View headerView = navView.getHeaderView(0);
TextView hname= headerView.findViewById(R.id.headername);
hname.setText("Your Navigation Text here!");