0

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>
Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
ManManu
  • 29
  • 5

1 Answers1

1
currentRandom = random.nextInt(layout.getChildCount() - 1);
...
Button winButton = findViewById(currentRandom);

The problem is here. currentRandom is random int between 0 and the amount of your buttons and then you are trying to run findViewById for that random Int. You should use getChildAt() instead of findViewById

Button winButton = (Button)layout.getChildAt(currentRandom);
grig
  • 848
  • 6
  • 15
  • the for the quick answer! i corrected my code with what u suggested, but unfortunately "layout" is red. when hovering over it, android studio tells me: "qualifier must be an expression". – ManManu Oct 19 '20 at 09:03
  • Did you defined it? `LinearLayout layout = findViewById(R.id.linearLayout);` – grig Oct 19 '20 at 09:05
  • setRandom and setButton both work perfectly now, thx for your help. newRedButton however does not seem to work at all. normally after clicking on the red button, it should select a new red button, but somehow it doesnt. the button just stays red – ManManu Oct 19 '20 at 09:27