我的应用程序崩溃时遇到问题。当我按下返回按钮或主页按钮时,它有时会崩溃。有时我可以退出应用程序,当我返回它时它会崩溃。我认为这是因为我正在使用线程。我使用了一个在线教程,展示了如何使用图形。它使用表面视图和线程。这是来自 GraphicalMenu.java 的代码。
class Panel extends SurfaceView implements SurfaceHolder.Callback{
private TutorialThread _thread;
public Panel(Context context) {
super(context);
getHolder().addCallback(this);
_thread = new TutorialThread(getHolder(), this);
setFocusable(true);
// TODO Auto-generated constructor stub
}
@Override
public void onDraw(Canvas canvas){
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return true;
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
_thread.setRunning(true);
_thread.start();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
boolean retry = true;
_thread.setRunning(false);
while (retry) {
try {
_thread.join();
retry = false;
} catch (InterruptedException e) {
// we will try it again and again...
}
}
}
}
class TutorialThread extends Thread {
private SurfaceHolder _surfaceHolder;
private Panel _panel;
private boolean _run = false;
public TutorialThread(SurfaceHolder surfaceHolder, Panel panel) {
_surfaceHolder = surfaceHolder;
_panel = panel;
}
public void setRunning(boolean run) {
_run = run;
}
@Override
public void run() {
Canvas c;
while (_run) {
c = null;
try {
c = _surfaceHolder.lockCanvas(null);
synchronized (_surfaceHolder) {
_panel.onDraw(c);
}
} finally {
// do this in a finally so that if an exception is thrown
// during the above, we don't leave the Surface in an
// inconsistent state
if (c != null) {
_surfaceHolder.unlockCanvasAndPost(c);
}
}
}
}
}
这是我在 logcat 中得到的错误和警告
09-01 10:41:39.690: E/global(17293): at com.UWIgem.test2d.Splash$1.run(Splash.java:37)
09-01 10:43:07.920: E/AndroidRuntime(17293): at com.UWIgem.test2d.GraphicalMenu$Panel.surfaceCreated(GraphicalMenu.java:193)
09-01 10:43:07.940: W/ActivityManager(2799): Force finishing activity com.UWIgem.test2d/.GraphicalMenu
09-01 10:43:08.475: W/ActivityManager(2799): Activity pause timeout for ActivityRecord{40757290 com.UWIgem.test2d/.GraphicalMenu}
09-01 10:43:17.879: W/ActivityManager(2799): Activity idle timeout for ActivityRecord{40757290 com.UWIgem.test2d/.GraphicalMenu}
09-01 10:43:18.525: W/WindowManager(2799): Force-removing child win Window{42377460 SurfaceView paused=false} from container Window{42313fe8 com.UWIgem.test2d/com.UWIgem.test2d.GraphicalMenu paused=false}
09-01 10:43:27.885: W/ActivityManager(2799): Activity destroy timeout for ActivityRecord{40757290 com.UWIgem.test2d/.GraphicalMenu}
09-01 10:47:36.930: E/AndroidRuntime(17445): at com.UWIgem.test2d.GraphicalMenu$Panel.surfaceCreated(GraphicalMenu.java:193)
09-01 10:47:36.980: W/ActivityManager(2799): Force finishing activity com.UWIgem.test2d/.GraphicalMenu
09-01 10:47:37.495: W/ActivityManager(2799): Activity pause timeout for ActivityRecord{41c1dcc8 com.UWIgem.test2d/.GraphicalMenu}
09-01 10:47:46.895: W/ActivityManager(2799): Activity idle timeout for ActivityRecord{41c1dcc8 com.UWIgem.test2d/.GraphicalMenu}
09-01 10:47:51.335: W/ActivityManager(2799): Activity idle timeout for ActivityRecord{41634868 com.UWIgem.test2d/.StartMenu}
09-01 10:47:52.259: W/ActivityManager(2799): Activity pause timeout for ActivityRecord{41634868 com.UWIgem.test2d/.StartMenu}
09-01 10:47:57.230: W/ActivityManager(2799): Activity destroy timeout for ActivityRecord{41c1dcc8 com.UWIgem.test2d/.GraphicalMenu}
09-01 10:52:41.354: W/WindowManager(2799): Force-removing child win Window{41a5dbb0 SurfaceView paused=false} from container Window{40b37498 com.UWIgem.test2d/com.UWIgem.test2d.GraphicalMenu paused=false}
我知道这是很多文字,但我总是看到有人在这个网站上要求 logcat 和代码。我不会覆盖 onPause onStart 或 onStop。我需要在这些方法中添加什么?如果您有任何意见,将不胜感激。