gpt4 book ai didi

java - MediaPlayer 使我的应用程序崩溃(编辑 : it was caused by an out of bound array)

转载 作者:行者123 更新时间:2023-12-01 09:35:19 26 4
gpt4 key购买 nike

作为此问题的后续App Crashing when playing sounds in succession in MediaPlayer

我将 onCreate 中的代码复制到另一个方法(名为 buttonTapped)中。应用程序在 onCreate 期间正确播放声音,但在调用 buttonTapped 方法(通过点击按钮)时崩溃且不播放任何声音。错误信息是:

unfortunately the application has stopped.

这是代码,后面是堆栈跟踪

public class MainActivity extends Activity implements MediaPlayer.OnCompletionListener {

int[] tracks = new int[3];
int currentTrack = 0;
private MediaPlayer mediaPlayer = null;

public void buttonTapped(View view){
tracks[0] = R.raw.p46;
tracks[1] = R.raw.p52;
tracks[2] = R.raw.p55;
mediaPlayer = MediaPlayer.create(getApplicationContext(), tracks[currentTrack]);
mediaPlayer.setOnCompletionListener(this);
mediaPlayer.start();

}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

tracks[0] = R.raw.p46;
tracks[1] = R.raw.p52;
tracks[2] = R.raw.p55;
mediaPlayer = MediaPlayer.create(getApplicationContext(), tracks[currentTrack]);
mediaPlayer.setOnCompletionListener(this);
mediaPlayer.start();
}

public void onCompletion(MediaPlayer arg0) {
arg0.release();
currentTrack++;

if (currentTrack < tracks.length) {
arg0 = MediaPlayer.create(getApplicationContext(), tracks[currentTrack]);
arg0.setOnCompletionListener(this);
arg0.start();
}
}

530-530/com.maxd.soundtesting E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.maxd.soundtesting, PID: 530
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:4020)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4015)
            at android.view.View.performClick(View.java:4780)
            at android.view.View$PerformClick.run(View.java:19866)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.ArrayIndexOutOfBoundsException: length=3; index=3
at com.maxd.soundtesting.MainActivity.buttonTapped(MainActivity.java:22)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at android.view.View$1.onClick(View.java:4015)
            at android.view.View.performClick(View.java:4780)
            at android.view.View$PerformClick.run(View.java:19866)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

最佳答案

在点击按钮时,您不会将 currentTrack 重置为 0。因此,在 onCreate 播放了 3 个声音后,currentTrack = 3,这是一个无效索引,因为当前轨道只能等于 0、1 或 2。

关于java - MediaPlayer 使我的应用程序崩溃(编辑 : it was caused by an out of bound array),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39027057/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com