Ciao a tutti, ricevo un errore in fase di debug:
java.lang.IllegalStateException: Could not find method add(View) in a parent or ancestor Context for android:onClick attribute defined on view class androidx.appcompat.widget.AppCompatButton with id 'add'
Il programma è davvero una stupidata... prende 2 numeri immessi dall'utente, premendo il pulsante add recuperi i valori di edittext e li converte in int.
Successivamente crea un oggetto ris di classe calculate che contiene l' operazione somma. Infine converte il risultato in testo e lo visualizza in un toast...
Activity_mail.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<EditText
android:id="@+id/editText2"
android:layout_width="143dp"
android:layout_height="0dp"
android:layout_marginTop="118dp"
android:layout_marginEnd="32dp"
android:ems="10"
android:inputType="numberDecimal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/editText"
android:layout_width="133dp"
android:layout_height="0dp"
android:layout_marginStart="32dp"
android:layout_marginTop="48dp"
android:ems="10"
android:inputType="numberDecimal"
app:layout_constraintEnd_toStartOf="@+id/editText2"
app:layout_constraintHorizontal_bias="0.361"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:layout_marginBottom="49dp"
android:text="Numero 1"
app:layout_constraintBottom_toTopOf="@+id/editText"
app:layout_constraintEnd_toStartOf="@+id/textView2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="80dp"
android:text="numero 2"
app:layout_constraintBottom_toTopOf="@+id/editText2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.491" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="76dp"
android:onClick="add"
android:text="+"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText"
app:layout_constraintVertical_bias="0.188" />
<TextView
android:id="@+id/risultato"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="risultato"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.532" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private EditText num1, num2;
public Button bAdd;
private Calculate ris;
private TextView risultato;
@Override
protected void onCreate ( Bundle savedInstanceState ) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
risultato = findViewById (R.id.risultato);
bAdd = findViewById (R.id.add);
}
public void add( View view, Calculate ris){
String n1 = num1.getText ().toString ();
String n2 = num2.getText ().toString ();
int numCon1 = Integer.parseInt (n1);
int numCon2 = Integer.parseInt (n2);
ris = new Calculate (numCon1, numCon2);
int tot = ris.somma (numCon1, numCon2);
String totText = String.valueOf (tot);
Toast.makeText (getApplicationContext (),totText, Toast.LENGTH_SHORT).show ();
}
}
Calculate.java
public class Calculate {
private int numero1;
private int numero2;
public Calculate (int numero1, int numero2) {
this.numero1 = numero1;
this.numero2 = numero2;
}
public Calculate(){
numero1 = 0;
numero2 = 0;
}
public int getNumero1 () {
return numero1;
}
public void setNumero1 ( int numero1 ) {
this.numero1 = numero1;
}
public int getNumero2 () {
return numero2;
}
public void setNumero2 ( int numero2 ) {
this.numero2 = numero2;
}
public int somma( int numero1, int numero2){
return numero1 + numero2;
}
}
Avete idea perchè da errore?
Grazie in aticipo