title: How to instantiate and add a view object programmatically
body: <p>I am trying to instantiate and add a view object programmatically</p>
  
  <pre><code>RelativeLayout activityMain = (RelativeLayout)findViewById(R.id.activity_main);
  View playControlsPanelMinimized = new View(this);
  activityMain.addView(playControlsPanelMinimized);
  </code></pre>
  
  <p>and I get this error in log cat </p>
  
  <pre><code>Attempt to invoke virtual method 
  'void android.widget.RelativeLayout.addView(android.view.View)' 
  on a null object reference
  at com.example.michael.musicplayer.PlayPanel.onBackPressed(PlayPanel.java:50)
  </code></pre>
  
  <p>This is <code>onBackPressed()</code> method and line # 50</p>
  
  <pre><code>@Override
  public void onBackPressed() {
      super.onBackPressed();
  
      // some code
      );
  
  RelativeLayout activityMain = (RelativeLayout)findViewById(R.id.activity_main);
  View playControlsPanelMinimized = new View(this);
  activityMain.addView(playControlsPanelMinimized); // Line # 50
  
  }
  </code></pre>
  
  <p>This is <code>activity_main.xml</code></p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:paddingLeft="0dp"
      android:paddingRight="0dp"
      android:paddingTop="0dp"
      android:paddingBottom="0dp"
      tools:context=".MainActivity"
      android:id="@+id/activity_main"&gt;
  
      &lt;ListView
          android:id="@+id/listView1"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:layout_alignParentBottom="true"
          android:layout_alignParentLeft="true"
          android:layout_alignParentStart="true"
          /&gt;
  
      &lt;RelativeLayout
          android:id="@+id/hidden_panel"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_below="@id/listView1"&gt;
  
          &lt;Button
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="@string/app_name" /&gt;
  
      &lt;/RelativeLayout&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>