2013-12-01 6 views
1

Я пытаюсь сделать понг, как игру. Я слежу за учебником отсюда http://mikeyhogarth.wordpress.com/2010/10/09/how-to-develop-pong-for-android/, но учебник имел фиксированную высоту и ширину для отображения игры. Я пытаюсь изменить код, чтобы найти фактические размеры экрана, и использовать их вместо фиксированных настроек. Таким образом, игра будет масштабироваться независимо от размера экрана. GameState.java - это место, где я помещаю getWidth и GetHeight. Эти две строки были оригинальными строками размера экрана от GameState.Fatal Exception, inflateexception

Оригинал

//screen width and height 
    final int _screenWidth = 300; 
    final int _screenHeight = 420;  

Модифицированный

//screen width and height 
    final int _screenWidth = getWidth(null); 
    final int _screenHeight = getHeight(null); 

MainActivity

import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 

public class MainActivity extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
} 

    @Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

} 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 
<com.dbryant423.classicpong.GameView 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"/> 
</FrameLayout> 

GameView.java

import android.content.Context; 
import android.os.Handler; 
import android.util.AttributeSet; 
import android.view.KeyEvent; 
import android.view.MotionEvent; 
import android.view.SurfaceHolder; 
import android.view.SurfaceView; 

public class GameView extends SurfaceView implements SurfaceHolder.Callback 
{ 
private GameThread _thread; 

public GameView(Context context, AttributeSet attrs) { 
    super(context, attrs); 

    //So we can listen for events... 
    SurfaceHolder holder = getHolder(); 
    holder.addCallback(this); 
    setFocusable(true); 

    //and instantiate the thread 
    _thread = new GameThread(holder, context, new Handler()); 
} 

@Override 
public boolean onKeyDown(int keyCode, KeyEvent msg) { 
    return _thread.getGameState().keyPressed(keyCode, msg); 
} 

//Implemented as part of the SurfaceHolder.Callback interface 
@Override 
public void surfaceChanged(SurfaceHolder holder, int format, int width, 
     int height) { 
    //Mandatory, just swallowing it for this example 

} 

//Implemented as part of the SurfaceHolder.Callback interface 
@Override 
public void surfaceCreated(SurfaceHolder holder) { 
    _thread.start(); 
} 

//Implemented as part of the SurfaceHolder.Callback interface 
@SuppressWarnings("deprecation") 
@Override 
public void surfaceDestroyed(SurfaceHolder holder) { 
    _thread.stop(); 
} 

private float _x = 0; 
private float _y = 0; 

@Override 
public boolean onTouchEvent(MotionEvent event) { 
if (event.getAction() == MotionEvent.ACTION_DOWN) { 
_x = event.getX(); 
_y = event.getY(); 
} 
if (event.getAction() == MotionEvent.ACTION_MOVE) { 
final float xdiff = (_x - event.getX()); 
final float ydiff = (_y - event.getY()); 
_thread.getGameState().surfaceTouched(_x, _y); 

_x = event.getX(); 
_y = event.getY(); 
} 
return true; 
} 
} 

GameThread.java

import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Paint; 
import android.os.Handler; 
import android.view.SurfaceHolder; 

public class GameThread extends Thread { 

/** Handle to the surface manager object we interact with */ 
private SurfaceHolder _surfaceHolder; 
private Paint _paint; 
private GameState _state; 

public GameThread(SurfaceHolder surfaceHolder, Context context, Handler handler) 
{ 
_surfaceHolder = surfaceHolder; 
_paint = new Paint(); 
_state = new GameState(); 
} 

@Override 
public void run() { 
while(true) 
{ 
Canvas canvas = _surfaceHolder.lockCanvas(); 
_state.update(); 
_state.draw(canvas,_paint); 
_surfaceHolder.unlockCanvasAndPost(canvas); 
} 
} 

public GameState getGameState() 
{ 
return _state; 
} 
} 

GameState.java

