I can not change the layout on android

0

Hello everyone, I'm doing an old game app for android, when the app starts I want this call the test layout, and when I click the test layout button I want to go to the layout that has the old game , so when I put:

setContentView (R.layout.test); in my java code the app when opens appears fatal error, But when I put: setContentView (R.layout.activity_velha); it works normally

java code:

package com.example.pc_vicl.velhaiftm;

import android.app.AlertDialog;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.view.*;
import android.widget.EditText;
import android.widget.TextView;


public class velha extends AppCompatActivity {

    private Button L11,L12,L13,L21,L22,L23,L31,L32,L33;
    private TextView J1wins,J2wins,Velhaswins,J1text,J2text;
    private EditText N1,N2;
    public String nome1,nome2;
    public int vez;
    public int wj1;
    public int wj2;
    public int velhw;
    public int[][]tab;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.teste);
        N1=(EditText) findViewById(R.id.editText);
        N2=(EditText) findViewById(R.id.editText2);
        L11=(Button) findViewById(R.id.buttonL11);
        L12=(Button) findViewById(R.id.buttonL12);
        L13=(Button) findViewById(R.id.buttonL13);
        L21=(Button) findViewById(R.id.buttonL21);
        L22=(Button) findViewById(R.id.buttonL22);
        L23=(Button) findViewById(R.id.buttonL23);
        L31=(Button) findViewById(R.id.buttonL31);
        L32=(Button) findViewById(R.id.buttonL32);
        L33=(Button) findViewById(R.id.buttonL33);
        J1wins=(TextView) findViewById(R.id.j1win);
        J2wins=(TextView) findViewById(R.id.j2win);
        Velhaswins=(TextView) findViewById(R.id.velhaswin);
        J1text=(TextView) findViewById(R.id.textj1);
        J2text=(TextView) findViewById(R.id.textj2);
        vez=0;
        wj1=0;
        wj2=0;
        velhw=0;
        J1text.setTextColor(Color.BLUE);
        J2text.setTextColor(Color.BLACK);


        tab=new int[3][3];
        for(int cont=0;cont<3;cont++){
            for(int cont2=0;cont2<3;cont2++){
               tab[cont][cont2]=0;

            }
        }

    }
    public void jgnovamente(){
        for(int cont=0;cont<3;cont++){
            for(int cont2=0;cont2<3;cont2++){
                tab[cont][cont2]=0;

            }
        }
        String name=" ";
        L11.setText(name);
        L12.setText(name);
        L13.setText(name);
        L21.setText(name);
        L22.setText(name);
        L23.setText(name);
        L31.setText(name);
        L32.setText(name);
        L33.setText(name);

    }


    public void vamos(){

        nome1=N1.getText().toString();
        nome2=N2.getText().toString();

        if(nome1.trim().isEmpty()||nome2.trim().isEmpty()){
            AlertDialog.Builder dlg= new AlertDialog.Builder(this);
            dlg.setMessage("Por Favor Informe os Nomes.\n");
            dlg.setNegativeButton("ok", null);
            dlg.show();
        }
        J1text.setText(nome1);
        J2text.setText(nome2);
        setContentView(R.layout.activity_velha);

    }
    public void jogador1win(){
        AlertDialog.Builder dlg= new AlertDialog.Builder(this);
        dlg.setMessage(""+nome1+" Ganhou\n");
        dlg.setNegativeButton("ok", null);
        dlg.show();
        wj1++;
        String name=String.valueOf(wj1);
        J1wins.setText(name);
        jgnovamente();
    }
    public void jogador2win(){
        AlertDialog.Builder dlg= new AlertDialog.Builder(this);
        dlg.setMessage(""+nome2+" Ganhou\n");
        dlg.setNegativeButton("ok", null);
        dlg.show();
        wj2++;
        String name=String.valueOf(wj2);
        J2wins.setText(name);
        jgnovamente();

    }
    public void velhawin(){
        AlertDialog.Builder dlg= new AlertDialog.Builder(this);
        dlg.setMessage("Velha.\n");
        dlg.setNegativeButton("ok", null);
        dlg.show();
        velhw++;
        String name=String.valueOf(velhw);
        Velhaswins.setText(name);
        jgnovamente();
    }
    public void verificar(int v[][]){

    int[] horizotal,vertical;

    horizotal=new int [3];
    vertical=new int [3];
    for(int cont=0;cont<3;cont++){
        horizotal[cont]=v[cont][0]+v[cont][1]+v[cont][2];
        if(horizotal[cont]==9){
            jogador2win();
        }
        if(horizotal[cont]==15){
            jogador1win();
        }
    }
    for(int cont2=0;cont2<3;cont2++){
        vertical[cont2]=v[0][cont2]+v[1][cont2]+v[2][cont2];
        if(vertical[cont2]==9){
            jogador2win();
        }
        if(vertical[cont2]==15){
            jogador1win();
        }
    }
        int dig1=v[0][0]+v[1][1]+v[2][2];
        int dig2=v[2][0]+v[1][1]+v[0][2];
        if(dig1==9){
            jogador2win();
        }
        if(dig1==15){
            jogador1win();
        }
        if(dig2==9){
            jogador2win();
        }
        if(dig2==15){
            jogador1win();
        }
        int nj=0;
        for(int cont=0;cont<3;cont++){
            for(int cont2=0;cont2<3;cont2++){
                if(v[cont][cont2]==3||v[cont][cont2]==5){
                    nj++;
                    if(nj==9){
                        velhawin();
                    }
                }
            }
        }

    }
    public int vezdequem(){

        vez++;
        int vezde;
        if(vez%2==0){
            vezde=3;
            J2text.setTextColor(Color.BLACK);
            J1text.setTextColor(Color.BLUE);
        }else{
            vezde=5;
            J1text.setTextColor(Color.BLACK);
            J2text.setTextColor(Color.RED);
        }

        return vezde;
    }
    public String mudaricone(int b){

        int thebutton=0;
        String name;

    for(int cont=0;cont<3;cont++) {
        for(int cont2=0;cont2<3;cont2++){
         thebutton++;

         if(thebutton==b){

        if (tab[cont][cont2]!=3&&tab[cont][cont2]!=5) {
            tab[cont][cont2] = vezdequem();
            if(tab[cont][cont2]==3){
                //mudar buton para O
                name="O";
                return name;

            }
            if(tab[cont][cont2]==5){
                //mudar buton para X
                name="X";
                return name;

            }

        } else {
            //alert para escolher outro
            AlertDialog.Builder dlgg= new AlertDialog.Builder(this);
            dlgg.setMessage("Por favor realize uma jogava Valida.\n");
            dlgg.setNegativeButton("ok", null);
            dlgg.show();
            if(tab[cont][cont2]==5){
                name="X";
                return name;
            }
            if(tab[cont][cont2]==3){
                name="O";
                return name;

            }



        }}


    }}

        return null;

    }


    public void b11 (View v){
        int ok11=1;
        String name=mudaricone(ok11);
        L11.setText(name);
        verificar(tab);
    }
    public void b12 (View v){
        int ok11=2;
        String name=mudaricone(ok11);
        L12.setText(name);
        verificar(tab);

    }
    public void b13 (View v){
        int ok11=3;
        String name=mudaricone(ok11);
        L13.setText(name);
        verificar(tab);
    }
    public void b21 (View v){
        int ok11=4;
        String name=mudaricone(ok11);
        L21.setText(name);
        verificar(tab);
    }
    public void b22 (View v){
        int ok11=5;
        String name=mudaricone(ok11);
        L22.setText(name);
        verificar(tab);
    }
    public void b23 (View v){
        int ok11=6;
        String name=mudaricone(ok11);
        L23.setText(name);
        verificar(tab);
    }
    public void b31 (View v){
        int ok11=7;
        String name=mudaricone(ok11);
        L31.setText(name);
        verificar(tab);
    }
    public void b32 (View v){
        int ok11=8;
        String name=mudaricone(ok11);
        L32.setText(name);
        verificar(tab);
    }
    public void b33 (View v){
        int ok11=9;
        String name=mudaricone(ok11);
        L33.setText(name);
        verificar(tab);
    }
}

