重新启动活动以IllegalStateException结束

时间:2015-09-15 06:27:14

标签: java android

我正在尝试暂停闹钟,按下暂停按钮会在预定义的时间后重新启动相同的活动。但是,当我按下贪睡按钮时,应用程序崩溃,并且在logcat中我收到错误:java.lang.IllegalStateException。这是我用来打盹的代码,帮助我找到我做错的地方以及修复它的最佳替代方案。谢谢,

  public void snoozeup(View view)
{
    SharedPreferences sa = getSharedPreferences("SnoozeList", Context.MODE_PRIVATE);
    int snox = sa.getInt("SnoozeX",0);

    if(snox==2) {

        Handler handler = new Handler();
        Runnable x = new Runnable() {
            @Override
            public void run()
            {
                startActivity(new Intent(Time_Date.this, Time_Date.class));
            }
        };
        handler.postDelayed(x, 120000);

    }

    else if(snox==3) {

        Handler handler = new Handler();
        Runnable x = new Runnable() {
            @Override
            public void run() {
                startActivity(new Intent(Time_Date.this, Time_Date.class));
            }
        };
        handler.postDelayed(x, 180000);

    }
    else if(snox==5) {

        Handler handler = new Handler();
        Runnable x = new Runnable() {
            @Override
            public void run() {
                startActivity(new Intent(Time_Date.this, Time_Date.class));
            }
        };
        handler.postDelayed(x, 300000);

    }
    else if(snox==10) {

        Handler handler = new Handler();
        Runnable x = new Runnable() {
            @Override
            public void run() {
                startActivity(new Intent(Time_Date.this, Time_Date.class));
            }
        };
        handler.postDelayed(x, 600000);

    }
    else
    {
        Handler handler = new Handler();
        Runnable x = new Runnable() {
            @Override
            public void run() {
                startActivity(new Intent(Time_Date.this, Time_Date.class));
            }
        };
        handler.postDelayed(x, 180000);

    }

    mp.stop();
    mp.release();
    voicePlayer.stop();
    voicePlayer.release();
    songPlayer.stop();
    songPlayer.release();



    finish();
}

这是完整的堆栈跟踪:

> java.lang.IllegalStateException
            at android.media.MediaPlayer.isPlaying(Native Method)
            at zyia.alarm.zyia.zyia.Time_Date$4.run(Time_Date.java:199)
            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:5254)
            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)
09-15 11:49:25.805    1793-1793/zyia.alarm.zyia.zyia I/Process﹕ Sending signal. PID: 1793 SIG: 9

此外,我在此活动中使用了isPlaying()方法,以下是这样的:

 try {
        final Handler hlr = new Handler();

        final Runnable looper = new Runnable() {
            @Override
            public void run() {

                if (mp != null) {

                    if (mp.isPlaying()) {
                        mp.stop();
                    }

                }


                mp.start();

            }
        };


        mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            public void onCompletion(MediaPlayer mp) {
                hlr.postDelayed(looper, 5000);
            }
        });

    }
    catch(IllegalStateException e)
    {
        e.printStackTrace();

    }

3 个答案:

答案 0 :(得分:1)

也适用于这些行的isPlaying()条件

mp.stop();
mp.release();
voicePlayer.stop();
voicePlayer.release();
songPlayer.stop();
songPlayer.release();
像这样

if(mp.isPlaying()) {
   mp.stop();
}

答案 1 :(得分:1)

  

假设您的mp已处于stop状态并且您正在尝试   再次stop,以便获得java.lang.IllegalStateException

还要检查您的资源是否null不是mp,voicePlayer,songPlayer

  

您应该在isPlaying()

之前检查stop()

做检查:

      try {
             if (mp != null && mp.isPlaying()) {
                Log.d("TAG------->", "mp is running");
                mp.stop();
                Log.d("TAG------->", "mp is stopped");
                mp.release();
                Log.d("TAG------->", "mp is released");

            }
        } catch (Exception e) {
            e.printStackTrace();
        }

答案 2 :(得分:0)

您正在调用stop()方法而不检查它是否正在播放。如果它没有播放,你调用stop()方法比发生illegalStateException 检查mediaPlayer是否实例isPlaying()

if(mp.isPlaying()){
    mp.stop();
}