我创建了一个应用程序,单击按钮说 [next] 。我正在调用一个方法,该方法的工作是生成 5 个随机数并将它们随机设置为 5 个按钮,单击每个按钮时,它将随机选择一个小(2 秒)音频文件并播放它。如果我单击按钮 [next] 超过 8 次,我的整个应用程序将重新启动。
如果nextChangeAdd();
一次又一次地调用方法,我的应用程序在 10 次函数调用后停止响应。请给我一些替代品?logcat 也没有显示任何错误
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class Addition extends Activity{
TextView addNo1, addNo2;
Button option1, option2, option3, option4, next;
Random randomGenerator;
Integer randomInt1, randomInt2;
int flag = 0, setans;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.additionactivity);
addNo1 = (TextView) findViewById(R.id.addNOOne);
addNo2 = (TextView) findViewById(R.id.addNOTwo);
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.addNextBtn);
next.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
nextChangeAdd();
}
});
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) {
//rightAns();
}
});
break;
case 2:
flag = 2;
option2.setText(addans);
option2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//rightAns();
}
});
break;
case 3:
flag = 3;
option3.setText(addans);
option3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//rightAns();
}
});
break;
case 4:
flag = 4;
option4.setText(addans);
option4.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//rightAns();
}
});
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();
}
});
}
}
}