1
I’m making a simple application, it’s just a counter, with two buttons, "+"e"-", which consists of displaying an initial value of 0, and if I click any of the buttons, the value displayed on the screen will be added, or decremented, according to the corresponding button. My problem is in displaying the new value on the screen. I take the value of Textview, make the changes, and finally, I can’t display it on the screen, the app just closes.
public class MainActivity extends AppCompatActivity {
Button btmais, btmenos, btmais2, btmenos2 ;
TextView pontuacao, pontuacao2;
EditText nome, nome2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btmais = (Button) findViewById(R.id.btmais);
btmenos = (Button) findViewById(R.id.btmenos);
btmais2 = (Button) findViewById(R.id.btmais2);
btmenos2 = (Button) findViewById(R.id.btmenos2);
pontuacao = (TextView) findViewById(R.id.pontuacao);
pontuacao2 = (TextView) findViewById(R.id.pontuacao2);
btmais.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
int numero = Integer.parseInt( pontuacao.getText().toString() );
numero = numero + 1;
pontuacao.setText(numero);
}
});
}
}
Xml codes:
<TableLayout
android:layout_width="182dp"
android:layout_height="match_parent">
<TableRow
android:layout_width="wrap_content"
android:layout_height="match_parent">
<EditText
android:id="@+id/nome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="Name" />
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="match_parent">
<TextView
android:id="@+id/pontuacao"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:textSize="100sp"
tools:text="00" />
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="match_parent">
<Button
android:id="@+id/btmais"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="+" />
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="match_parent">
<Button
android:id="@+id/btmenos"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="-" />
</TableRow>
</TableLayout>
<TableLayout
android:layout_width="wrap_content"
android:layout_height="match_parent">
<TableRow
android:layout_width="wrap_content"
android:layout_height="match_parent">
<EditText
android:id="@+id/nome2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="Name" />
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="match_parent">
<TextView
android:id="@+id/pontuacao2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="01"
android:textAlignment="center"
android:textSize="100sp"
tools:text="@string/pontuacao2" />
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="match_parent">
<Button
android:id="@+id/btmais2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="+" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/btmenos2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="-" />
</TableRow>
</TableLayout>
Many components of the layout are still in disuse because I first need to make this code work, it is the main part.
Thank you very much for your help and attention
Edit the question and insert your activity_main.xml into it.
– viana