移动到下一个屏幕时,onPause()方法崩溃我的应用程序

时间:2014-06-25 16:13:44

标签: android onresume onpause

我正在做一个简单的应用程序。目前它在提示时播放声音并且工作正常但我想在屏幕锁定时停止声音,允许用户控制声音播放的时间。所以,我找到了一个方法来执行此操作,但现在应用程序在尝试移动屏幕(向后或向前)时崩溃

这是方法

public class Tables1 extends Activity
{
MediaPlayer mysound;
protected boolean active = true;
boolean created;
protected int splashtime = 17000;



@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    setContentView(R.layout.tables1);
    mysound=MediaPlayer.create(Tables1.this, R.raw.part1 );
   created = true;
   Log.d("Exercise", "created = true ");

}
@Override
protected void onPause() {
    // when the screen is about to turn off
    if (ScreenReceiver.wasScreenOn) {
        mysound.pause();
        mysound.release();
        System.out.println("SCREEN TURNED OFF");
    } else {
        // this is when onPause() is called when the screen state has not changed
    }
    super.onPause();
}

@Override
protected void onResume() {
    // only when screen turns on
    if (!ScreenReceiver.wasScreenOn) 
    {

        System.out.println("SCREEN TURNED ON");
    } else {
        // this is when onResume() is called when the screen state has not changed
    }
    super.onResume();
}
@Override
public void onBackPressed() {
}

/*@Override
protected void onDestroy()
{

    super.onDestroy();
    mysound.stop();
    mysound.reset();
    mysound.release();
    created = false;
    mysound = null;
}*/
  public void listen(View view) 
    {

      if(mysound==null)
      {
          mysound=MediaPlayer.create(Tables1.this, R.raw.part2 );
      }
        mysound.setOnCompletionListener(new OnCompletionListener() {
            public void onCompletion(MediaPlayer mysound) {
                // TODO Auto-generated method stub
                mysound.stop();
                mysound.reset();
                //mysound.release();
                Log.d("Exercise", "not sound reset ");
            }

        });   
        mysound.start();

    }
  public void Next(View view) 
    {
      if(mysound==null)
      {
          mysound=MediaPlayer.create(Tables1.this, R.raw.part2 );
      }
      if(mysound.isPlaying())
        {
            mysound.stop();
            mysound.reset();
            mysound.release();
            created = false;
            Log.d("Exercise", "sound destroyed ");

        }
      else
      {
          Log.d("Exercise", "not working ");
      }
        mysound.release();
        created = false;
        Log.d("Exercise", "created = false ");
        Intent i = new Intent();
        i.setClassName("com.example","com.example.timestableseasy.Exercise1");
        startActivity(i); 
    }
  public void home(View view) 
    {
      if(mysound==null)
      {
          mysound=MediaPlayer.create(Tables1.this, R.raw.part2 );
      }
      if(created = true)
      {
          if(mysound.isPlaying())
            {
                mysound.stop();
                mysound.reset();

            }
          mysound.release();
      }

        Intent i = new Intent();
        i.setClassName("com.example","com.example.timestableseasy.Menu2");
        startActivity(i); 
    }


}

在此类中调用的ScreenReceiver类

public class ScreenReceiver extends BroadcastReceiver 
{    
public static boolean wasScreenOn = true;
@Override
public void onReceive(Context context, Intent intent)
{
    if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) 
    {
        wasScreenOn = false;
    } 
    else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) 
    {
        wasScreenOn = true;
    }
}

和日志猫

06-25 17:11:43.065: E/AndroidRuntime(26822): FATAL EXCEPTION: main
06-25 17:11:43.065: E/AndroidRuntime(26822): java.lang.RuntimeException: Unable to pause activity {com.example/com.example.timestableseasy.Tables1}: java.lang.IllegalStateException
06-25 17:11:43.065: E/AndroidRuntime(26822):    at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3159)
06-25 17:11:43.065: E/AndroidRuntime(26822):    at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3114)
06-25 17:11:43.065: E/AndroidRuntime(26822):    at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3092)
06-25 17:11:43.065: E/AndroidRuntime(26822):    at android.app.ActivityThread.access$800(ActivityThread.java:150)
06-25 17:11:43.065: E/AndroidRuntime(26822):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1309)
06-25 17:11:43.065: E/AndroidRuntime(26822):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-25 17:11:43.065: E/AndroidRuntime(26822):    at android.os.Looper.loop(Looper.java:213)
06-25 17:11:43.065: E/AndroidRuntime(26822):    at android.app.ActivityThread.main(ActivityThread.java:5225)
06-25 17:11:43.065: E/AndroidRuntime(26822):    at java.lang.reflect.Method.invokeNative(Native Method)
06-25 17:11:43.065: E/AndroidRuntime(26822):    at java.lang.reflect.Method.invoke(Method.java:525)
06-25 17:11:43.065: E/AndroidRuntime(26822):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
06-25 17:11:43.065: E/AndroidRuntime(26822):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
06-25 17:11:43.065: E/AndroidRuntime(26822):    at dalvik.system.NativeStart.main(Native Method)
06-25 17:11:43.065: E/AndroidRuntime(26822): Caused by: java.lang.IllegalStateException
06-25 17:11:43.065: E/AndroidRuntime(26822):    at android.media.MediaPlayer._pause(Native Method)
06-25 17:11:43.065: E/AndroidRuntime(26822):    at android.media.MediaPlayer.pause(MediaPlayer.java:1108)
06-25 17:11:43.065: E/AndroidRuntime(26822):    at com.example.timestableseasy.Tables1.onPause(Tables1.java:37)
06-25 17:11:43.065: E/AndroidRuntime(26822):    at android.app.Activity.performPause(Activity.java:5235)
06-25 17:11:43.065: E/AndroidRuntime(26822):    at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1233)
06-25 17:11:43.065: E/AndroidRuntime(26822):    at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3145)
06-25 17:11:43.065: E/AndroidRuntime(26822):    ... 12 more
抱怨的第99行是" if(mysound.isPlaying())"但是手头有空检查。

基本上我想知道为什么onPause()方法弄乱了改变屏幕的按钮

0 个答案:

没有答案