gpt4 book ai didi

android - Android应用程序崩溃了吗?

转载 作者:行者123 更新时间:2023-12-03 16:14:59 24 4
gpt4 key购买 nike

我创建了一个应用程序,单击该按钮时说[next]。我正在调用一种方法,该方法的工作是生成5个随机数并将它们随机设置到5个按钮上,单击每个按钮将随机选择一个较小的(2秒)音频文件并进行播放。
如果我单击按钮[下一步]超过8次,则整个应用程序都将重新启动。

logcat没有显示任何错误...

TextView addNo1, addNo2;
Button option1, option2, option3, option4, next;
Random randomGenerator;
Integer randomInt1, randomInt2;
int flag = 0, setans;
MediaPlayer wa1,wa2,wa3,wa4,wa5,wa6,wa7;
MediaPlayerHelper m;
MediaPlayer mp1, mp2, mp3, mp4, mp5, mp6, mp7, mp8, mp9, mp10, mp11, mp12,
mp13, mp14, mp15, mp16, mp17, mp18, mp19, mp20;
int correctmusiclist[] = { R.raw.correct1, R.raw.correct2, R.raw.correct3,
R.raw.correct4, R.raw.correct5, R.raw.correct6, R.raw.correct7,
R.raw.correct8, R.raw.correct9, R.raw.correct10, R.raw.correct11,
R.raw.correct12, R.raw.correct13, R.raw.correct14, R.raw.correct15 };
int wrongmusiclist[] = {R.raw.wrong1,R.raw.wrong2,R.raw.wrong3,R.raw.wrong4,R.raw.wrong5,R.raw.wrong6,
R.raw.wrong7};
Context context = Addition.this;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.additionactivity);
addNo1 = (TextView) findViewById(R.id.textView1);
addNo2 = (TextView) findViewById(R.id.textView2);
option1 = (Button) findViewById(R.id.addoption1);
option2 = (Button) findViewById(R.id.addoption2);
option3 = (Button) findViewById(R.id.addoption3);
option4 = (Button) findViewById(R.id.addoption4);
next = (Button) findViewById(R.id.button1);
next.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
nextChangeAdd();
}
});
m = new MediaPlayerHelper(Addition.this);
randomGenerator = new Random();
Typeface tf = Typeface.createFromAsset(getAssets(),
"fonts/StefanieDots.ttf");
addNo1.setTypeface(tf);
nextChangeAdd();

}

public void nextChangeAdd() {
randomInt1 = randomGenerator.nextInt(10);
randomInt2 = randomGenerator.nextInt(10);
setans = randomGenerator.nextInt(5);
if (setans == 0) {
setans++;
}
int ans = randomInt1 + randomInt2;
String addans = Integer.toString(ans);
addNo1.setText(randomInt1.toString());
addNo2.setText(randomInt2.toString());
List<Integer> generated = new ArrayList<Integer>();
for (int i = 0; i < 3; i++) {
while (true) {
Integer next = randomGenerator.nextInt(19) + 1;
if (!generated.contains(ans)) {
generated.add(next);
break;
}
}
}

switch (setans) {
case 1:
flag = 1;
option1.setText(addans);
option1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
getAudio();
}
});

break;
case 2:
flag = 2;
option2.setText(addans);
option2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
getAudio();
}
});

break;
case 3:
flag = 3;
option3.setText(addans);
option3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
getAudio();
}
});
break;
case 4:
flag = 4;
option4.setText(addans);
option4.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
getAudio();
}
});
break;
}
String a2 = generated.get(0).toString();
String a3 = generated.get(1).toString();
String a4 = generated.get(2).toString();

if (flag == 1) {
option2.setText(a2);
option2.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
wrongAns();
}
});
option3.setText(a3);
option3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
wrongAns();
}
});
option4.setText(a4);
option4.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
wrongAns();
}
});
}
if (flag == 2) {
option1.setText(a2);
option2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
wrongAns();
}
});
option3.setText(a3);
option3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
wrongAns();
}
});
option4.setText(a4);
option4.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
wrongAns();
}
});
}
if (flag == 3) {
option2.setText(a2);
option2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
wrongAns();
}
});
option1.setText(a3);
option1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
wrongAns();
}
});
option4.setText(a4);
option4.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
wrongAns();
}
});
}

