gpt4 book ai didi

android - 应用程序在 getHeight 和 getWidth 上崩溃

转载 作者:行者123 更新时间:2023-11-29 21:47:32 24 4
gpt4 key购买 nike

我需要一些帮助,因为当我想应用 display.getWidth()display.getHeight() 时,Eclipse 中没有错误,但是当我想在虚拟设备上启动它,应用程序崩溃(应用程序意外停止...)

public class GameState extends Activity {

//Largeur et hauteur de l'écran
Display display = getWindowManager().getDefaultDisplay();
int _screenWidth = display.getWidth();
int _screenHeight = display.getHeight();


//La balle
final int _ballSize = 10;
int _ballX = _screenWidth/2; int _ballY = _screenHeight/2;
int _ballVelocityX = 2; int _ballVelocityY = 4;

//Les barres
final int _batLength = 75; final int _batHeight = 10;
int _topBatX = (_screenWidth/2) - (_batLength / 2);
final int _topBatY = 10;
int _bottomBatX = (_screenWidth/2) - (_batLength / 2);
final int _bottomBatY = _screenHeight - 20;

等等

提前致谢(我是法语所以请使用简单的词)

编辑 1

像这样?

public class GameState extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Display display = getWindowManager().getDefaultDisplay();
setContentView(display);}

//Largeur et hauteur de l'écran
int _screenWidth = display.getWidth();
int _screenHeight = display.getHeight();


//La balle
final int _ballSize = 10;
int _ballX = _screenWidth/2; int _ballY = _screenHeight/2;
int _ballVelocityX = 2; int _ballVelocityY = 4;

这里我得到 3 个错误:Activity 类型中的方法 setContentView(int) 不适用于参数 (Display)

还有2次:显示无法解析

    03-14 16:29:44.655: E/AndroidRuntime(538): FATAL EXCEPTION: main
03-14 16:29:44.655: E/AndroidRuntime(538): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nicmer.pong/com.salocincreations.pong.P1_vs_P1}: android.view.InflateException: Binary XML file line #8: Error inflating class com.salocincreations.pong.GameView
03-14 16:29:44.655: E/AndroidRuntime(538): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
03-14 16:29:44.655: E/AndroidRuntime(538): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
03-14 16:29:44.655: E/AndroidRuntime(538): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-14 16:29:44.655: E/AndroidRuntime(538): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
03-14 16:29:44.655: E/AndroidRuntime(538): at android.os.Handler.dispatchMessage(Handler.java:99)
03-14 16:29:44.655: E/AndroidRuntime(538): at android.os.Looper.loop(Looper.java:123)
03-14 16:29:44.655: E/AndroidRuntime(538): at android.app.ActivityThread.main(ActivityThread.java:3683)
03-14 16:29:44.655: E/AndroidRuntime(538): at java.lang.reflect.Method.invokeNative(Native Method)
03-14 16:29:44.655: E/AndroidRuntime(538): at java.lang.reflect.Method.invoke(Method.java:507)
03-14 16:29:44.655: E/AndroidRuntime(538): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-14 16:29:44.655: E/AndroidRuntime(538): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-14 16:29:44.655: E/AndroidRuntime(538): at dalvik.system.NativeStart.main(Native Method)
03-14 16:29:44.655: E/AndroidRuntime(538): Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class com.salocincreations.pong.GameView
03-14 16:29:44.655: E/AndroidRuntime(538): at android.view.LayoutInflater.createView(LayoutInflater.java:518)
03-14 16:29:44.655: E/AndroidRuntime(538): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:570)
03-14 16:29:44.655: E/AndroidRuntime(538): at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
03-14 16:29:44.655: E/AndroidRuntime(538): at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
03-14 16:29:44.655: E/AndroidRuntime(538): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
03-14 16:29:44.655: E/AndroidRuntime(538): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
03-14 16:29:44.655: E/AndroidRuntime(538): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
03-14 16:29:44.655: E/AndroidRuntime(538): at android.app.Activity.setContentView(Activity.java:1657)
03-14 16:29:44.655: E/AndroidRuntime(538): at com.salocincreations.pong.P1_vs_P1.onCreate(P1_vs_P1.java:20)
03-14 16:29:44.655: E/AndroidRuntime(538): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-14 16:29:44.655: E/AndroidRuntime(538): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
03-14 16:29:44.655: E/AndroidRuntime(538): ... 11 more
03-14 16:29:44.655: E/AndroidRuntime(538): Caused by: java.lang.reflect.InvocationTargetException
03-14 16:29:44.655: E/AndroidRuntime(538): at java.lang.reflect.Constructor.constructNative(Native Method)
03-14 16:29:44.655: E/AndroidRuntime(538): at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
03-14 16:29:44.655: E/AndroidRuntime(538): at android.view.LayoutInflater.createView(LayoutInflater.java:505)
03-14 16:29:44.655: E/AndroidRuntime(538): ... 21 more
03-14 16:29:44.655: E/AndroidRuntime(538): Caused by: java.lang.NullPointerException
03-14 16:29:44.655: E/AndroidRuntime(538): at com.salocincreations.pong.GameState.<init>(GameState.java:12)
03-14 16:29:44.655: E/AndroidRuntime(538): at com.salocincreations.pong.GameThread.<init>(GameThread.java:19)
03-14 16:29:44.655: E/AndroidRuntime(538): at com.salocincreations.pong.GameView.<init>(GameView.java:23)
03-14 16:29:44.655: E/AndroidRuntime(538): ... 24 more

