0
我真的把我的头发拉上了这个有趣的问题。我正在做我的加载屏幕。 SmallBox会根据资产经理的负载百分比向右移动。当我尝试为我的小盒子设置X()时,我收到了“FATAL EXCEPTION:GLThread 2967”。我尝试调整大小()但没有运气,同样的错误。我不确定我出错的地方。Actor.setx =致命的例外:GLThread 2967
这里是我的代码:对logcat的
11-01 22:08:54.308: I/awesomegame(24701): LoadingScreen: =-800 getProgress0.0
11-01 22:08:54.318: D/memalloc(24701): /dev/pmem: Unmapping buffer base:0x51f68000 size:10297344 offset:9527296
11-01 22:08:54.328: W/dalvikvm(24701): threadid=11: thread exiting with uncaught exception (group=0x40c2ca68)
11-01 22:08:54.328: E/AndroidRuntime(24701): FATAL EXCEPTION: GLThread 2967
11-01 22:08:54.328: E/AndroidRuntime(24701): java.lang.NullPointerException
11-01 22:08:54.328: E/AndroidRuntime(24701): at com.example.awesomegame.screens.LoadingScreen.render(LoadingScreen.java:128)
11-01 22:08:54.328: E/AndroidRuntime(24701): at com.badlogic.gdx.Game.render(Game.java:46)
11-01 22:08:54.328: E/AndroidRuntime(24701): at com.example.awesomegame.somegame.render(somegame.java:25)
11-01 22:08:54.328: E/AndroidRuntime(24701): at com.badlogic.gdx.backends.android.AndroidGraphics.onDrawFrame(AndroidGraphics.java:487)
11-01 22:08:54.328: E/AndroidRuntime(24701): at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1463)
11-01 22:08:54.328: E/AndroidRuntime(24701): at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1217)
然而
public class LoadingScreen extends AbstractScreen {
private Image bigBox, smallBox;
private Stage stage;
float loadingPercent=0f;
private SpriteBatch batch;
private Texture loadingTexture;
public LoadingScreen(game game) {
super(game);
// load asset here
Gdx.app.log("gamelog", "assetManager.load()");
game.assetManager.load("img/loading.pack", TextureAtlas.class);
game.assetManager.load("img/splash.pack", TextureAtlas.class);
}
@Override
public void show() {
stage = new Stage();
batch = new SpriteBatch();
loadingTexture = new Texture(Gdx.files.internal("img/loadingTexture.png"));
loadingTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
TextureRegion bigBoxTr = new TextureRegion(loadingTexture, 0, 0, 800, 480);
TextureRegion smallBoxTr = new TextureRegion(loadingTexture, 0, 790, 800, 110);
Image bigBox = new Image(bigBoxTr);
Image smallBox = new Image(smallBoxTr);
smallBox.setX(-800);
smallBox.setY(160);
bigBox.setX(0);
bigBox.setY(0);
stage.addActor(bigBox);
stage.addActor(smallBox);
}
@Override
public void render(float delta) {
// Clear the screen
Gdx.gl.glClearColor(0f, 0f, 0f, 1f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
loadingPercent = Interpolation.linear.apply(loadingPercent, game.assetManager.getProgress(), 0.1f);
Gdx.app.log("gamelog", "LoadingScreen: =" + (-800+(int)(loadingPercent*800*2)) + " getProgress" + game.assetManager.getProgress());
smallBox.setX(-800+(int)(loadingPercent*800*2)); //<------ problem point
smallBox.invalidate();
if (game.assetManager.update()) {
Gdx.app.log("gamelog", "LoadingScreen: before calling SplashScreen =" + (-800+(int)(loadingPercent*800*2)) + " getProgress" + game.assetManager.getProgress());
game.setScreen(new SplashScreen(game));
}
stage.act(delta);
stage.draw();
}
错误日志,bigBox显示器正常工作时我注释掉线“smallBox.setX(-800+(INT)(loadingPercent * 800 * 2)); // < ------问题点“。我还以为是浮/ INT问题,然而,根据logcat的(当行注释掉):
11-01 22:03:29.261: I/awesomegame(24283): LoadingScreen: =-800 getProgress0.0
11-01 22:03:29.301: D/memalloc(24283): /dev/pmem: Mapped buffer base:0x52e94000 size:4255744 offset:3485696 fd:67
11-01 22:03:29.301: I/awesomegame(24283): LoadingScreen: =-800 getProgress0.0
11-01 22:03:29.311: D/memalloc(24283): /dev/pmem: Mapped buffer base:0x533c7000 size:5025792 offset:4255744 fd:70
11-01 22:03:29.321: I/awesomegame(24283): LoadingScreen: =-800 getProgress0.0
11-01 22:03:29.331: I/awesomegame(24283): LoadingScreen: =-800 getProgress0.0
...
11-01 22:03:30.362: I/awesomegame(24283): LoadingScreen: =-38 getProgress0.5
11-01 22:03:30.512: I/awesomegame(24283): LoadingScreen: =-34 getProgress0.5
11-01 22:03:30.532: I/awesomegame(24283): LoadingScreen: =-31 getProgress0.5
11-01 22:03:30.532: I/awesomegame(24283): LoadingScreen: =-28 getProgress0.5
11-01 22:03:30.552: I/awesomegame(24283): LoadingScreen: =-25 getProgress0.5
11-01 22:03:30.572: I/awesomegame(24283): LoadingScreen: =-23 getProgress0.5
11-01 22:03:30.572: I/awesomegame(24283): LoadingScreen: =-21 getProgress0.5
11-01 22:03:30.592: I/awesomegame(24283): LoadingScreen: =-19 getProgress0.5
11-01 22:03:30.612: I/awesomegame(24283): LoadingScreen: =-17 getProgress0.5
11-01 22:03:30.612: I/awesomegame(24283): LoadingScreen: =-15 getProgress0.5
11-01 22:03:30.672: I/awesomegame(24283): LoadingScreen: =-14 getProgress0.5
11-01 22:03:30.692: I/awesomegame(24283): LoadingScreen: =-12 getProgress0.5
11-01 22:03:30.872: I/awesomegame(24283): LoadingScreen: =-11 getProgress0.5
11-01 22:03:30.882: I/awesomegame(24283): LoadingScreen: before calling SplashScreen =-11 getProgress1.0
似乎工作公式是正确的。我一直在这个问题上坚持了几天,并不确定在哪里以及如何解决它。
非常感谢,任何帮助,非常感谢。
问候 的Zii
使用你的调试器来发现问题。 'smallBox.setX(-800 +(int)(lo ...'如果'smallBox'或'loadingPercent'为'null'只能抛出'NullPointerException'。'loadingPercent'实际上不能为'null',因为它的因为'smallBox'必须是'null' – noone
谢谢你没有,你说得对,我刚刚测试过,似乎'smallBox'是空的。你有什么建议吗?我应该如何解决这个问题呢?它真的很重要'loadingPercent'是'float'或'Float'?有什么区别? – Ziiiii
由给定的代码判断,'smallBox'不能是'null',它曾经在' show()'然后永远不会改变(至少在我能看到的代码中,所以问题必须在其他地方),是的,它有所不同,'float'不能为'null',而'Float'假设你有'Float foo = null;',那么下面两行将会抛出'NullPointerException':1)'Float bar = foo * 10;'2)'float bar =(float)foo;' – noone