我正在尝试制作一个在单击按钮时播放声音的应用程序,并且我有 100 多个声音和 100 多个按钮。我已经创建了按钮,现在将其映射到要播放的声音。另一件事是,当点击次数超过“x”次时会mediaplayer
崩溃。关于如何解决这个问题的任何想法?
注意:代码不想像现在这样执行,我真的不明白为什么。
package com.example.buttonsdemo;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.support.v4.widget.SimpleCursorAdapter.ViewBinder;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity implements View.OnClickListener {
// Creating Sound arrays
int i = 0;
MediaPlayer[] mediaplayer = new MediaPlayer[120];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for (int z = 0; z < 121; z++) {
mediaplayer[z] = null;
}
// Creating Button Array
Button button[] = new Button[120];
for (int x = 0; x < 121; x++) {
button[x] = null;
button[x].setOnClickListener(this);
}
// Creating Media player array
mediaplayer[1] = MediaPlayer.create(this, R.raw.alistar);
mediaplayer[0] = MediaPlayer.create(this, R.raw.akali);
button[i].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.akali:
i = 0;
break;
case R.id.alistar:
i = 1;
break;
}
mediaplayer[i].start();
}
});
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
日志猫:
03-05 01:10:34.674: E/AndroidRuntime(1541): FATAL EXCEPTION: main
03-05 01:10:34.674: E/AndroidRuntime(1541): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.buttonsdemo/com.example.buttonsdemo.MainActivity}: java.lang.ArrayIndexOutOfBoundsException: length=120; index=120
03-05 01:10:34.674: E/AndroidRuntime(1541): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
03-05 01:10:34.674: E/AndroidRuntime(1541): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-05 01:10:34.674: E/AndroidRuntime(1541): at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-05 01:10:34.674: E/AndroidRuntime(1541): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-05 01:10:34.674: E/AndroidRuntime(1541): at android.os.Handler.dispatchMessage(Handler.java:99)
03-05 01:10:34.674: E/AndroidRuntime(1541): at android.os.Looper.loop(Looper.java:137)
03-05 01:10:34.674: E/AndroidRuntime(1541): at android.app.ActivityThread.main(ActivityThread.java:5041)
03-05 01:10:34.674: E/AndroidRuntime(1541): at java.lang.reflect.Method.invokeNative(Native Method)
03-05 01:10:34.674: E/AndroidRuntime(1541): at java.lang.reflect.Method.invoke(Method.java:511)
03-05 01:10:34.674: E/AndroidRuntime(1541): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-05 01:10:34.674: E/AndroidRuntime(1541): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-05 01:10:34.674: E/AndroidRuntime(1541): at dalvik.system.NativeStart.main(Native Method)
03-05 01:10:34.674: E/AndroidRuntime(1541): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=120; index=120
03-05 01:10:34.674: E/AndroidRuntime(1541): at com.example.buttonsdemo.MainActivity.onCreate(MainActivity.java:28)
03-05 01:10:34.674: E/AndroidRuntime(1541): at android.app.Activity.performCreate(Activity.java:5104)
03-05 01:10:34.674: E/AndroidRuntime(1541): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-05 01:10:34.674: E/AndroidRuntime(1541): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-05 01:10:34.674: E/AndroidRuntime(1541): ... 11 more