title: Attempt to invoke virtual method 'void android.view.ViewGroup.setVisibility(int)' on a null object reference
body: <p>I tried to implement an expandable/collapsing <code>CardView</code> with a ToogleButton using <code>Animating Layout Changes</code> which allows the user to hide or show the Content inside the <code>CardView</code>. When I tried to run the app, Android Studio showed me this message:</p>
  
  <pre><code>FATAL EXCEPTION: main
                                                     Process: com.appmac.ron.expandcards, PID: 29977
                                                     java.lang.RuntimeException: Unable to start activity ComponentInfo{com.appmac.ron.expandcards/com.appmac.ron.expandcards.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.ViewGroup.setVisibility(int)' on a null object reference
                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
                                                         at android.app.ActivityThread.access$800(ActivityThread.java:144)
                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
                                                         at android.os.Handler.dispatchMessage(Handler.java:102)
                                                         at android.os.Looper.loop(Looper.java:135)
                                                         at android.app.ActivityThread.main(ActivityThread.java:5221)
                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                         at java.lang.reflect.Method.invoke(Method.java:372)
                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
                                                      Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.ViewGroup.setVisibility(int)' on a null object reference
                                                         at com.appmac.ron.expandcards.MainActivity.onCreate(MainActivity.java:36)
                                                         at android.app.Activity.performCreate(Activity.java:5933)
                                                         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
                                                         at android.app.ActivityThread.access$800(ActivityThread.java:144) 
                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
                                                         at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                         at android.os.Looper.loop(Looper.java:135) 
                                                         at android.app.ActivityThread.main(ActivityThread.java:5221) 
                                                         at java.lang.reflect.Method.invoke(Native Method) 
                                                         at java.lang.reflect.Method.invoke(Method.java:372) 
                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
  </code></pre>
  
  <p>MainActivity.java</p>
  
  <pre><code>package com.appmac.ron.expandcards;
  
  import android.content.Context;
  import android.os.Bundle;
  import android.support.design.widget.FloatingActionButton;
  import android.support.design.widget.Snackbar;
  import android.support.v7.app.AppCompatActivity;
  import android.support.v7.widget.Toolbar;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.ViewGroup;
  import android.widget.RelativeLayout;
  import android.widget.ToggleButton;
  
  public class MainActivity extends AppCompatActivity {
  
      private ViewGroup mContainerView;
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
          setSupportActionBar(toolbar);
  
          FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
          fab.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                          .setAction("Action", null).show();
              }
          });
  
  
          mContainerView = (ViewGroup) findViewById(R.id.container);
          mContainerView.setVisibility(View.INVISIBLE);
  
      }
  
      public void changeVisibility(View view){
  
          boolean checked = ((ToggleButton)view).isChecked();
          if(checked){
              mContainerView.setVisibility(View.VISIBLE);
          }
          else{
              mContainerView.setVisibility(View.INVISIBLE);
          }
  
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.menu_main, menu);
          return true;
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          // Handle action bar item clicks here. The action bar will
          // automatically handle clicks on the Home/Up button, so long
          // as you specify a parent activity in AndroidManifest.xml.
          int id = item.getItemId();
  
          //noinspection SimplifiableIfStatement
          if (id == R.id.action_settings) {
              return true;
          }
  
          return super.onOptionsItemSelected(item);
      }
  }
  </code></pre>
  
  <p>AndroidManifest.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.appmac.ron.expandcards"&gt;
  
      &lt;application
          android:allowBackup="true"
          android:icon="@mipmap/ic_launcher"
          android:label="@string/app_name"
          android:supportsRtl="true"
          android:theme="@style/AppTheme"&gt;
          &lt;activity
              android:name=".MainActivity"
              android:label="@string/app_name"
              android:theme="@style/AppTheme.NoActionBar"&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
  
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
  
  
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>
  
  <p>activity_main.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;android.support.design.widget.CoordinatorLayout 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:layout_width="match_parent"
      android:layout_height="match_parent"
      android:fitsSystemWindows="true"
      tools:context="com.appmac.ron.expandcards.MainActivity"&gt;
  
      &lt;android.support.design.widget.AppBarLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:theme="@style/AppTheme.AppBarOverlay"&gt;
  
          &lt;android.support.v7.widget.Toolbar
              android:id="@+id/toolbar"
              android:layout_width="match_parent"
              android:layout_height="?attr/actionBarSize"
              android:background="?attr/colorPrimary"
              app:popupTheme="@style/AppTheme.PopupOverlay" /&gt;
  
      &lt;/android.support.design.widget.AppBarLayout&gt;
  
      &lt;include layout="@layout/content_main" /&gt;
  
      &lt;android.support.design.widget.FloatingActionButton
          android:id="@+id/fab"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_gravity="bottom|end"
          android:layout_margin="@dimen/fab_margin"
          android:src="@android:drawable/ic_dialog_email" /&gt;
  
      &lt;RelativeLayout 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:layout_width="match_parent"
          android:layout_height="match_parent"
          android:paddingBottom="@dimen/activity_vertical_margin"
          android:paddingLeft="@dimen/activity_horizontal_margin"
          android:paddingRight="@dimen/activity_horizontal_margin"
          android:paddingTop="@dimen/activity_vertical_margin"
          app:layout_behavior="@string/appbar_scrolling_view_behavior"
          tools:context="com.appmac.ron.expandcards.MainActivity"
          tools:showIn="@layout/activity_main"&gt;
  
          &lt;android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
              android:id="@+id/cardFacebook"
              android:layout_width="fill_parent"
              android:layout_height="150dp"
              android:layout_marginLeft="10dp"
              android:layout_marginRight="10dp"
              android:layout_marginTop="10dp"
              android:background="?attr/selectableItemBackground"
              android:clickable="true"
              card_view:cardCornerRadius="0dp"&gt;
  
              &lt;RelativeLayout
                  android:layout_width="fill_parent"
                  android:layout_height="50dp"
                  android:id="@+id/relativeLayoutTop"&gt;
  
                  &lt;TextView
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:textAppearance="?android:attr/textAppearanceLarge"
                      android:text="TOP"
                      android:id="@+id/textViewTop" /&gt;
  
                  &lt;ToggleButton
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:textOff="Show More"
                      android:textOn="Hide"
                      android:id="@+id/toggleButton"
                      android:layout_centerVertical="true"
                      android:layout_alignParentRight="true"
                      android:layout_alignParentEnd="true"
                      android:backgroundTint="#fbfbfb"
                      android:textColor="#006199"
                      android:onClick="changeVisibility"/&gt;
  
              &lt;/RelativeLayout&gt;
  
  
              &lt;RelativeLayout
                  android:layout_width="fill_parent"
                  android:layout_height="100dp"
                  android:layout_alignParentLeft="true"
                  android:layout_alignParentStart="false"
                  android:layout_below="@+id/container"
                  android:layout_marginTop="50dp"
                  android:divider="?android:dividerHorizontal"
                  android:animateLayoutChanges="true"&gt;
  
                  &lt;TextView
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:textAppearance="?android:attr/textAppearanceLarge"
                      android:text="BOTTOM"
                      android:id="@+id/textViewBottom" /&gt;
              &lt;/RelativeLayout&gt;
  
  
          &lt;/android.support.v7.widget.CardView&gt;
  
  
  
      &lt;/RelativeLayout&gt;
  
  &lt;
  </code></pre>
  
  <p>/android.support.design.widget.CoordinatorLayout></p>
  
  <p>About the TranistionLayout:
  <a href="http://developer.android.com/intl/es/training/animation/layout.html" rel="nofollow">http://developer.android.com/intl/es/training/animation/layout.html</a></p>
  
  <p>Tankful for any help! </p>