Test xml code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Nome do Jogador 1:"
        android:id="@+id/textView"
        android:layout_marginTop="147dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="17dp"
        android:layout_marginStart="17dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Nome do Jogador 2:"
        android:id="@+id/textView2"
        android:layout_below="@+id/editText"
        android:layout_toLeftOf="@+id/editText2"
        android:layout_toStartOf="@+id/editText2"
        android:layout_marginTop="40dp" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="10"
        android:id="@+id/editText"
        android:layout_alignBaseline="@+id/textView"
        android:layout_alignBottom="@+id/textView"
        android:layout_toRightOf="@+id/textView"
        android:layout_toEndOf="@+id/textView" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="10"
        android:id="@+id/editText2"
        android:layout_alignBaseline="@+id/textView2"
        android:layout_alignBottom="@+id/textView2"
        android:layout_alignLeft="@+id/editText"
        android:layout_alignStart="@+id/editText" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Começar"
        android:id="@+id/comecar"
        android:layout_below="@+id/editText2"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="63dp"
        android:onClick="vamos"
        android:nestedScrollingEnabled="false" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Definir Nomes:"
        android:id="@+id/textView3"
        android:layout_marginBottom="49dp"
        android:layout_above="@+id/editText"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/desenvolvedor"
        android:id="@+id/textView4"
        android:layout_alignParentBottom="true"
        android:layout_alignLeft="@+id/textView2"
        android:layout_alignStart="@+id/textView2"
        android:layout_marginBottom="40dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Desenvolvedor:"
        android:id="@+id/textView5"
        android:layout_above="@+id/textView4"
        android:layout_alignLeft="@+id/textView4"
        android:layout_alignStart="@+id/textView4" />


