我是安卓新手。目前正在为Android构建一个简单的游戏。
我的问题是当按下主页按钮时,它会调用 onPause(),有时它会同时调用 onPause() 和 onStop()。但是,当我尝试返回游戏(通过单击主屏幕中的图标)时,既不会调用 onResume() 也不会调用 onRestart()。游戏一直在渲染,但我无法与游戏互动。触摸事件不起作用。当我尝试触摸按钮时,什么也没有发生,几秒钟后,它说“没有响应”,然后我必须强制关闭。
顺便提一句。我还注意到,当转到主屏幕时,会调用 MainGamePanel 中的 surfaceDestroyed() 函数。我以为这个函数会阻塞线程,但实际上线程在主屏幕时仍在运行。
有人可以告诉我为什么 onRestart() 或 onResume() 没有触发以及为什么从主屏幕返回游戏后触摸事件不起作用?
非常感谢。
我重写 onStop()、onPause()、onResume 和 onRestart() 以在调用函数时在日志中输出一条消息:
@Override
protected void onStop() {
Log.d(TAG, "Stopping...");
super.onStop();
}
下面是我的活动和表面视图的代码。我有一个 ActivityMainGame 扩展了 Activity:
public class ActivityMainGame extends Activity
{
private MainGamePanel mainGamePanel;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_game);
Log.w(TAG, "Creating the game");
//set our MainGamePanel as the view
mainGamePanel = new MainGamePanel(this);
setContentView(mainGamePanel);
}
}
我创建了一个 MainGamePanel 扩展了 SurfaceView 实现了 SurfaceHolder.Callback,这个类启动了一个线程来在画布上渲染东西:
public class MainGamePanel extends SurfaceView implements SurfaceHolder.Callback
{
private MainGameThread thread;
public MainGamePanel(Context context)
{
super(context);
// adding the callback (this) to the surface holder to intercept events
getHolder().addCallback(this);
// create the game loop thread
thread = new MainGameThread(getHolder(), this);
// make the GamePanel focusable so it can handle events
setFocusable(true);
}
@Override
public void surfaceCreated(SurfaceHolder holder)
{
if (thread.getState() == Thread.State.NEW)
{
thread.setRunning(true);
thread.start();
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder)
{
boolean retry = true;
while (retry)
{
try
{
thread.join();
retry = false;
}
catch (InterruptedException e)
{
Log.d(TAG, e.getMessage());
}
}
}
应阿迪尔的要求,我将错误信息放在 logcat 中。这些是我的游戏被强制关闭时的消息:
03-11 15:04:21.491: E/ActivityManager(58): ANR in com.myslotgame (com.myslotgame /.ActivityMainGame)
03-11 15:04:21.491: E/ActivityManager(58): Reason: keyDispatchingTimedOut
03-11 15:04:21.491: E/ActivityManager(58): Load: 0.52 / 0.19 / 0.18
03-11 15:04:21.491: E/ActivityManager(58): CPU usage from 551359ms to 51ms ago:
03-11 15:04:21.491: E/ActivityManager(58): system_server: 4% = 3% user + 0% kernel / faults: 5766 minor
03-11 15:04:21.491: E/ActivityManager(58): adbd: 0% = 0% user + 0% kernel / faults: 55 minor
03-11 15:04:21.491: E/ActivityManager(58): m.android.phone: 0% = 0% user + 0% kernel / faults: 17 minor
03-11 15:04:21.491: E/ActivityManager(58): ndroid.launcher: 0% = 0% user + 0% kernel / faults: 1625 minor
03-11 15:04:21.491: E/ActivityManager(58): qemud: 0% = 0% user + 0% kernel
03-11 15:04:21.491: E/ActivityManager(58): events/0: 0% = 0% user + 0% kernel
03-11 15:04:21.491: E/ActivityManager(58): zygote: 0% = 0% user + 0% kernel / faults: 1201 minor
03-11 15:04:21.491: E/ActivityManager(58): id.defcontainer: 0% = 0% user + 0% kernel / faults: 77 minor
03-11 15:04:21.491: E/ActivityManager(58): logcat: 0% = 0% user + 0% kernel
03-11 15:04:21.491: E/ActivityManager(58): rild: 0% = 0% user + 0% kernel
03-11 15:04:21.491: E/ActivityManager(58): ndroid.settings: 0% = 0% user + 0% kernel / faults: 9 minor
03-11 15:04:21.491: E/ActivityManager(58): roid.alarmclock: 0% = 0% user + 0% kernel / faults: 14 minor
03-11 15:04:21.491: E/ActivityManager(58): re-initialized>: 0% = 0% user + 0% kernel / faults: 7 minor
03-11 15:04:21.491: E/ActivityManager(58): d.process.media: 0% = 0% user + 0% kernel / faults: 9 minor
03-11 15:04:21.491: E/ActivityManager(58): com.android.mms: 0% = 0% user + 0% kernel / faults: 16 minor
03-11 15:04:21.491: E/ActivityManager(58): +myslotgame: 0% = 0% user + 0% kernel
03-11 15:04:21.491: E/ActivityManager(58): -myslotgame: 0% = 0% user + 0% kernel
03-11 15:04:21.491: E/ActivityManager(58): TOTAL: 7% = 6% user + 1% kernel + 0% irq + 0% softirq