title: How to change the HeaderLayout background of Navigation Drawer programatically?
body: <p><strong>Question</strong></p>
  
  <p>Actually I want to change a HeaderLayout background of <code>Navigation Drawer</code> programmatically, </p>
  
  <p><strong>nav_header_main.xml:</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;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/side_nav_bar_fr"
  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"
  android:id="@+id/header"&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p><strong>activity_main.xml:</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;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"&gt;
  
  &lt;include layout="@layout/app_bar_main" android:layout_width="match_parent"
      android:layout_height="match_parent" /&gt;
  
  &lt;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" /&gt;
  
   &lt;/android.support.v4.widget.DrawerLayout&gt;
  </code></pre>
  
  <p>And I try to change header background with this code :</p>
  
  <pre><code> LinearLayout header;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
    header= (LinearLayout)findViewById(R.id.header);
  
    header.setBackgroundResource(R.drawable.side_nav_bar_ru);
  </code></pre>
  
  <p>but an error is displayed</p>
  
  <blockquote>
    <p>java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.LinearLayout.setBackgroundResource(int)' on a null object reference</p>
  </blockquote>
  
  <p>Someone help me please :(</p>