我正在尝试创建一个按钮点击时播放声音的应用程序,我有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
}
}
logcat的:
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
答案 0 :(得分:1)
您初始化了数组。但是你错过了其内容的初始化。
在你的循环中你需要把
button[x]= new Button(this);
button[x].setOnClickListener(this);
在初始化之前,您还使用变量i
。
但是现在我想到了,你真正需要做的就是了解你的MediaPlayer
文档。你真的不需要(也不想)拥有120个MediaPlayer对象的数组。来自documentation:
完成MediaPlayer后,你应该调用release()来释放 资源。如果未释放,将导致过多的MediaPlayer实例 在一个例外中。
您的最新版本有多处错误。第一个错误:
for (int z = 0; z < 121; z++) {
mediaplayer[z] = null;
}
由于您的数组有120个元素,因此您的条件必须是
for (int z = 0; z < 120; z++) {
mediaplayer[z] = null;
}
第二个错误:您将按钮数组元素设置为null,然后尝试访问它们
for(int x=0;x<120;x++){
button[x]=new Button(this);
button[x].setOnClickListener(this);
}