2015-12-29 3 views
0

Я хотел бы просто изменить текст в моем DrawerLayout Но все, что я пытаюсь достичь к NullPointerExceptionИзменение TextView в DrawerLayout

Может кто-нибудь мне помочь? я не могу добиться успеха, чтобы пойти корыто макеты ..

Activity_main.xml

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

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

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

Единственное, что мне удастся это получить NavigationView.

NavigationView nv = (NavigationView) findViewById(R.id.nav_view); 

Тогда я застрял здесь!

Мой nav_header_main.xml

<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/icon_top" 
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" android:orientation="vertical" 
android:gravity="bottom"> 

<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" 
    android:paddingTop="@dimen/nav_header_vertical_spacing" 
    android:src="@android:drawable/sym_def_app_icon" android:id="@+id/imageView" /> 

<TextView android:layout_width="match_parent" android:layout_height="wrap_content" 
    android:id="@+id/labelConnection" 
    android:paddingTop="@dimen/nav_header_vertical_spacing" android:text="Se connecter..." 
    android:textAppearance="@style/TextAppearance.AppCompat.Body1" 

    android:onClick="onClick" 
    android:clickable="true" /> 

Когда я пытаюсь получить nv.findViewById (R.id.labelConnection), у меня нулевой указатель исключение.

Почему?

Edit: Я нашел решение, чтобы надуть headerLayout но Я этот результат: https://gyazo.com/4450a3401a30d4000a3021a0a1cb6ea9

Это не достаточно хорошо .. Я не знаю, как удалить первый заголовок ...

ответ

0

Вы должны сделать это

View headerView = nv.getHeaderView(0); 
headerView.findViewById(R.id.labelConnection); 

удалить первый вид заголовка сделать:

nv.removeHeaderView(nv.getHeaderView(0)); 

, но убедитесь, что вы проверяете наличие дубликатов заголовков, иначе он будет генерировать indexOutOfBoundException.

+0

getHeaderView не может решить метод, как вы можете получить этот метод? –

+0

Возможно, вы также можете просто findViewById, потому что все те, которые принадлежат к активности, находятся в иерархии – Eenvincible

+0

На самом деле, я не нахожу, как я достигаю текстового представления через иерархию ... Из NavigationView я не знаю –

Смежные вопросы