ボタンをクリックすると [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();
}
} }