如何恢复暂停 activity
How to resume paused activity
我一直在尝试 resume/restart activity。
预期结果:-当用户打开应用程序时,菜单 Activity 在初始屏幕及其背景 music.And 之后打开,然后在移动到其他 activities.So 之后可以导航到其他 activities.So 31=] 并使用 phone 的后退按钮返回菜单,菜单的背景音乐必须重新启动。
问题:-重新调回后音乐不会重新启动,按下主页按钮并返回应用程序时也不会重新启动。
到目前为止我做了什么:-我尝试使用 onRestart() 和 onResume(),但两者都给我致命错误。
菜单代码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
它让我 IllegalStateException.How 当用户导航回菜单时我是否恢复媒体播放器。
谢谢。
更新:-
现在我不明白 error.Error 是因为 mediaplayer 在重新初始化 mediaplayer 后返回 null.But 它仍然没有播放歌曲。
检查您的 MediaPlayer return 是否为 null 或 returning 到 activity 之后的内容...
如果它 return 为 null 那么你必须先初始化它然后开始...
我一直在尝试 resume/restart activity。
预期结果:-当用户打开应用程序时,菜单 Activity 在初始屏幕及其背景 music.And 之后打开,然后在移动到其他 activities.So 之后可以导航到其他 activities.So 31=] 并使用 phone 的后退按钮返回菜单,菜单的背景音乐必须重新启动。
问题:-重新调回后音乐不会重新启动,按下主页按钮并返回应用程序时也不会重新启动。
到目前为止我做了什么:-我尝试使用 onRestart() 和 onResume(),但两者都给我致命错误。 菜单代码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
它让我 IllegalStateException.How 当用户导航回菜单时我是否恢复媒体播放器。 谢谢。 更新:- 现在我不明白 error.Error 是因为 mediaplayer 在重新初始化 mediaplayer 后返回 null.But 它仍然没有播放歌曲。
检查您的 MediaPlayer return 是否为 null 或 returning 到 activity 之后的内容... 如果它 return 为 null 那么你必须先初始化它然后开始...