0

我正在尝试制作一个在单击按钮时播放声音的应用程序,并且我有 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
4

1 回答 1

1

您初始化了数组。但是您缺少对其内容的初始化。

在你的循环中,你需要放

button[x]= new Button(this);
button[x].setOnClickListener(this);

您还在初始化变量i之前使用它。

但现在我想了想,你真正需要做的是复习你的MediaPlayer文档。您真的不需要(也不想)拥有一个包含 120 个 MediaPlayer 对象的数组。从文档中:

完成 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);
}
于 2013-03-05T00:14:14.990 回答