gpt4 book ai didi

android - 在某些设备上调用 Activity onCreate()时,应用崩溃

转载 作者:行者123 更新时间:2023-12-03 17:25:47 26 4
gpt4 key购买 nike

加载屏幕后,我们的应用在某些设备上崩溃。
它在我们的测试设备上运行顺利,因此我们无法为logcat提供错误。
因此,唯一的问题是调用onCreate方法后,我们的应用在某些设备上崩溃。

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);

setVolumeControlStream(AudioManager.STREAM_MUSIC);

WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
screenWidth = size.x;
screenHeight = size.y;
screenSizeAverage = (screenWidth + screenHeight) / 2;

this.gameStatus = 5;
this.isEnemiesStarted = false;
this.score = 0;
this.playerPos = 0;

prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
bestScore = prefs.getInt("key", 0);

gameView = new GameView(this.getApplicationContext(), this);
gameView.setOnTouchListener(new View.OnTouchListener(){

@Override
public boolean onTouch(View v, MotionEvent event) {

if(event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
if(gameStatus == 1){
if(isEnemiesStarted == false){
isEnemiesStarted = true;
}
if(event.getX() < screenWidth / 2 && event.getY() < screenHeight / 2) {
if(playerPos != 1){
gameView.setPlayerPos(screenSizeAverage / 8, screenSizeAverage / 8);
playerPos = 1;
score++;
gameView.playSound(1);
}
}
else if(event.getX() > screenWidth / 2 && event.getY() < screenHeight / 2) {
if(playerPos != 2){
gameView.setPlayerPos(screenWidth - screenSizeAverage / 8 - gameView.getPlayerWidth(), screenSizeAverage / 8);
playerPos = 2;
score++;
gameView.playSound(1);
}
}
else if(event.getX() < screenWidth / 2 && event.getY() > screenHeight / 2) {
if(playerPos != 3){
gameView.setPlayerPos(screenSizeAverage / 8, screenHeight - screenSizeAverage / 8 - gameView.getPlayerWidth());
playerPos = 3;
score++;
gameView.playSound(1);
}
}
else if(event.getX() > screenWidth / 2 && event.getY() > screenHeight / 2) {
if(playerPos != 4){
gameView.setPlayerPos(screenWidth - screenSizeAverage / 8 - gameView.getPlayerWidth(), screenHeight - screenSizeAverage / 8 - gameView.getPlayerWidth());
playerPos = 4;
score++;
gameView.playSound(1);
}
}
}
}

else if (event.getAction() == android.view.MotionEvent.ACTION_UP) {
if(gameStatus == 3){
if(gameView.getLoadStatus() == 2){
gameView.closeGameOver();
gameView.playSound(2);
}
}
else if(gameStatus == 4){
if(gameView.getLoadStatus() == 1){
gameView.setBestScore(bestScore);
gameView.setLoadStatus(2);
} else if(gameView.getLoadStatus() == 2){
gameView.restartGame();
gameView.setLoadStatus(0);
gameView.playSound(2);
}
}
}

return true;
}

});
setContentView(gameView);

最佳答案

我在三星Galaxy S4 Mini上有同样的问题。问题是

display.getSize(size);

我不知道为什么。为了我
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
int width = metrics.widthPixels;
int height = metrics.heightPixels;

作品。

关于android - 在某些设备上调用 Activity onCreate()时,应用崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26829323/

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