在某些设备上调用onCreate()活动时,应用程序崩溃

时间:2014-11-09 14:36:55

标签: android methods crash device oncreate

加载屏幕后,我们的应用在某些设备上崩溃了。 它在我们的测试设备上运行顺畅,因此我们无法为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);    

1 个答案:

答案 0 :(得分:0)

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

display.getSize(size);

我不知道为什么。对我来说

DisplayMetrics metrics = context.getResources().getDisplayMetrics();
int width = metrics.widthPixels;
int height = metrics.heightPixels;

作品。