-2
Could someone tell me why the program closes after clicking the button?
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import org.w3c.dom.Text;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//reference to xml widgets
TextView FishText = (TextView) findViewById(R.id.FishText);
TextView DogText = (TextView) findViewById((R.id.DogText));
Button ChangeNameButton = (Button) findViewById(R.id.ChangeNameButton);
final String animals[] = new String[5];
}
public void ChangeFish(){
String [] animals = {"Dog","Cat","Rat","Horse","Mouse"};
final TextView FishText = (TextView) findViewById(R.id.FishText);
for(int contador = 0;contador<5;contador++) {
FishText.setText(animals[contador]);
}
}
}
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="41dp"
android:text="This words will change if your press the button!"
android:textSize="18dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintTop_creator="1" />
<TextView
android:id="@+id/FishText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="162dp"
android:layout_marginStart="162dp"
android:layout_marginTop="60dp"
android:text="Fish"
android:textColor="#C12B2B"
android:textSize="20sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintTop_creator="1" />
<Button
android:id="@+id/ChangeNameButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="ChangeFish"
android:text="Click me to change the word"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:layout_constraintBottom_creator="1"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintTop_creator="1" />
<TextView
android:id="@+id/DogText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:text="Dog"
android:textSize="20sp"
app:layout_constraintRight_toRightOf="@+id/FishText"
app:layout_constraintTop_toBottomOf="@+id/FishText"
tools:layout_constraintRight_creator="1"
tools:layout_constraintTop_creator="1" /><![CDATA[
;
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.271"/>
=D Which button?! Where is activity_main.xml ?
– viana
I’ve already edited with activity_main.xml thanks for your time.
– Pedro Gouveia