“[AppName] 已停止”在我的物理设备中。 logcat 中的错误:java.lang.NullPointerException
"[AppName] has stopped" in my physical device. ERROR in logcat: java.lang.NullPointerException
我一直在 android 工作室和 libgdx 开发游戏,每当我 运行 在我的物理设备上玩游戏时,它都会显示“[AppName] 已停止”。
我已经检查了我使用的图像的名称(拼写、大小写),除此之外我不知道是什么问题。
这是我在 logcat 中收到的错误:
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 中的代码 class:
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() {
}
}
您尚未对 "title" 进行任何设置。你只是宣布它存在。
标题在构造函数之后调用的 show() 中设置。
你需要做
title = new Texture ("BGASTRO.png");
在尝试获取纹理高度之前。这也适用于所有其他变量,请确保在使用它们之前将它们设置为某些内容。
此外,您应该删除
Gdx.gl.glClearColor(1,0,0,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
来自 NinjaAssault 中的 render() class。这是不必要的,如果你打电话
batch.begin()
你需要打电话给
batch.end()
也是,但在这种情况下,它从未被使用过,所以根本不需要它。
我一直在 android 工作室和 libgdx 开发游戏,每当我 运行 在我的物理设备上玩游戏时,它都会显示“[AppName] 已停止”。
我已经检查了我使用的图像的名称(拼写、大小写),除此之外我不知道是什么问题。
这是我在 logcat 中收到的错误:
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 中的代码 class:
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() {
}
}
您尚未对 "title" 进行任何设置。你只是宣布它存在。 标题在构造函数之后调用的 show() 中设置。 你需要做
title = new Texture ("BGASTRO.png");
在尝试获取纹理高度之前。这也适用于所有其他变量,请确保在使用它们之前将它们设置为某些内容。
此外,您应该删除
Gdx.gl.glClearColor(1,0,0,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
来自 NinjaAssault 中的 render() class。这是不必要的,如果你打电话
batch.begin()
你需要打电话给
batch.end()
也是,但在这种情况下,它从未被使用过,所以根本不需要它。