英文:
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 < 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>
答案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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论