Android onResume 未调用
我正在为 Android 开发一个应用程序,我希望它在您按下主页按钮(onPause)时暂停音乐。这工作正常,但是当我尝试再次启动游戏时,onResume、onRestart、onStart、onRestoreInstanceState 和 onCreate 永远不会被调用,它告诉我应用程序没有响应。 LogCat 中没有显示任何异常...所以我不知道发生了什么。有人对为什么会出现这种情况有任何建议吗?
-编辑- 当应用程序暂停时,我确实在 LogCat 中收到此错误:
04-16 20:09:32.659: ERROR/ActivityManager(66): Reason: Broadcast of Intent { act=android.intent.action.CLOSE_SYSTEM_DIALOGS cmp=com.android. settings/.widget.SettingsAppWidgetProvider (有额外内容) }
我的 onPause() 代码:
public void onPause() {
super.onPause();
panel.mediaPlayer.pause();
panel.thread.running=false;
}
主应用程序线程:
public void run() {
running = true;
while(running) {
//new Canvas.
Canvas c = null;
//Update information
update();
//Draw everything to screen
try {
//Gets the canvas from the surfaceHolder.
c = surfaceHolder.lockCanvas(null);
synchronized(surfaceHolder) {
//draw to the canvas
doDraw(c);
try {
Thread.sleep(gameSpeed);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} finally {
if (c != null) {
surfaceHolder.unlockCanvasAndPost(c);
}
}
}
媒体播放器:
mediaPlayer = new MediaPlayer();
sublime = context.getResources().openRawResourceFd(R.raw.sublime);
try {
mediaPlayer.setLooping(true);
mediaPlayer.setDataSource(sublime.getFileDescriptor(), sublime.getStartOffset(), sublime.getLength());
mediaPlayer.prepare();
} catch (IllegalArgumentException e1) {
} catch (IllegalStateException e1) {
} catch (IOException e1) {
}
mediaPlayer.start();
I'm developing an app for Android, and I want it to pause the music when you press the home button (onPause). That works fine, but then when I try to start up the game again, onResume, onRestart, onStart, onRestoreInstanceState and onCreate are never called and it tells me that the application is not responding. There are no Exceptions shown in the LogCat... So I have no idea what is happening. Does anyone have any suggestions as to why this might be the case?
-EDIT-
I do get this error in the LogCat when the application pauses:
04-16 20:09:32.659: ERROR/ActivityManager(66): Reason: Broadcast of Intent { act=android.intent.action.CLOSE_SYSTEM_DIALOGS cmp=com.android.settings/.widget.SettingsAppWidgetProvider (has extras) }
my onPause() code:
public void onPause() {
super.onPause();
panel.mediaPlayer.pause();
panel.thread.running=false;
}
Main application thread:
public void run() {
running = true;
while(running) {
//new Canvas.
Canvas c = null;
//Update information
update();
//Draw everything to screen
try {
//Gets the canvas from the surfaceHolder.
c = surfaceHolder.lockCanvas(null);
synchronized(surfaceHolder) {
//draw to the canvas
doDraw(c);
try {
Thread.sleep(gameSpeed);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} finally {
if (c != null) {
surfaceHolder.unlockCanvasAndPost(c);
}
}
}
mediaPlayer:
mediaPlayer = new MediaPlayer();
sublime = context.getResources().openRawResourceFd(R.raw.sublime);
try {
mediaPlayer.setLooping(true);
mediaPlayer.setDataSource(sublime.getFileDescriptor(), sublime.getStartOffset(), sublime.getLength());
mediaPlayer.prepare();
} catch (IllegalArgumentException e1) {
} catch (IllegalStateException e1) {
} catch (IOException e1) {
}
mediaPlayer.start();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您收到“应用程序未响应”错误时,这意味着您的活动在主应用程序线程上花费的时间太长。确定您在主应用程序线程上执行的所有工作,并将缓慢的内容迁移到后台线程或 AsyncTask。
When you get an "application not responding" error, that means that your activity has taken too long on the main application thread. Identify all the work that you are doing on the main application thread and migrate the slow stuff to a background thread or
AsyncTask
.我想我可能知道我的错误是什么:当在 SurfaceView 上调用 onSizeChanged 时,它正在运行一些不需要的代码(我猜当您暂停 Activity 并查看其他内容时可能会发生这种情况)...我会检查它出去。谢谢大家!
I think I may know what my error is: It is running some unneeded code when onSizeChanged is called on the SurfaceView (which i guess might happen when you pause the Activity and something else comes in to view)... I'll check it out. Thanks everyone!