So this code should first create 16 buttons, then select one of them randomly and change its colour to red. Now the user should click on the red button and then a new button becomes red.
In android studio, everything seems to be working, but when I want to open it with the emulator, the app instantly crashes.
I was wondering if someone here knows what to do.
MainActivity.java:
package com.example.sixteenbuttons;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout layout = findViewById(R.id.linearLayout);
//aufgabe1
for (int i = 1; i < 17; i++) {
final Button button = new Button(this);
button.setText("Button " + i);
layout.addView(button);
//aufgabe2
button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Klick auf " + button.getText(),Toast.LENGTH_LONG).show();
}
});
}
setRandom();
setRedButton();
newRedButton();
}
int currentRandom;
public void setRandom() {
Random random = new Random();
LinearLayout layout = findViewById(R.id.linearLayout);
currentRandom = random.nextInt(layout.getChildCount() - 1);
}
public void setRedButton() {
Button winButton = findViewById(currentRandom);
winButton.setTextColor(Color.RED);
}
public void newRedButton() {
Button winButton = findViewById(currentRandom);
winButton.setTextColor(Color.BLACK);
setRandom();
setRedButton();
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
</android.support.constraint.ConstraintLayout>
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sixteenbuttons">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>