久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何在導(dǎo)航抽屜標(biāo)題中更改 TextView 的文本?

How to change text of a TextView in navigation drawer header?(如何在導(dǎo)航抽屜標(biāo)題中更改 TextView 的文本?)
本文介紹了如何在導(dǎo)航抽屜標(biāo)題中更改 TextView 的文本?的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我想更改導(dǎo)航抽屜標(biāo)題內(nèi)的 TextView 的文本.但我得到這個(gè)錯(cuò)誤:

I want to change the text of a TextView inside the navigation drawer header. But I get this error:

java.lang.NullPointerException: 嘗試調(diào)用虛方法 'voidandroid.widget.TextView.setText(java.lang.CharSequence)' 為空對(duì)象引用

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

<小時(shí)>

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

我該怎么做?

推薦答案

在你的navigationView上使用getHeaderView函數(shù):

Use function getHeaderView on your navigationView:

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

這篇關(guān)于如何在導(dǎo)航抽屜標(biāo)題中更改 TextView 的文本?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周?chē)h(huán)繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動(dòng)生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數(shù)據(jù)庫(kù))
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對(duì)象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 久久精品99 | 欧美日韩电影免费观看 | 亚洲视频欧美视频 | 在线啊v | 亚洲国产精品一区二区久久 | 四虎永久免费地址 | 日韩av啪啪网站大全免费观看 | 中文成人在线 | 一区二区三区视频 | 亚洲激情网站 | 日本aa毛片a级毛片免费观看 | 特黄特色大片免费视频观看 | 中文字幕精品一区二区三区在线 | 久久草在线视频 | 国产亚洲精品综合一区 | 一级一片在线观看 | 国产午夜在线观看 | 日本人麻豆 | 91资源在线观看 | 欧美一级片在线观看 | 国产在线中文字幕 | 99久久免费精品视频 | 99精品免费久久久久久久久日本 | 欧美一区二区三区在线观看视频 | 激情a| www.蜜桃av| 日本手机在线 | 国产高清在线 | 国产精品久久国产精品 | 国产精品视频久久久 | 日韩视频免费看 | 日韩精品在线播放 | 欧美日韩在线高清 | 中文字幕一区在线观看视频 | 亚洲图片一区二区三区 | 尤物在线精品视频 | 免费毛片网 | 一道本不卡 | 亚洲精品中文字幕 | 国产目拍亚洲精品99久久精品 | 午夜天堂精品久久久久 |