编辑 2

package com.salocincreations.pong;

import android.app.Activity;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;

public class GameState extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.p1_vs_p1);

Display display = getWindowManager().getDefaultDisplay();
int _screenWidth = display.getWidth();
int _screenHeight = display.getHeight();


//La balle
final int _ballSize = 10;
int _ballX = _screenWidth/2; int _ballY = _screenHeight/2;
int _ballVelocityX = 2; int _ballVelocityY = 4;

//Les barres
final int _batLength = 75; final int _batHeight = 10;
int _topBatX = (_screenWidth/2) - (_batLength / 2);
final int _topBatY = 10;
int _bottomBatX = (_screenWidth/2) - (_batLength / 2);
final int _bottomBatY = _screenHeight - 20;

public GameState()
{
}

//The update method
public void update() {

_ballX += _ballVelocityX;
_ballY += _ballVelocityY;

//DEATH!
if(_ballY > _bottomBatY + 10 || _ballY < 0)
{_ballX = 100; _ballY = 100;}//Collisions with the goals

if(_ballX > _screenWidth || _ballX < 0)
_ballVelocityX *= -1; //Collisions with the sides

if(_ballX > _topBatX && _ballX < _topBatX+_batLength && _ballY - 16 < _topBatY)
_ballVelocityY *= -1; //Collisions with the bats

if(_ballX > _bottomBatX && _ballX < _bottomBatX+_batLength
&& _ballY + 16 > _bottomBatY)
_ballVelocityY *= -1;
}

public boolean surfaceTouched(float posX, float posY) {
_topBatX = (int) posX;
_bottomBatX = (int) posX;

return true;
}


//the draw method
public void draw(Canvas canvas, Paint paint) {

//Clear the screen
canvas.drawRGB(0, 0, 0);

//set the colour
paint.setARGB(200, 0, 200, 700);

//draw the ball
canvas.drawRect(new Rect(_ballX,_ballY,_ballX + _ballSize,_ballY + _ballSize),
paint);

//draw the bats
canvas.drawRect(new Rect(_topBatX, _topBatY, _topBatX + _batLength,
_topBatY + _batHeight), paint); //top bat
canvas.drawRect(new Rect(_bottomBatX, _bottomBatY, _bottomBatX + _batLength,
_bottomBatY + _batHeight), paint); //bottom bat

// Nous allons dessiner nos points par rapport à la résolution de l'écran
int iWidth = canvas.getWidth(); // Largeur
int iHeight = canvas.getHeight(); // Hauteur

// Affecter une couleur de manière aléatoire
paint.setARGB(255, 500, 500, 500);
// Définir l'épaisseur du segment
paint.setStrokeWidth (2);
// Puis dessiner nos points dans le cavenas
canvas.drawLine(0, iHeight/2, iWidth, iHeight/2, paint);
canvas.drawCircle(iWidth/2, iHeight/2, 50, paint);
}
}

