为什么新意图在第二部分(onTouchEvent 内部)中起作用,但在第一部分(更新())中不起作用。如果我把它放在方法 update() 中(我需要它的地方),它不会做任何事情。我在其中放置了一个日志条目,以查看它是否真的通过了 if/then,它确实通过了。
更新:
好的,现在变得很奇怪。我在这里尝试了所有 3 个答案,但仍然无法正常工作。直到我的手机屏幕熄灭(使用通过 USB 连接的 android)。现在,当我再次打开它时,我看到新活动已经开始!
更新 2:在模拟器中尝试过,它确实工作正常!这怎么可能?
class GameView extends SurfaceView implements SurfaceHolder.Callback {
private MainThread _thread;
public GameView(Context context) {
super(context);
getHolder().addCallback(this);
_thread = new MainThread(getHolder(), this);
setFocusable(true);
}
public void update() {
if (deckPos >= 25) {
Log.v("DEBUG", "finished");
// --- DOESNT WORKS HERE ---
Context context = getContext();
Intent intent = new Intent(context, ScoreScreen.class);
context.startActivity(intent);
// ------------------
}
}
public boolean onTouchEvent(MotionEvent event) {
synchronized (_thread.getSurfaceHolder()) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// --- WORKS HERE ---
Context context = getContext();
Intent intent = new Intent(context, ScoreScreen.class);
context.startActivity(intent);
// ------------------
touchX = (int) event.getX();
touchY = (int) event.getY();
touching = true;
if (cardDispencer.contains(touchX, touchY)) {
holdingCard = true;
}
int t = returnTouchingCardGrid(touchX, touchY);
if (t < gridIDs.length && gridIDs[t] == 0) {
gridIDs[t] = deck[deckPos];
deckPos++;
updateScore();
}
}
if (event.getAction() == MotionEvent.ACTION_MOVE) {
touchX = (int) event.getX();
touchY = (int) event.getY();
}
if (event.getAction() == MotionEvent.ACTION_UP) {
int t = returnTouchingCardGrid((int) event.getX(), (int) event.getY());
if (t < gridIDs.length && gridIDs[t] == 0 && holdingCard) {
gridIDs[t] = deck[deckPos];
deckPos++;
updateScore();
}
touching = false;
holdingCard = false;
}
return true;
}
}
}