我在使用安卓工作室项目时遇到了问题。

huangapple go评论100阅读模式
英文:

I have a problem with my android studio project

问题

这段代码首先应该创建16个按钮,然后随机选择其中一个按钮,并将其颜色更改为红色。接下来用户应该点击红色按钮,然后会有一个新的按钮变为红色。

在Android Studio中,一切似乎都在正常工作,但是当我尝试在模拟器中打开应用时,应用会立即崩溃。

我想知道这里是否有人知道该怎么做。

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);
        
        // 创建16个按钮
        for (int i = 1; i < 17; i++) {
            final Button button = new Button(this);
            button.setText("Button " + i);
            layout.addView(button);

            // 设置按钮点击事件
            button.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View v) {
                    Toast.makeText(MainActivity.this, "点击了" + 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>
英文:

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 &lt; 17; i++) {
            final Button button = new Button(this);
            button.setText(&quot;Button &quot; + i);
            layout.addView(button);

            //aufgabe2
            button.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View v) {
                    Toast.makeText(MainActivity.this, &quot;Klick auf &quot; + 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:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;android.support.constraint.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    tools:context=&quot;.MainActivity&quot;&gt;

    &lt;ScrollView
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;match_parent&quot;&gt;

        &lt;LinearLayout
            android:id=&quot;@+id/linearLayout&quot;
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:orientation=&quot;vertical&quot;&gt;

        &lt;/LinearLayout&gt;
    &lt;/ScrollView&gt;

&lt;/android.support.constraint.ConstraintLayout&gt;

AndroidManifest.xml:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;manifest xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    package=&quot;com.example.sixteenbuttons&quot;&gt;

    &lt;application
        android:allowBackup=&quot;true&quot;
        android:icon=&quot;@mipmap/ic_launcher&quot;
        android:label=&quot;@string/app_name&quot;
        android:roundIcon=&quot;@mipmap/ic_launcher_round&quot;
        android:supportsRtl=&quot;true&quot;
        android:theme=&quot;@style/AppTheme&quot;&gt;
        &lt;activity android:name=&quot;.MainActivity&quot;&gt;
            &lt;intent-filter&gt;
                &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;
                &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
            &lt;/intent-filter&gt;
        &lt;/activity&gt;
    &lt;/application&gt;

&lt;/manifest&gt;

答案1

得分: 1

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

问题在这里。`currentRandom` 是一个在 0 和您的按钮数量之间的随机整数,然后您尝试为该随机整数运行 `findViewById`。您应该使用 `getChildAt()` 而不是 `findViewById`。

Button winButton = (Button) layout.getChildAt(currentRandom);
英文:
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);

huangapple
  • 本文由 发表于 2020年10月19日 16:26:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/64423665.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定