我需要一些帮助,因为当我想应用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)不适用于参数(显示)
并且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);
}
}
答案 0 :(得分:1)
在创建DisplayManager
之前,您无法使用Activity
获取所需的任何信息。
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;
}
答案 1 :(得分:1)
在屏幕有焦点之前,你不会得到宽度和高度。试试这个:
@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;
}