编辑 3

package com.salocincreations.pong;

import android.app.Activity;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;

public class GameState extends Activity {
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
Display display = getWindowManager().getDefaultDisplay();
int _screenWidth = display.getWidth();
int _screenHeight = display.getHeight();;


//La balle
final int _ballSize = 10;
int _ballX = _screenWidth/2; int _ballY = _screenHeight/2;
int _ballVelocityX = 2; int _ballVelocityY = 4;

//Les barres
final int _batLength = 75; final int _batHeight = 10;
int _topBatX = (_screenWidth/2) - (_batLength / 2);
final int _topBatY = 10;
int _bottomBatX = (_screenWidth/2) - (_batLength / 2);
final int _bottomBatY = _screenHeight - 20;

public GameState()
{
}

//The update method
public void update() {

_ballX += _ballVelocityX;
_ballY += _ballVelocityY;

//DEATH!
if(_ballY > _bottomBatY + 10 || _ballY < 0)
{_ballX = 100; _ballY = 100;}//Collisions with the goals

if(_ballX > _screenWidth || _ballX < 0)
_ballVelocityX *= -1; //Collisions with the sides

if(_ballX > _topBatX && _ballX < _topBatX+_batLength && _ballY - 16 < _topBatY)
_ballVelocityY *= -1; //Collisions with the bats

if(_ballX > _bottomBatX && _ballX < _bottomBatX+_batLength
&& _ballY + 16 > _bottomBatY)
_ballVelocityY *= -1;
}

public boolean surfaceTouched(float posX, float posY) {
_topBatX = (int) posX;
_bottomBatX = (int) posX;

return true;
}


//the draw method
public void draw(Canvas canvas, Paint paint) {

//Clear the screen
canvas.drawRGB(0, 0, 0);

//set the colour
paint.setARGB(200, 0, 200, 700);

//draw the ball
canvas.drawRect(new Rect(_ballX,_ballY,_ballX + _ballSize,_ballY + _ballSize),
paint);

//draw the bats
canvas.drawRect(new Rect(_topBatX, _topBatY, _topBatX + _batLength,
_topBatY + _batHeight), paint); //top bat
canvas.drawRect(new Rect(_bottomBatX, _bottomBatY, _bottomBatX + _batLength,
_bottomBatY + _batHeight), paint); //bottom bat

// Nous allons dessiner nos points par rapport à la résolution de l'écran
int iWidth = canvas.getWidth(); // Largeur
int iHeight = canvas.getHeight(); // Hauteur

// Affecter une couleur de manière aléatoire
paint.setARGB(255, 500, 500, 500);
// Définir l'épaisseur du segment
paint.setStrokeWidth (2);
// Puis dessiner nos points dans le cavenas
canvas.drawLine(0, iHeight/2, iWidth, iHeight/2, paint);
canvas.drawCircle(iWidth/2, iHeight/2, 50, paint);
}
}

最佳答案

在创建 Activity 之前,您不能使用 DisplayManager 获取任何您想要的信息。

public class GameState extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);

Display display = getWindowManager().getDefaultDisplay();
int _screenWidth = display.getWidth();
int _screenHeight = display.getHeight();


//La balle
final int _ballSize = 10;
int _ballX = _screenWidth/2; int _ballY = _screenHeight/2;
int _ballVelocityX = 2; int _ballVelocityY = 4;

//Les barres
final int _batLength = 75; final int _batHeight = 10;
int _topBatX = (_screenWidth/2) - (_batLength / 2);
final int _topBatY = 10;
int _bottomBatX = (_screenWidth/2) - (_batLength / 2);
final int _bottomBatY = _screenHeight - 20;

}

关于android - 应用程序在 getHeight 和 getWidth 上崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15414945/

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