Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android应用程序在播放声音后试图移动到下一个屏幕时崩溃_Android_Media Player - Fatal编程技术网

Android应用程序在播放声音后试图移动到下一个屏幕时崩溃

Android应用程序在播放声音后试图移动到下一个屏幕时崩溃,android,media-player,Android,Media Player,我正在制作一个基本类,它使用一个按钮播放声音,然后使用另一个按钮转到下一个屏幕。问题是,在使用“声音”按钮后,“下一步”按钮会使应用程序崩溃。我以为这是因为我在释放声音,然后检查它,但它仍然崩溃 public class Explain1 extends Activity { MediaPlayer mysound; protected boolean active = true; protected int splashtime = 17000; @Overri

我正在制作一个基本类,它使用一个按钮播放声音,然后使用另一个按钮转到下一个屏幕。问题是,在使用“声音”按钮后,“下一步”按钮会使应用程序崩溃。我以为这是因为我在释放声音,然后检查它,但它仍然崩溃

public class Explain1 extends Activity
{
    MediaPlayer mysound;
    protected boolean active = true;
    protected int splashtime = 17000;
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.exp1);
        mysound=MediaPlayer.create(Explain1.this, R.raw.ex1 );
    }
    @Override
        protected void onDestroy()
        {

            super.onDestroy();
            mysound.stop();
            mysound.reset();
            mysound.release();
            mysound = null;
        }
    public void cont(View view) 
    {
        if(mysound.isPlaying())
        {
            mysound.stop();
            mysound.reset();
            mysound.release();

            Log.d("Exercise", "Sound Stopped");
        }
        Intent i = new Intent();
        i.setClassName("com.example","com.example.timestableseasy.Explain2");
        startActivity(i); 
    }

  public void listen(View view) 
    {
        mysound=MediaPlayer.create(Explain1.this, R.raw.ex1 );
        mysound.setOnCompletionListener(new OnCompletionListener() {
            public void onCompletion(MediaPlayer mysound) 
            {
                mysound.stop();
                mysound.release();
                mysound=MediaPlayer.create(Explain1.this, R.raw.ex1 );
                Log.d("Exercise", "Sound PLayed");
            }

        });   
        mysound.start();

    }

}
这是原木猫

06-10 14:35:39.444: E/AndroidRuntime(30611): FATAL EXCEPTION: main
06-10 14:35:39.444: E/AndroidRuntime(30611): java.lang.IllegalStateException: Could not execute method of the activity
06-10 14:35:39.444: E/AndroidRuntime(30611):    at android.view.View$1.onClick(View.java:3814)
06-10 14:35:39.444: E/AndroidRuntime(30611):    at android.view.View.performClick(View.java:4421)
06-10 14:35:39.444: E/AndroidRuntime(30611):    at android.view.View$PerformClick.run(View.java:17903)
06-10 14:35:39.444: E/AndroidRuntime(30611):    at android.os.Handler.handleCallback(Handler.java:730)
06-10 14:35:39.444: E/AndroidRuntime(30611):    at android.os.Handler.dispatchMessage(Handler.java:92)
06-10 14:35:39.444: E/AndroidRuntime(30611):    at android.os.Looper.loop(Looper.java:213)
06-10 14:35:39.444: E/AndroidRuntime(30611):    at android.app.ActivityThread.main(ActivityThread.java:5225)
06-10 14:35:39.444: E/AndroidRuntime(30611):    at java.lang.reflect.Method.invokeNative(Native Method)
06-10 14:35:39.444: E/AndroidRuntime(30611):    at java.lang.reflect.Method.invoke(Method.java:525)
06-10 14:35:39.444: E/AndroidRuntime(30611):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
06-10 14:35:39.444: E/AndroidRuntime(30611):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
06-10 14:35:39.444: E/AndroidRuntime(30611):    at dalvik.system.NativeStart.main(Native Method)
06-10 14:35:39.444: E/AndroidRuntime(30611): Caused by: java.lang.reflect.InvocationTargetException
06-10 14:35:39.444: E/AndroidRuntime(30611):    at java.lang.reflect.Method.invokeNative(Native Method)
06-10 14:35:39.444: E/AndroidRuntime(30611):    at java.lang.reflect.Method.invoke(Method.java:525)
06-10 14:35:39.444: E/AndroidRuntime(30611):    at android.view.View$1.onClick(View.java:3809)
06-10 14:35:39.444: E/AndroidRuntime(30611):    ... 11 more
06-10 14:35:39.444: E/AndroidRuntime(30611): Caused by: java.lang.IllegalStateException
06-10 14:35:39.444: E/AndroidRuntime(30611):    at android.media.MediaPlayer.isPlaying(Native Method)  
06-10 14:35:39.444: E/AndroidRuntime(30611):    at com.example.timestableseasy.Explain1.cont(Explain1.java:38)
06-10 14:35:39.444: E/AndroidRuntime(30611):    ... 14 more

这可能很简单,但我看不出问题出在哪里。

你不应该
释放
MediaPlayer的连接。只需重置
即可

释放(): 此方法释放与MediaPlayer对象连接的任何资源

重置(): 此方法重置媒体播放器

你需要做的是

mp.reset();
mp.setDataSource(/*Code to provide data source*/);
mp.prepare();
mp.start();

请检查此项。

当您尝试获取布尔值mediaPlayer.isplay时,mediaPlayer此时似乎无效。而不是iPlay检查它=空…排序它的thanx。我正在释放声音,所以没有任何剩余的声音