import android.annotation.TargetApi; 
    import android.content.Context; 
    import android.graphics.Canvas; 
    import android.graphics.Paint; 
    import android.graphics.Point; 
    import android.graphics.Rect; 
    import android.os.Build; 
    import android.view.Display; 
    import android.view.KeyEvent; 
    import android.view.WindowManager; 


    public class GameState { 



@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2) public static int 
getWidth(Context mContext){ 
     int width=0; 
     WindowManager wm = (WindowManager)  mContext.getSystemService(Context.WINDOW_SERVICE); 
     Display display = wm.getDefaultDisplay(); 
     if(Build.VERSION.SDK_INT>12){     
      Point size = new Point(); 
      display.getSize(size); 
      width = size.x; 
     } 
     else{ 
      width = display.getWidth(); // deprecated 
     } 
     return width; } 


    @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2) public static int 
getHeight(Context mContext){ 
     int height=0; 
     WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE); 
     Display display = wm.getDefaultDisplay(); 
     if(Build.VERSION.SDK_INT>12){    
      Point size = new Point(); 
      display.getSize(size); 
      height = size.y; 
     }else{   
      height = display.getHeight(); // deprecated 
     } 
     return height;   } 


    //screen width and height 
    final int _screenWidth = getWidth(null); 
    final int _screenHeight = getHeight(null); 

    //The ball 
    final int _ballSize = 10; 
    int _ballX = 100; int _ballY = 100; 
    int _ballVelocityX = 3;  int _ballVelocityY = 3; 

    //The bats 
    final int _batLength = 75; final int _batHeight = 10; 
    int _topBatX = (_screenWidth/2) - (_batLength/2); 
    final int _topBatY = 20; 
    int _bottomBatX = (_screenWidth/2) - (_batLength/2); 
    final int _bottomBatY = 400; 
    final int _batSpeed = 3; 


    public GameState() 
    { 
    } 

    //The update method 
    public void update() { 

    _ballX += _ballVelocityX; 
    _ballY += _ballVelocityY; 

    //DEATH! 
    if(_ballY > _screenHeight || _ballY < 0)   
    {_ballX = 100; _ballY = 100;}  //Collisions with the sides 

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

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

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

    public boolean keyPressed(int keyCode, KeyEvent msg) 
    { 
    if(keyCode == KeyEvent.KEYCODE_DPAD_LEFT) //left 
    { 
    _topBatX += _batSpeed; _bottomBatX -= _batSpeed; 
    } 

    if(keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) //right 
    { 
    _topBatX -= _batSpeed; _bottomBatX += _batSpeed; 
    } 

    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(255, 255, 255, 255); 

    //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 

    canvas.drawRect(new Rect(505, 500, 500, 0), paint); //Right Wall 
    canvas.drawRect(new Rect(5, 500, 0, 0), paint); //Left Wall 

    } 


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

    return true; 
    } 
    } 

И, наконец, LogCat:

12-01 14:32:43.861: D/AndroidRuntime(2318): Shutting down VM 
12-01 14:32:43.861: W/dalvikvm(2318): threadid=1: thread exiting with uncaught exception (group=0x415c3b90) 
12-01 14:32:43.871: E/AndroidRuntime(2318): FATAL EXCEPTION: main 
12-01 14:32:43.871: E/AndroidRuntime(2318): Process: com.dbryant423.classicpong, PID: 2318 
12-01 14:32:43.871: E/AndroidRuntime(2318): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dbryant423.classicpong/com.dbryant423.classicpong.MainActivity}: android.view.InflateException: Binary XML file line #5: Error inflating class com.dbryant423.classicpong.GameView 
12-01 14:32:43.871: E/AndroidRuntime(2318):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176) 
12-01 14:32:43.871: E/AndroidRuntime(2318):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226) 
12-01 14:32:43.871: E/AndroidRuntime(2318):  at android.app.ActivityThread.access$700(ActivityThread.java:135) 
12-01 14:32:43.871: E/AndroidRuntime(2318):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397) 
12-01 14:32:43.871: E/AndroidRuntime(2318):  at android.os.Handler.dispatchMessage(Handler.java:102) 
12-01 14:32:43.871: E/AndroidRuntime(2318):  at android.os.Looper.loop(Looper.java:137) 
12-01 14:32:43.871: E/AndroidRuntime(2318):  at android.app.ActivityThread.main(ActivityThread.java:4998) 
12-01 14:32:43.871: E/AndroidRuntime(2318):  at java.lang.reflect.Method.invokeNative(Native Method) 
12-01 14:32:43.871: E/AndroidRuntime(2318):  at java.lang.reflect.Method.invoke(Method.java:515) 
12-01 14:32:43.871: E/AndroidRuntime(2318):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777) 
12-01 14:32:43.871: E/AndroidRuntime(2318):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593) 
12-01 14:32:43.871: E/AndroidRuntime(2318):  at dalvik.system.NativeStart.main(Native Method) 
12-01 14:32:43.871: E/AndroidRuntime(2318): Caused by: android.view.InflateException: Binary XML file line #5: Error inflating class com.dbryant423.classicpong.GameView 
12-01 14:32:43.871: E/AndroidRuntime(2318):  at android.view.LayoutInflater.createView(LayoutInflater.java:620) 
12-01 14:32:43.871: E/AndroidRuntime(2318):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696) 
12-01 14:32:43.871: E/AndroidRuntime(2318):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:755) 
12-01 14:32:43.871: E/AndroidRuntime(2318):  at android.view.LayoutInflater.inflate(LayoutInflater.java:492) 
12-01 14:32:43.871: E/AndroidRuntime(2318):  at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 
12-01 14:32:43.871: E/AndroidRuntime(2318):  at android.view.LayoutInflater.inflate(LayoutInflater.java:353) 
12-01 14:32:43.871: E/AndroidRuntime(2318):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290) 
12-01 14:32:43.871: E/AndroidRuntime(2318):  at android.app.Activity.setContentView(Activity.java:1928) 
12-01 14:32:43.871: E/AndroidRuntime(2318):  at com.dbryant423.classicpong.MainActivity.onCreate(MainActivity.java:12) 
12-01 14:32:43.871: E/AndroidRuntime(2318):  at android.app.Activity.performCreate(Activity.java:5243) 
12-01 14:32:43.871: E/AndroidRuntime(2318):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
12-01 14:32:43.871: E/AndroidRuntime(2318):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140) 
12-01 14:32:43.871: E/AndroidRuntime(2318):  ... 11 more 
12-01 14:32:43.871: E/AndroidRuntime(2318): Caused by: java.lang.reflect.InvocationTargetException 
12-01 14:32:43.871: E/AndroidRuntime(2318):  at java.lang.reflect.Constructor.constructNative(Native Method) 
12-01 14:32:43.871: E/AndroidRuntime(2318):  at java.lang.reflect.Constructor.newInstance(Constructor.java:423) 
12-01 14:32:43.871: E/AndroidRuntime(2318):  at android.view.LayoutInflater.createView(LayoutInflater.java:594) 
12-01 14:32:43.871: E/AndroidRuntime(2318):  ... 22 more 
12-01 14:32:43.871: E/AndroidRuntime(2318): Caused by: java.lang.NullPointerException 
12-01 14:32:43.871: E/AndroidRuntime(2318):  at com.dbryant423.classicpong.GameState.getWidth(GameState.java:20) 
12-01 14:32:43.871: E/AndroidRuntime(2318):  at com.dbryant423.classicpong.GameState.<init>(GameState.java:50) 
12-01 14:32:43.871: E/AndroidRuntime(2318):  at com.dbryant423.classicpong.GameThread.<init>(GameThread.java:20) 
12-01 14:32:43.871: E/AndroidRuntime(2318):  at com.dbryant423.classicpong.GameView.<init>(GameView.java:24) 
12-01 14:32:43.871: E/AndroidRuntime(2318):  ... 25 more 
12-01 14:32:46.613: I/Process(2318): Sending signal. PID: 2318 SIG: 9 

ответ

0

Ошибка здесь

<com.dbryant423.classicpong.GameView 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"/> 

Вы добавили свою активность в манифест?

<activity android:name=".GameView"> 
    <intent-filter> 
     <category android:name="android.intent.category.GAMEVIEW"></category> 
     <action android:name="android.intent.action.DEFAULT"></action> 
    </intent-filter> 
</activity> 
+0

Я добавил его к манифесту. У вас есть предложение о том, как исправить остальное? когда я переключаюсь на графическое представление для activity_main.xml, я получаю com.dbryant423.classicpong.GameView не удалось создать экземпляр – poboy975

+0

, вы должны использовать View.isInEditMode() –

+0

Спасибо, что помогает видеть во время редактирования в eclipse. Но ничто из этого не решает проблемы, которые отображаются неправильно. Вы говорите мне, где ошибка, но вы не даете мне решения для исправления ошибки. – poboy975