</RelativeLayout>

activity_velha xml code:

<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.pc_vicl.velhaiftm.velha">

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="80dp"
        android:layout_height="50dp"
        android:text="@string/l11"
        android:id="@+id/buttonL11"
        android:layout_marginLeft="48dp"
        android:layout_marginStart="48dp"
        android:layout_marginTop="146dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:onClick="b11" />

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="80dp"
        android:layout_height="50dp"
        android:text="@string/l12"
        android:id="@+id/buttonL12"
        android:layout_alignBottom="@+id/buttonL11"
        android:layout_toRightOf="@+id/buttonL11"
        android:layout_toEndOf="@+id/buttonL11"
        android:onClick="b12" />

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="80dp"
        android:layout_height="50dp"
        android:text="@string/l13"
        android:id="@+id/buttonL13"
        android:layout_alignBottom="@+id/buttonL12"
        android:layout_toRightOf="@+id/buttonL12"
        android:layout_toEndOf="@+id/buttonL12"
        android:onClick="b13" />

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="80dp"
        android:layout_height="50dp"
        android:text="@string/l21"
        android:id="@+id/buttonL21"
        android:layout_below="@+id/buttonL11"
        android:layout_alignLeft="@+id/buttonL11"
        android:layout_alignStart="@+id/buttonL11"
        android:onClick="b21" />

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="80dp"
        android:layout_height="50dp"
        android:text="@string/l22"
        android:id="@+id/buttonL22"
        android:layout_below="@+id/buttonL12"
        android:layout_toRightOf="@+id/buttonL21"
        android:layout_toEndOf="@+id/buttonL21"
        android:onClick="b22"/>

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="80dp"
        android:layout_height="50dp"
        android:text="@string/l23"
        android:id="@+id/buttonL23"
        android:layout_below="@+id/buttonL13"
        android:layout_toRightOf="@+id/buttonL22"
        android:layout_toEndOf="@+id/buttonL22"
        android:onClick="b23"/>

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="80dp"
        android:layout_height="50dp"
        android:text="@string/l31"
        android:id="@+id/buttonL31"
        android:layout_below="@+id/buttonL21"
        android:layout_alignLeft="@+id/buttonL21"
        android:layout_alignStart="@+id/buttonL21"
        android:onClick="b31"/>

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="80dp"
        android:layout_height="50dp"
        android:text="@string/l32"
        android:id="@+id/buttonL32"
        android:layout_alignBottom="@+id/buttonL31"
        android:layout_toRightOf="@+id/buttonL31"
        android:layout_toEndOf="@+id/buttonL31"
        android:onClick="b32"/>

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="80dp"
        android:layout_height="50dp"
        android:text="@string/l33"
        android:id="@+id/buttonL33"
        android:layout_alignBottom="@+id/buttonL32"
        android:layout_toRightOf="@+id/buttonL32"
        android:layout_toEndOf="@+id/buttonL32"
        android:onClick="b33"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="0"
        android:id="@+id/j2win"
        android:textSize="20dp"
        android:layout_below="@+id/j1win"
        android:layout_alignLeft="@+id/j1win"
        android:layout_alignStart="@+id/j1win" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="0"
        android:id="@+id/velhaswin"
        android:textSize="20dp"
        android:layout_below="@+id/j2win"
        android:layout_alignLeft="@+id/j2win"
        android:layout_alignStart="@+id/j2win" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="0"
        android:id="@+id/j1win"
        android:textSize="20dp"
        android:layout_marginTop="10dp"
        android:layout_alignParentTop="true"
        android:layout_alignRight="@+id/buttonL11"
        android:layout_alignEnd="@+id/buttonL11" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Jogador 1:   "
        android:id="@+id/textj1"
        android:textSize="20dp"
        android:layout_alignTop="@+id/j1win"
        android:layout_toLeftOf="@+id/j2win"
        android:layout_toStartOf="@+id/j2win"
        android:textColor="#0000ff" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Jogador 2:  "
        android:id="@+id/textj2"
        android:textSize="20dp"
        android:layout_alignTop="@+id/j2win"
        android:layout_alignLeft="@+id/textj1"
        android:layout_alignStart="@+id/textj1"
        android:textColor="#ff0000" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Velha:  "
        android:id="@+id/textView6"
        android:textSize="20dp"
        android:layout_below="@+id/textj2"
        android:layout_alignRight="@+id/textj2"
        android:layout_alignEnd="@+id/textj2" />
