title: NullPointerException When Switching between views
body: <p>Some users are getting this error.</p>
  
  <p>I use a viewgroup to continuously display a banner add.</p>
  
  <p>I then use child views for the program and switch back and forth between views as the user clicks a button.</p>
  
  <p>I can cause a crash by switching back and forth between child views 6 to 10 times.</p>
  
  <p>Here is the layout for the viewgroup:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  
  &lt;LinearLayout    
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:myapp="http://schemas.android.com/apk/res/com.company.programname"
      xmlns:app="http://schemas.android.com/apk/res/com.adwhirl"      
      android:orientation="vertical"      
      android:background="@color/darkslategrey"
      android:id="@+id/LinearLayout01"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"&gt;
      &lt;com.adwhirl.AdWhirlLayout          
          android:id="@+id/adwhirl_layout"
          android:gravity="center"          
          android:layout_width="fill_parent"          
          android:layout_height="52dp"
      /&gt;  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>Here is the code creating the ViewGroup:</p>
  
  <pre><code>   setContentView(R.layout.viewgrouplayout);
     llLinLay=(LinearLayout)findViewById(R.id.LinearLayout01);
     liInflater=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  </code></pre>
  
  <p>Here is the main child layout with irrelevent pieces cut out:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  
  &lt;RelativeLayout
      android:id="@+id/MainLayout"
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"&gt;
  
  
      &lt;Button
          android:id="@+id/MainShowAllDesiredButton"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_below="@id/AnotherButton"
          android:layout_toRightOf="@id/AButton"
          android:textSize="15sp"
          android:text="Show All Desired"
          android:onClick="MainDesired"
      /&gt;
  
  
      &lt;ListView
          android:id="@+id/ItemTypeList"
          android:background="@color/aqua"
          android:cacheColorHint="@color/aqua"
          android:fastScrollEnabled="true" 
          android:layout_width="fill_parent"
          android:layout_height="160dip"
          android:layout_below="@id/SomeItem"
      /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>Here is an example of the code creating a view:</p>
  
  <pre><code>   vAllDesired=liInflater.inflate(R.layout.alldesiredlayout,null);
     vAllDesired.setId(7);
     llLinLay.addView(vAllDesired);
     final ListView lvAllDesired = (ListView)findViewById(R.id.AllDesiredList);
     laAllDesiredAdapter = new AllDesiredListAdapter(this, alAllDesired);                    
     lvAllCoinsDesired.setAdapter(laAllDesiredAdapter);        
     ViewGroup.LayoutParams AllDesiredParams = lvAllCoinsDesired.getLayoutParams(); 
     AllDesiredParams.height = AllCoinsDesiredHeight; 
     lvAllCoinsDesired.setLayoutParams(AllDesiredParams); 
     FillAllDesiredArray();
  </code></pre>
  
  <p>Here is an example of the code used to switch between views:</p>
  
  <pre><code>    bReturnToMainScreen = (Button) findViewById(R.id.DesiredReturnToMainScreenButton);
      bReturnToMainScreen.setOnClickListener(new View.OnClickListener(){
         @Override
         public void onClick(View V) {
          llLinLay.removeView(vAllDesired);
          MainWindow();
         }
       });
  </code></pre>
  
  <p>Here is the trace from Android Developer website.  The line number in the program is not always the same.</p>
  
  <pre><code>java.lang.NullPointerException
   at android.webkit.WebView.requestFocus(WebView.java:6113)
   at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:1073)
   at android.view.ViewGroup.requestFocus(ViewGroup.java:1029)
   at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:1073)
   at android.view.ViewGroup.requestFocus(ViewGroup.java:1029)
   at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:1073)
   at android.view.ViewGroup.requestFocus(ViewGroup.java:1029)
   at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:1073)
   at android.view.ViewGroup.requestFocus(ViewGroup.java:1029)
   at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:1073)
   at android.view.ViewGroup.requestFocus(ViewGroup.java:1029)
   at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:1073)
   at android.view.ViewGroup.requestFocus(ViewGroup.java:1032)
   at android.view.View.requestFocus(View.java:3559)
   at android.view.ViewRoot.clearChildFocus(ViewRoot.java:1586)
   at android.view.ViewGroup.clearChildFocus(ViewGroup.java:508)
   at android.view.ViewGroup.clearChildFocus(ViewGroup.java:508)
   at android.view.ViewGroup.clearChildFocus(ViewGroup.java:508)
   at android.view.ViewGroup.clearChildFocus(ViewGroup.java:508)
   at android.view.ViewGroup.removeViewInternal(ViewGroup.java:2207)
   at android.view.ViewGroup.removeViewInternal(ViewGroup.java:2181)
   at android.view.ViewGroup.removeView(ViewGroup.java:2129)
   at com.jimbobga.mycoinsus.ProgramName$8.onClick(ProgramName.java:488)
   at android.view.View.performClick(View.java:2411)
   at android.view.View$PerformClick.run(View.java:8819)
   at android.os.Handler.handleCallback(Handler.java:587)
   at android.os.Handler.dispatchMessage(Handler.java:92)
   at android.os.Looper.loop(Looper.java:123)
   at android.app.ActivityThread.main(ActivityThread.java:4627)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:521)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
   at dalvik.system.NativeStart.main(Native Method)
  </code></pre>