title: How do I change layout view on the application when i have multiple separate layouts(screens)?
body: <p>xml mainactivity screen 1</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;RelativeLayout
     xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
     xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
     xmlns:tools=&quot;http://schemas.android.com/tools&quot;
     android:layout_width=&quot;match_parent&quot;
     android:layout_height=&quot;match_parent&quot;
     tools:context=&quot;.MainActivity&quot;
     android:background=&quot;#00008b&quot;
     android:id=&quot;@+id/page&quot;
  
  
     &gt;
  
  
     &lt;TextView
         android:id=&quot;@+id/game&quot;
         android:layout_width=&quot;wrap_content&quot;
         android:layout_height=&quot;wrap_content&quot;
         android:text=&quot;The Random Game&quot;
         android:textStyle=&quot;bold|italic&quot;
         android:layout_centerInParent=&quot;true&quot;
         android:layout_alignParentTop=&quot;true&quot;
         app:layout_constraintEnd_toEndOf=&quot;parent&quot;
         app:layout_constraintStart_toStartOf=&quot;parent&quot;
         android:textColor=&quot;#006400&quot;
         android:outlineSpotShadowColor=&quot;#2196F3&quot;
         app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
     &lt;Button
         android:id=&quot;@+id/buttonstart&quot;
         android:text=&quot;Start&quot;
         android:textStyle=&quot;bold|italic&quot;
         android:layout_width=&quot;wrap_content&quot;
         android:layout_height=&quot;wrap_content&quot;
         android:layout_below=&quot;@id/game&quot;
         android:layout_centerInParent=&quot;true&quot;
         android:textColor=&quot;#006400&quot;
         android:background=&quot;#8b0000&quot;
  
         /&gt;
     &lt;TextView
         android:id=&quot;@+id/TVshowmessage&quot;
         android:text=&quot;the number is&quot;
         android:layout_width=&quot;wrap_content&quot;
         android:layout_height=&quot;wrap_content&quot;
         android:layout_below=&quot;@id/buttonstart&quot;
         android:layout_centerInParent=&quot;true&quot;
         android:textColor=&quot;#006400&quot;
  
  
         /&gt;
     &lt;Button
         android:id=&quot;@+id/buttonplus&quot;
         android:text=&quot;+&quot;
         android:textStyle=&quot;bold|italic&quot;
         android:textSize=&quot;@android:dimen/app_icon_size&quot;
         android:layout_width=&quot;wrap_content&quot;
         android:layout_height=&quot;wrap_content&quot;
         android:layout_below=&quot;@id/TVshowmessage&quot;
         android:layout_centerInParent=&quot;true&quot;
         android:textColor=&quot;#006400&quot;
         android:background=&quot;#8b0000&quot;
         /&gt;
     &lt;TextView
         android:id=&quot;@+id/TVcurrentnum&quot;
         android:text=&quot;0&quot;
         android:layout_width=&quot;wrap_content&quot;
         android:layout_height=&quot;wrap_content&quot;
         android:layout_below=&quot;@id/buttonplus&quot;
         android:layout_centerInParent=&quot;true&quot;
         android:textColor=&quot;#006400&quot;
         android:background=&quot;#8b0000&quot;
  
         /&gt;
     &lt;Button
         android:id=&quot;@+id/buttonminus&quot;
         android:text=&quot;-&quot;
         android:textSize=&quot;@android:dimen/app_icon_size&quot;
         android:layout_width=&quot;wrap_content&quot;
         android:layout_height=&quot;wrap_content&quot;
         android:layout_below=&quot;@id/TVcurrentnum&quot;
         android:layout_centerInParent=&quot;true&quot;
         android:textColor=&quot;#006400&quot;
         android:background=&quot;#8b0000&quot;
  
         /&gt;
  
     &lt;TextView
         android:id=&quot;@+id/tvcounter&quot;
         android:text=&quot;counter&quot;
         android:layout_width=&quot;wrap_content&quot;
         android:layout_height=&quot;66dp&quot;
         android:layout_centerInParent=&quot;true&quot;
         android:layout_marginBottom=&quot;300dp&quot;
         android:background=&quot;#8b0000&quot;
         android:textColor=&quot;#006400&quot;
         android:textStyle=&quot;bold|italic&quot;
         android:textAlignment=&quot;center&quot;
         android:layout_below=&quot;@id/buttonrestart&quot;
         /&gt;
  
     &lt;Button
         android:id=&quot;@+id/buttoncheck&quot;
         android:layout_width=&quot;wrap_content&quot;
         android:layout_height=&quot;wrap_content&quot;
         android:layout_centerInParent=&quot;true&quot;
         android:layout_centerHorizontal=&quot;true&quot;
         android:textColor=&quot;#006400&quot;
         android:background=&quot;#8b0000&quot;
         android:textStyle=&quot;bold|italic&quot;
         android:outlineSpotShadowColor=&quot;#008B02&quot;
         android:text=&quot;Check&quot;
         android:layout_below=&quot;@id/buttonminus&quot;
         /&gt;
     &lt;Button
         android:id=&quot;@+id/buttonrestart&quot;
         android:text=&quot;restart&quot;
         android:layout_width=&quot;wrap_content&quot;
         android:layout_height=&quot;wrap_content&quot;
         android:layout_centerInParent=&quot;true&quot;
         android:textColor=&quot;#006400&quot;
         android:background=&quot;#8b0000&quot;
         android:textStyle=&quot;bold|italic&quot;
         android:layout_below=&quot;@id/buttoncheck&quot;
  
         /&gt;
  
  &lt;/RelativeLayout&gt; 
  </code></pre>
  <p>foundnum xml screen 2</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;RelativeLayout
      xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot; android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;
      android:background=&quot;#00008b&quot;
      android:id=&quot;@+id/grats&quot;
      &gt;
  
      &lt;TextView
          android:id=&quot;@+id/tvfound&quot;
          android:layout_width=&quot;wrap_content&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:text=&quot;congrats&quot;
          android:textColor=&quot;#006400&quot;
          android:background=&quot;#8b0000&quot;
          android:textStyle=&quot;bold|italic&quot;
          android:layout_above=&quot;@+id/buttonrestart1&quot;
          android:layout_centerInParent=&quot;true&quot;
          /&gt;
  
  
      &lt;Button
          android:id=&quot;@+id/buttonrestart1&quot;
          android:text=&quot;restart&quot;
          android:layout_width=&quot;wrap_content&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:layout_centerInParent=&quot;true&quot;
          android:textColor=&quot;#006400&quot;
          android:background=&quot;#8b0000&quot;
          android:textStyle=&quot;bold|italic&quot;
  
          /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  <p>main activity java.</p>
  <pre><code>    package com.example.findrandomnumber;
  
      import androidx.appcompat.app.AppCompatActivity;
  
      import android.os.Bundle;
      import android.util.Log;
      import android.view.View;
      import android.widget.Button;
      import android.widget.RelativeLayout;
      import android.widget.TextView;
      import android.widget.Toast;
  
  
       public class MainActivity extends AppCompatActivity implements View.OnClickListener {
          private Button buttonstart, buttonplus, buttonminus, buttoncheck, buttonrestart,buttonrestart1;
          private TextView TVcurrentnum, TVshowmessage, tvcounter,tvfoundnum;
          private String TAG = &quot;gilog&quot;,currentnum,counter;
          private Controllerguessnum myC;
  
  
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          Log.d(TAG, &quot;onCreate: hi&quot;);
  
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
  
          buttonstart =(Button) findViewById(R.id.buttonstart);
          buttonplus = (Button)findViewById(R.id.buttonplus);
          buttonminus =(Button) findViewById(R.id.buttonminus);
          buttoncheck = (Button)findViewById(R.id.buttoncheck);
          buttonrestart = (Button)findViewById(R.id.buttonrestart);
          buttonrestart1 = (Button)findViewById(R.id.buttonrestart1);
  
          TextView  tvcounter= findViewById(R.id.tvcounter);
          TextView  TVshowmessage= findViewById(R.id.TVshowmessage);
          TextView  TVcurrentnum= findViewById(R.id.TVcurrentnum);
          TextView tvfoundnum= findViewById(R.id.tvfound);
          findViewById(R.id.page);
          findViewById(R.id.grats);
  
  
          buttonstart.setOnClickListener(this);
          buttonrestart.setOnClickListener(this);
          buttoncheck.setOnClickListener(this);
          buttonplus.setOnClickListener(this);
          buttonminus.setOnClickListener(this);
  
          Log.d(TAG, &quot;onCreate: set clickers worked&quot;);
  
  
      }
  
      public void startGame() {
              myC = new Controllerguessnum();
  
             Log.d(TAG, &quot;startGame: random number is &quot; + myC.crandom());
  
  
      }
      public void showmessage(){
          TextView  TVshowmessage= findViewById(R.id.TVshowmessage);
  
          Log.d(TAG, &quot;onClick: button check was pressed&quot;);
  
  
          if (myC.ccheck() == &quot;smaller&quot;){
              Log.d(TAG, &quot;onClick: check is ? &quot;+ myC.ccheck() +&quot; numbers: &quot;+myC.ccurrentnum()+ &quot;&lt;&quot;+ myC.crandom());
              TVshowmessage.setText(&quot;number is smaller than random number&quot; + 0);
          }
  
          else
              if (myC.ccheck() == &quot;equal&quot;){
                  Log.d(TAG, &quot;onClick: check is ? &quot;+ myC.ccheck() + &quot;=&quot;+ myC.crandom());
                  TVshowmessage.setText(&quot;congrats you found the number, &quot;+String.valueOf(myC.ccounter()/2)+&quot; tries,&quot;+ &quot; press restart to play again&quot; );
              }
          else {
              if(myC.ccheck() == &quot;bigger&quot;) {
                  Log.d(TAG, &quot;onClick: check is ? &quot;+ myC.ccheck() + &quot;&gt;&quot;+ myC.crandom());
                  TVshowmessage.setText(&quot;number you chose is bigger than random number&quot; + 0);
              }
          }
  
      }
      public void changepage(){
          TextView tvfoundnum= findViewById(R.id.tvfound);
          RelativeLayout grats= findViewById(R.id.grats);
          RelativeLayout page= findViewById(R.id.page);
          TextView  TVshowmessage= findViewById(R.id.TVshowmessage);
          if (myC.ccheck() == &quot;equal&quot;){
              page.setVisibility(View.GONE);
              setContentView(grats);
          }
          else{
              setContentView(page);
  
          }
      }
  
  
      @Override
      public void onClick(View v) {
          buttonstart =(Button) findViewById(R.id.buttonstart);
          buttonplus = (Button)findViewById(R.id.buttonplus);
          buttonminus =(Button) findViewById(R.id.buttonminus);
          buttoncheck = (Button)findViewById(R.id.buttoncheck);
          buttonrestart = (Button)findViewById(R.id.buttonrestart);
          TVcurrentnum= findViewById(R.id.TVcurrentnum);
          tvcounter= findViewById(R.id.tvcounter);
  
  
          if (v.getId() == R.id.buttonstart || v.getId() == R.id.buttonrestart) {
             startGame();
          }
          if (v.getId() == R.id.buttonrestart1){
              findViewById(R.id.page).setVisibility(View.VISIBLE);
              findViewById(R.id.grats).setVisibility(View.GONE);
              startGame();
          }
          //button check
          if (v.getId() == R.id.buttoncheck) {
              showmessage();
              myC.ccntplus();
              tvcounter.setText(String.valueOf((int)(myC.ccounter())+ 0));
              Log.d(TAG, &quot;onClick: counter returned&quot;+ tvcounter.getText());
              changepage();
          }
          if (v.getId() == R.id.buttonplus) {
              Log.d(TAG, &quot;onClick: button plus was pressed&quot;);
              myC.cplus();
              TVcurrentnum.setText(String.valueOf((int)(myC.ccurrentnum()) + 0));
  
          } else if (v.getId() == R.id.buttonminus) {
              Log.d(TAG, &quot;onClick: button minus was pressed&quot;);
              myC.cminus();
              TVcurrentnum.setText(String.valueOf((int)(myC.ccurrentnum()) + 0));
  
  
          }
      }
  }
  
  </code></pre>
  <p>there is more code in two other java files but these are not relevant to changing screen but I will add it below.
  controller.java</p>
  <pre><code>
  package com.example.findrandomnumber;
  
  public class Controllerguessnum {
  
      private Modelguessnum myModel1;
      public double counter=0;
      private int random;
  
      public Controllerguessnum(){
          myModel1= new Modelguessnum();
          cstartGame();
          this.counter=0;
      }
      public void cstartGame(){
          myModel1.mStartGame();
          this.counter=0;    }
      public void crestartgame(){
          myModel1.mStartGame();
          this.counter=0;
      }
      public void cplus(){
          myModel1.mplus();
      }
      public void cminus(){
          myModel1.mminus();
      }
      public void ccntplus(){
          this.counter++;
      }
  
      public String ccheck(){
  
          String res = myModel1.mcheck();
          return res;
      }
  
      public double ccurrentnum(){
          int cnum=myModel1.getCurrentnum();
          return cnum;
  
      }
  
      public double ccounter(){
          double cnt= this.counter;
          return cnt;
  }
      public int crandom(){
          this.random = myModel1.getRandomnum();
          return this.random;
  }
  }
  </code></pre>
  <p>model.java</p>
  <pre><code>package com.example.findrandomnumber;
  
  import java.util.Random;
  
  public class Modelguessnum {
      private int currentnum;
      private int  randomnum;
      private  int counter=0;
  
      public Modelguessnum() {
  
      }
      public void mStartGame(){
  //        this.randomnum= new Random().nextInt( 100);
  //        this.currentnum= new Random().nextInt(100);
          this.randomnum=5;
          this.currentnum=5;
      }
      public int getCurrentnum(){
          int mnum= currentnum;
          return mnum;
      }
      public int getRandomnum(){
          return randomnum;
      }
  
  
      public void mplus(){
          this.currentnum++;
      }
      public void mminus()
      {
          this.currentnum--;
      }
  
      public String mcheck() {
          //-1 is smaller
          //0 equal
          //1 is bigger
          if (this.currentnum &gt; this.randomnum) {
              return &quot;bigger&quot;;
          }
          else
            if (this.currentnum==this.randomnum){
                 return &quot;equal&quot;;
          }
  
          else
              if(this.currentnum &lt; this.randomnum){
                  return &quot;smaller&quot;;
              }
          else
             return &quot;neither&quot;;
  }
  }
  
  </code></pre>
  <p>the math part of the code worked fine- any improvements are welcome. - how do I solve the errors so that when I find the number, when the random number = current number and when I press check, I want it to bring me to the second screen/layout(foundnum.xml) - foundnum.xml
  the error I get from this is the following:</p>
  <pre><code>E/AndroidRuntime: FATAL EXCEPTION: main
      Process: com.example.findrandomnumber, PID: 480
      java.lang.IllegalArgumentException: Cannot add a null child view to a ViewGroup
          at android.view.ViewGroup.addView(ViewGroup.java:3718)
          at android.view.ViewGroup.addView(ViewGroup.java:3700)
          at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:687)
          at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:175)
          at com.example.findrandomnumber.MainActivity.changepage(MainActivity.java:95)
          at com.example.findrandomnumber.MainActivity.onClick(MainActivity.java:129)
          at android.view.View.performClick(View.java:4780)
          at android.view.View$PerformClick.run(View.java:19866)
          at android.os.Handler.handleCallback(Handler.java:739)
          at android.os.Handler.dispatchMessage(Handler.java:95)
          at android.os.Looper.loop(Looper.java:135)
          at android.app.ActivityThread.main(ActivityThread.java:5254)
          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:903)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)&gt;
  
  </code></pre>