</RelativeLayout>
    
asked by anonymous 17.04.2016 / 18:10

1 answer

2

Once you call the setContentView() method passing a layout as a parameter, you can only manipulate and instantiate the elements of this layout , so once you've called setContentView(R.layout.teste) you can only manipulate elements contained in the test.xml file, that is:

  • TextViews textView, textView2, textView3, textView4, textView5
  • The EditTexts editext, editext2
  • The Start Button
  • However, in your code, what you do is try to access the elements that are in the activity_velha file by

        N1=(EditText) findViewById(R.id.editText);
        N2=(EditText) findViewById(R.id.editText2);
        L11=(Button) findViewById(R.id.buttonL11);
        L12=(Button) findViewById(R.id.buttonL12);
        L13=(Button) findViewById(R.id.buttonL13);
        L21=(Button) findViewById(R.id.buttonL21);
        L22=(Button) findViewById(R.id.buttonL22);
        L23=(Button) findViewById(R.id.buttonL23);
        L31=(Button) findViewById(R.id.buttonL31);
        L32=(Button) findViewById(R.id.buttonL32);
        L33=(Button) findViewById(R.id.buttonL33);
        J1wins=(TextView) findViewById(R.id.j1win);
        J2wins=(TextView) findViewById(R.id.j2win);
        Velhaswins=(TextView) findViewById(R.id.velhaswin);
        J1text=(TextView) findViewById(R.id.textj1);
        J2text=(TextView) findViewById(R.id.textj2);
    

    So the application is crashing, it is trying to access layout elements that do not exist in the test.xml file

    You have two options to work around:

  • Create a new activity that uses the test layout

    In this case, when the user clicked on the layout_test button of their activity_velha , it would call a new activity, which would be their activity containing the test layout. p>

  • Use fragments to dynamically create and change layout

  • 17.04.2016 / 23:02