TextView from a NavigationView header returning null

I am using NavigationView, which has a header layout, this header layout contains the text view that I am trying to get in my Activity, and it returns null.

I tried to spend a lot of time on this, there is no hope.

My activity

public class MainActivity extends AppCompatActivity {

    private DrawerLayout mDrawerLayout;
    RelativeLayout headerLL;
    ImageView userIV;
    TextView usernameTV;

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

        headerLL = (RelativeLayout) findViewById(R.id.navHeaderLL);
        userIV = (ImageView) findViewById(R.id.userDPIV);
        usernameTV = (TextView) findViewById(R.id.usernameTV);
        //getting error here
        usernameTV.setText("Ashiq");



        initializeDrawer();

    }
}

activity_mail.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_height="match_parent"
        android:layout_width="match_parent"
        android:fitsSystemWindows="true"
        tools:context=".MainActivity">

        <include layout="@layout/include_list_viewpager"/>

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

    </android.support.v4.widget.DrawerLayout>

nav_header.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="192dp"
        xmlns:tools="http://schemas.android.com/tools"
        android:background="?attr/colorPrimaryDark"
        android:padding="16dp"
        android:id="@+id/navHeaderLL"
        android:theme="@style/ThemeOverlay.AppCompat.Dark">

        <ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:id="@+id/userDPIV"
            android:elevation="4dp"
            android:layout_above="@+id/usernameTV"
            android:layout_marginBottom="10dp"
            tools:src="@drawable/ic_menu"
            />

        <TextView
            android:id="@+id/usernameTV"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            style="@style/TextViewAppearance.UserName"
            tools:text="Username"
        android:layout_alignParentBottom="true"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1"/>

</RelativeLayout>
+4
source share
2 answers

After initializing the NavigationView, you will catch the header puffed up with navigationView.getHeaderView (0) and use findViewById to get the view

navigationView = (NavigationView) findViewById(R.id.nav_view);
(TextView) navigationView.getHeaderView(0).findViewById(R.id.viewName);
+23
source

Do it.

  • You must initialize NavigationView (following from setcontentview)

2. " "

 public class MainActivity extends AppCompatActivity {
    private DrawerLayout mDrawerLayout;
    RelativeLayout headerLL;
    ImageView userIV;
    TextView usernameTV;

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

        // 1
        initializeDrawer(); 

        //2

        headerLL = (RelativeLayout) findViewById(R.id.navHeaderLL);
        userIV = (ImageView) findViewById(R.id.userDPIV);
        usernameTV = (TextView) findViewById(R.id.usernameTV);
        //getting error here
        usernameTV.setText("Ashiq");


    }
}

, .

:)

+1

Source: https://habr.com/ru/post/1614576/


All Articles