我一直试图恢复/重新开始活动。
预期结果: - 当用户打开应用程序时,菜单活动在启动画面后打开,并显示其背景音乐。然后可以导航到其他活动。因此,在移动到其他活动并使用电话的后退按钮返回菜单后菜单的背景音乐必须重新启动。
问题: - 重新调回后音乐没有重启,按下主页按钮并返回应用程序时也没有。
到目前为止我做了什么: - 我尝试使用onRestart()和onResume(),但两者都给我致命错误。 菜单活动代码: -
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。当用户导航回菜单时,我如何恢复媒体播放器。 谢谢。 更新: - 现在我没有得到错误。错误是由于媒体播放器返回null。但是在重新初始化媒体播放器后它仍然没有播放该歌曲。
答案 0 :(得分:1)
检查您的MediaPlayer返回null或返回活动后的内容... 如果它返回null,那么你必须先将它初始化然后再开始......