2016-01-24 78 views
12

我想更改導航抽屜頁眉內部的TextView文本。但是,我得到這個錯誤:如何更改導航抽屜標題中的TextView文本?

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference


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> 

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> 

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");  
} 

我該怎麼做?

回答

68

使用getHeaderView`on您navigationView

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
View headerView = navigationView.getHeaderView(0); 
navUsername = (TextView) headerView.findViewById(R.id.navUsername); 
navUsernam.setText("Your Text Here"); 
6

您應該NavigationView第一擡高你的頭佈局...

navHeaderView= navigationView.inflateHeaderView(R.layout.nav_header_main); 
tvHeaderName= (TextView) navHeaderView.findViewById(R.id.tvHeaderName); 
tvHeaderName.setText("Saly"); 
+3

對我inflateHeaderView沒有工作。我用getHeaderView(0)代替。 – Zeezer

+5

此解決方案無法正常工作。這顯示標題兩次。使用getHeaderView(0)的作品。 – AlphaDeveloper

+1

@BereketGobeze有正確的答案,這不能正常工作 – touchofsarcasm

0

對我有用嗎?它這樣的: -

但要記住從XML文件即從NavigationView刪除此: - app:headerLayout="@layout/nav_header_main"


answer..over幾個月我現在想通有一個更好的方法上面好吧除了這如果你在android開發中足夠先進的話,就做這件事!!只需在那裏放置框架佈局並在其中加載我們有Recyclerview和標題部分的片段。使用Recyclerview您可以根據需要隨意設置標題,包括textviews,圖像等等。我正在處理的最近的應用程序就是這樣做的。動態地將數據從帶有圖標的fragmentactivity傳遞到Recyclerview,並在Recyclerview之上放置一個相對佈局(在textviews和imageview中),作爲標題。

0

使用該工具來獲取導航標題

View hview = navigationView.getHeaderView(0);

然後使用hview獲得導航標題的不同意見,並做你想做的

其他使用navHeaderView= navigationView.inflateHeaderView(R.layout.nav_header_main);這是創建一個新的標題在索引1處。

0

在本質上,我們必須通過在導航視圖中放大視圖變量來獲取導航視圖的實例,稍後可以將textView實例設置爲e dit textview