if (flag == 4) {
option2.setText(a2);
option2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
wrongAns();
}
});
option3.setText(a3);
option3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
wrongAns();
}
});
option1.setText(a4);
option1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
wrongAns();
}
});
}
}

public void getAudio() {
Random randomGenerator = new Random();
int i = randomGenerator.nextInt(10);
mp1 = MediaPlayer.create(context, correctmusiclist[0]);
mp2 = MediaPlayer.create(context, correctmusiclist[1]);
mp3 = MediaPlayer.create(context, correctmusiclist[2]);
mp4 = MediaPlayer.create(context, correctmusiclist[3]);
mp5 = MediaPlayer.create(context, correctmusiclist[4]);
mp6 = MediaPlayer.create(context, correctmusiclist[5]);
mp7 = MediaPlayer.create(context, correctmusiclist[6]);
mp8 = MediaPlayer.create(context, correctmusiclist[7]);
mp9 = MediaPlayer.create(context, correctmusiclist[8]);
mp10 = MediaPlayer.create(context, correctmusiclist[9]);
mp11 = MediaPlayer.create(context, correctmusiclist[10]);
mp12 = MediaPlayer.create(context, correctmusiclist[11]);
mp13 = MediaPlayer.create(context, correctmusiclist[12]);
mp14 = MediaPlayer.create(context, correctmusiclist[13]);
mp15 = MediaPlayer.create(context, correctmusiclist[14]);
switch (i) {
case 1:
mp1.start();
break;
case 2:
mp2.start();
break;
case 3:
mp3.start();
break;
case 4:
mp4.start();
break;
case 5:
mp5.start();
break;
case 6:
mp6.start();
break;
case 7:
mp7.start();
break;
case 8:
mp8.start();
break;
case 9:
mp9.start();
break;
case 10:
mp10.start();
break;
case 11:
mp11.start();
break;
case 12:
mp12.start();
break;
case 13:
mp13.start();
break;
case 14:
mp14.start();
break;
case 15:
mp15.start();
break;
default:
mp10.start();
}

}

public void wrongAns()
{
Random randomGenerator = new Random();
int i = randomGenerator.nextInt(10);
wa1 = MediaPlayer.create(context, wrongmusiclist[0]);
wa2 = MediaPlayer.create(context, wrongmusiclist[1]);
wa3 = MediaPlayer.create(context, wrongmusiclist[2]);
wa4 = MediaPlayer.create(context, wrongmusiclist[3]);
wa5 = MediaPlayer.create(context, wrongmusiclist[4]);
wa6 = MediaPlayer.create(context, wrongmusiclist[5]);
wa7 = MediaPlayer.create(context, wrongmusiclist[6]);
switch (i) {
case 1:
wa1.start();
break;
case 2:
wa2.start();
break;
case 3:
wa3.start();
break;
case 4:
wa4.start();
break;
case 5:
wa5.start();
break;
case 6:
wa6.start();
break;
case 7:
wa7.start();
break;
default:
wa7.start();
}

}
}

最佳答案

首先,在getAudio()和rongAns()内部播放随机音频是完全错误的,因为您通过创建许多MediaPlayer对象完全将其弄乱了。您需要在生成随机数时仅创建一个MediaPlayer对象并在切换的情况下播放声音。像这样

 Random randomGenerator = new Random();
int i = randomGenerator.nextInt(10);


switch (i) {
case 1:
wa1 = MediaPlayer.create(context, wrongmusiclist[1]);
wa1.start();
break;
case 2:
wa1 = MediaPlayer.create(context, wrongmusiclist[2]);
wa1.start();
break;

并将wa1作为类级别变量。

更新
使用此功能播放音频
public void playBeep() {
try {

if (m.isPlaying()) {
m.stop();
m.release();
m = new MediaPlayer();
}
AssetFileDescriptor descriptor = getAssets().openFd("beepbeep.mp3");
m.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength());
descriptor.close();

m.prepare();
m.setVolume(1f, 1f);
m.setLooping(true);
m.start();
} catch (Exception e) {
}
}

并将文件路径存储在字符串数组中。然后将此函数传递给随机数,然后从数组中选择随机索引到其他文件路径。

关于android - Android应用程序崩溃了吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13611434/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com