Search code examples
androidruntimeexceptiononresume

How to resume paused activity


I have been trying to resume/restart an activity.

Expected Result:-When the user opens App the Menu Activity opens after Splash Screen with its background music.And then one can navigate to other activities.So after moving to other activity and returning back to Menu using back button of phone the background music of Menu must restart.

Problem:-After retuning back music doesn't restart neither does it when pressed home button and back to the app.

What have I done so far:-I tried using onRestart() and onResume(),But both give me Fatal Error. Code of Menu Activity:-

 public class Menu  extends ListActivity {
 MediaPlayer song;

String classes[] = { "MainActivity", "Text", "Email", "Sample", "About",
        "AboutMe", "example6"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setListAdapter(new ArrayAdapter<String>(Menu.this, android.R.layout.simple_expandable_list_item_1, classes));
    song = MediaPlayer.create(Menu.this, R.raw.backmusic);
    song.start();
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    super.onListItemClick(l, v, position, id);
    String cheese = classes[position];
    try{
    Class ourClass = Class.forName("com.stenstudios.secondapp." + cheese);
    Intent ourIntent = new Intent(Menu.this, ourClass);
    startActivity(ourIntent);
}catch(ClassNotFoundException e){
    e.printStackTrace();
}


}
@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    song.release(); 
}

@Override
protected void onRestart() {
    // TODO Auto-generated method stub
    super.onRestart();
    song.start();
}

LogCat:-`

  04-07 15:44:29.500: E/AndroidRuntime(18816): FATAL EXCEPTION: main
  04-07 15:44:29.500: E/AndroidRuntime(18816):Process:com.stenstudios.secondapp, PID: 18816
  04-07 15:44:29.500: E/AndroidRuntime(18816): java.lang.RuntimeException: Unable to resume activity {com.stenstudios.secondapp/com.stenstudios.secondapp.Menu}: java.lang.IllegalStateException
  04-07 15:44:29.500: E/AndroidRuntime(18816):  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2812)
  04-07 15:44:29.500: E/AndroidRuntime(18816):  at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2841)
  04-07 15:44:29.500: E/AndroidRuntime(18816):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
  04-07 15:44:29.500: E/AndroidRuntime(18816):  at android.os.Handler.dispatchMessage(Handler.java:102)
  04-07 15:44:29.500: E/AndroidRuntime(18816):  at android.os.Looper.loop(Looper.java:136)
  04-07 15:44:29.500: E/AndroidRuntime(18816):  at android.app.ActivityThread.main(ActivityThread.java:5146)
  04-07 15:44:29.500: E/AndroidRuntime(18816):  at java.lang.reflect.Method.invokeNative(Native Method)
  04-07 15:44:29.500: E/AndroidRuntime(18816):  at java.lang.reflect.Method.invoke(Method.java:515)
  04-07 15:44:29.500: E/AndroidRuntime(18816):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
  04-07 15:44:29.500: E/AndroidRuntime(18816):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
  04-07 15:44:29.500: E/AndroidRuntime(18816):  at dalvik.system.NativeStart.main(Native Method)
  04-07 15:44:29.500: E/AndroidRuntime(18816): Caused by: java.lang.IllegalStateException
  04-07 15:44:29.500: E/AndroidRuntime(18816):  at android.media.MediaPlayer._start(Native Method)
  04-07 15:44:29.500: E/AndroidRuntime(18816):  at android.media.MediaPlayer.start(MediaPlayer.java:1064)
  04-07 15:44:29.500: E/AndroidRuntime(18816):  at com.stenstudios.secondapp.Menu.onRestart(Menu.java:53)
  04-07 15:44:29.500: E/AndroidRuntime(18816):  at android.app.Instrumentation.callActivityOnRestart(Instrumentation.java:1181)
  04-07 15:44:29.500: E/AndroidRuntime(18816):  at android.app.Activity.performRestart(Activity.java:5291)
  04-07 15:44:29.500: E/AndroidRuntime(18816):  at android.app.Activity.performResume(Activity.java:5302)
  04-07 15:44:29.500: E/AndroidRuntime(18816):  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2802)
  04-07 15:44:29.500: E/AndroidRuntime(18816):  ... 10 more
  04-07 15:44:30.992: I/Process(18816): Sending signal. PID: 18816 SIG: 9

It gives me IllegalStateException.How do I resume mediaplayer as user navigates back to the Menu. Thanks. Update:- Now I don't get the error.Error was due to mediaplayer returned null.But after reinitializing mediaplayer it still doesn't play the song.


Solution

  • check whether your MediaPlayer return null or what after returning to activity... if it returns null then you have to initialize it first and then start...