gpt4 book ai didi

java - Android 切换 Activity 后无法单击按钮

转载 作者:行者123 更新时间:2023-12-01 19:23:45 25 4
gpt4 key购买 nike

我正在将 Activity 从 SurfaceView(游戏内 Activity)切换到 MenuActivity。

切换后,我的听众不再使用我的按钮。


My menu activity
public class MenuActivity extends Activity {

private ImageButton startButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("Switchujem","test");

setContentView(R.layout.main_activity);

initButtons();
}

public void playGame(View view){
Constants.mediaPlayer.stop();
Intent game = new Intent(Constants.context, GameActivity.class);
startActivity(game);
}

private void initButtons(){

//INIT PLAY BUTTON
startButton = (ImageButton) findViewById(R.id.playButton);
startButton.setOnTouchListener(new View.OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
Log.d("Click","ME");
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
// Do something
startButton.setImageResource(R.drawable.play_clicked);
return true;
case MotionEvent.ACTION_UP:
startButton.setImageResource(R.drawable.play);
playGame(startButton.getRootView());
return true;
}
return false;
}
});
}

}

GameView切换方法


public void update(){
....

if(player.isDead()) {
Log.d("DEAD", String.valueOf(player.isDead()));
mediaPlayer.stop();
Intent i = new Intent(this.context, MenuActivity.class);
context.startActivity(i);
thread.setRunning(false);
((GameActivity) context).switchScene();
}
}

GameActivity切换方法

    public void switchScene(){
Intent menu = new Intent(this.getApplicationContext(), MainActivity.class);
startActivity(menu);
}

我开始游戏后我的游戏 Activity 像这样运行:1.MainActivity -> 只需初始化一些常量并运行 MenuActivity2.MenuActivity ->现在一切正常,按钮可以点击3.GameActivity(带有自定义GameView)->当我切换回菜单 Activity 按钮时不可点击。

更新::我终于切换了场景,但切换游戏崩溃了。这是我现在的输出:

W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@151581e
W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@d0bd591
D/Switchujem: test -> My custom output in MENU_ACTIVITY
D/EGL_emulation: eglMakeCurrent: 0xe8faab00: ver 3 0 (tinfo 0xe905a910)
I/mple.doodlejum: Thread[6,tid=30588,WaitingInMainSignalCatcherLoop,Thread*=0xe0f2a800,peer=0x145002b8,"Signal Catcher"]: reacting to signal 3
I/mple.doodlejum: Wrote stack traces to tombstoned

最佳答案

您的回调被错过了,所以当您进入 GameActivity 时,它们被错过了,所以像这样重写 onResume() :

@Override
public void onResume(){
super.onResume();
initButtons();

}

希望这有帮助

关于java - Android 切换 Activity 后无法单击按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59330294/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com