如何在导航抽屉标题中更改TextView的文本?

Sal*_*ari 68 java android textview

我想在导航抽屉标题内更改TextView的文本.但我得到这个错误:

java.lang.NullPointerException:尝试在空对象引用上调用虚方法'void android.widget.TextView.setText(java.lang.CharSequence)'


activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <include
            layout="@layout/app_bar_main"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <FrameLayout
            android:id="@+id/frame_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </LinearLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

</android.support.v4.widget.DrawerLayout>
Run Code Online (Sandbox Code Playgroud)

nav_header_main

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="@dimen/nav_header_height"
    android:background="@drawable/slide_nav_bar"
    android:gravity="bottom"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:theme="@style/ThemeOverlay.AppCompat.Dark">

    <ImageView
        android:id="@+id/tvHeaderIcon"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        android:src="@android:drawable/sym_def_app_icon" />

    <TextView
        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" />

    <TextView
        android:id="@+id/tvHeaderName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

MainActivity.java

TextView tvHeaderName;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    tvHeaderName= (TextView) findViewById(R.id.tvHeaderName);
    tvHeaderName.setText("Saly");    
}
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

Ber*_*eze 212

在您的navigationView上使用getHeaderView`

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
View headerView = navigationView.getHeaderView(0);
TextView navUsername = (TextView) headerView.findViewById(R.id.navUsername);
navUsername.setText("Your Text Here");
Run Code Online (Sandbox Code Playgroud)

  • 为了在导航抽屉标题中可靠地更新`TextView`,请在[`ActionBarDrawerToggle.onDrawerOpened()`](https://developer.android.com/reference/android/support/v4/app/ActionBarDrawerToggle中使用此代码。 html#onDrawerOpened(android.view.View))方法。请参阅:https://stackoverflow.com/a/35952939/1657502。 (2认同)

小智 11

这是代码的 Kotlin 版本,希望它可以帮助那里的人。

val navigationView : NavigationView  = findViewById(R.id.nav_view)
val headerView : View = navigationView.getHeaderView(0)
val navUsername : TextView = headerView.findViewById(R.id.txtUserName)
val navUserEmail : TextView = headerView.findViewById(R.id.txtEmail)

navUsername.text = username.toString()
navUserEmail.text = email.toString()
Run Code Online (Sandbox Code Playgroud)


小智 7

你应该首先从NavigationView中扩展你的Header布局......

navHeaderView= navigationView.inflateHeaderView(R.layout.nav_header_main);
tvHeaderName= (TextView) navHeaderView.findViewById(R.id.tvHeaderName);
tvHeaderName.setText("Saly"); 
Run Code Online (Sandbox Code Playgroud)

  • 此解决方案无法正常工作.这显示两次标题.使用getHeaderView(0)工作. (6认同)
  • 对我来说inflateHeaderView不起作用.我改用了getHeaderView(0). (3认同)

Ner*_*eri 6

您也可以在23.1.1设计支持库中使用它

View header = navigationView.getHeaderView(0)
TextView text = (TextView) header.findViewById(R.id.textView);
Run Code Online (Sandbox Code Playgroud)

参考:https://stackoverflow.com/a/33692431/2761923