2016-10-05 6 views
-4

나는 안드로이드 스튜디오 libgdx에서 게임에 노력하고있다 그리고 나는 내 물리적 장치에서 실행 때마다 그것은 "[프로그램 응용]이 중지되었습니다"라고.실제 장치에 "[AppName] has stopped"이 있습니다. 로그 캣에서 ERROR : java.lang.NullPointerException이

나는 문제가 무엇인지 전혀 모르는 것보다 다른 내가 사용되는 이미지 (철자, 대문자)의 이름을 확인했다. 여기

10-06 12:01:51.055 21591-21627/com.jpfalmazan.ninjaassault E/AndroidRuntime: FATAL EXCEPTION: GLThread 2891 
                      java.lang.NullPointerException 
                       at com.jpfalmazan.ninjaassault.GameScreens.MenuScreen.<init>(MenuScreen.java:23) 
                       at com.jpfalmazan.ninjaassault.NinjaAssault.create(NinjaAssault.java:19) 
                       at com.badlogic.gdx.backends.android.AndroidGraphics.onSurfaceChanged(AndroidGraphics.java:275) 
                       at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1505) 
                       at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1240) 

게임 내 코드입니다 :

package com.jpfalmazan.ninjaassault; 

import com.badlogic.gdx.Game; 

import com.badlogic.gdx.Gdx; 
import com.badlogic.gdx.graphics.GL20; 
import com.badlogic.gdx.graphics.Texture; 
import com.badlogic.gdx.graphics.g2d.SpriteBatch; 
import com.jpfalmazan.ninjaassault.GameScreens.MenuScreen; 


public class NinjaAssault extends Game { 
    public SpriteBatch batch; 
    Texture img; 

    @Override 
    public void create() { 
     batch = new SpriteBatch(); 
     this.setScreen(new MenuScreen(this)); ////This is the "NinjaAssault.java:19 " error shown in logcat 
    } 

    @Override 
    public void render() { 
     super.render(); 
     Gdx.gl.glClearColor(1,0,0,1); 
     Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 
     batch.begin(); 
    } 

    @Override 
    public void dispose() { 
     batch.dispose(); 
     img.dispose(); 
    } 
} 

그리고 여기 MenuScreen 클래스 내 코드입니다 :

package com.jpfalmazan.ninjaassault.GameScreens; 

import com.badlogic.gdx.Gdx; 
import com.badlogic.gdx.Screen; 
import com.badlogic.gdx.graphics.GL20; 
import com.badlogic.gdx.graphics.Texture; 
import com.badlogic.gdx.graphics.g2d.SpriteBatch; 
import com.jpfalmazan.ninjaassault.NinjaAssault; 

public class MenuScreen implements Screen{ 
    NinjaAssault game; 
    public MenuScreen (NinjaAssault game){ 
     this.game = game; 
    } 
    public SpriteBatch batch; 
    public Texture background; 
    public Texture title; 
    public Texture playButtonINACTIVE; 
    public Texture playButtonACTIVE; 

    public float screenWidth = Gdx.graphics.getWidth(); 
    public float screenHeight = Gdx.graphics.getHeight(); 
    public float titleWidth = title.getWidth(); //This is the "MenuScreen.java:23" error shown in logcat 
    public float titleHeight = title.getHeight(); 
    public float xLimit1 = (float) ((screenWidth/2)-(titleWidth/2)); 
    public float xLimit2 = (float) ((screenWidth/2)+(titleWidth/2)); 
    public float yLimit1 = (float) ((screenHeight*.35)); 
    public float yLimit2 = (float) ((screenHeight*.35)+titleHeight); 



    @Override 
    public void show() { 
     batch = new SpriteBatch(); 
     background = new Texture("BGYELL.png"); 
     title = new Texture ("BGASTRO.png"); 
     playButtonINACTIVE = new Texture("BTPLAY.png"); 
     playButtonACTIVE = new Texture("BTPLAYON.png"); 

    } 

    @Override 
    public void render(float delta) { 
     Gdx.gl.glClearColor(1,0,0,1); 
     Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 
     game.batch.begin(); 
     game.batch.draw(background,0,0,screenWidth,screenHeight); 
     if((Gdx.input.getX()> xLimit1 && Gdx.input.getX() < xLimit2) && (Gdx.input.getY() < yLimit2 && Gdx.input.getY() > yLimit1)){ 
      game.batch.draw(playButtonACTIVE,(screenWidth/2)-(playButtonACTIVE.getWidth()/2), (float) (screenHeight*.35)); 
     }else game.batch.draw(playButtonINACTIVE,(screenWidth/2)-(playButtonINACTIVE.getWidth()/2), (float) (screenHeight*.35)); 
     game.batch.draw(title,(screenWidth/2)-((float)(screenWidth*.8)/2), (float) (screenHeight*.65),(float)(screenWidth*.8) , (float) ((screenWidth*.8)*(titleHeight/titleWidth))); 




     game.batch.end(); 
    } 

    @Override 
    public void resize(int width, int height) { 

    } 

    @Override 
    public void pause() { 

    } 

    @Override 
    public void resume() { 

    } 

    @Override 
    public void hide() { 

    } 

    @Override 
    public void dispose() { 

    } 
} 

내가 로그 캣에서받은 오류입니다
+0

당신은 할당되지 않은 인스턴스 변수에 대한 메소드를 호출 할 수 없습니다. 'title' 내가 그런 실수는 쉽게 간과 할 수 있도록 내가 자바 프로그래밍 초보자입니다 말했듯이 분명 –

+0

null입니다. 응답 주셔서 감사합니다. – JAlmazan

답변

1

당신은 아직 아무것도에 "제목"을 설정하지 않았습니다. 당신은 그것이 존재한다고 단언했습니다. title은 생성자 다음에 호출되는 show()에 설정됩니다. 당신은 질감의 높이를 취득하기 전에 당신은

title = new Texture ("BGASTRO.png"); 

할 필요가있다. 이것은 다른 모든 변수에도 적용됩니다. 변수를 사용하기 전에 설정되어 있는지 확인하십시오. 당신이 렌더링에서

또한, NinjaAssault 클래스에()

Gdx.gl.glClearColor(1,0,0,1); 
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 
batch.begin(); 

을 제거해야합니다. 그것은 불필요하고

batch.begin() 

를 호출하는 경우 당신은 너무

batch.end() 

를 호출해야하지만,이 경우는 전혀 필요없이 어쨌든 사용되지 않습니다.

+0

응답 해 주셔서 감사합니다. 사실, 난 이미 내 위의 코드와 같이 새로운 텍스처 ("BGASTRO.png)로 제목을 설정했습니다. 내 코드에서 나는 titleWidth 및 titleHeight 변수를 넣어해야 – JAlmazan

+0

을? – JAlmazan

+0

나는 실제로 그것을 내 프로그램에 필요한 조정을하고있다 작동합니다! IronMonkey 도움에 감사드립니다. – JAlmazan