在iOS上运行简单代码时出现异常

时间:2014-02-13 02:26:49

标签: playn

在iOS上运行一个在Java上运行良好的代码时,我遇到以下异常:

Unhandled Exception:
java.lang.IllegalStateException: Attempted to generate texture before GL was initialized. 
You cannot create and render SurfaceLayer in Game.init() on iOS.
2014-02-13 02:12:18.821 ProjectMainJava[19077:21e03] Unhandled managed exception: Attempted to generate texture before GL was initialized. You cannot create and render SurfaceLayer in Game.init() on iOS. (java.lang.IllegalStateException)
  at playn.ios.IOSGLContext.createTexture (Boolean repeatX, Boolean repeatY, Boolean mipmaps) [0x001e7] in IOSGLContext.java:165 
  at playn.ios.IOSGLContext.createTexture (Int32 width, Int32 height, Boolean repeatX, Boolean repeatY, Boolean mm) [0x0000a] in IOSGLContext.java:170 
  at playn.core.gl.SurfaceGL.createTexture () [0x00001] in SurfaceGL.java:63 
  at playn.core.gl.SurfaceGL..ctor (playn.core.gl.GLContext ctx, Single width, Single height) [0x00045] in SurfaceGL.java:34 
  at playn.core.gl.GraphicsGL.createSurfaceGL (Single width, Single height) [0x00001] in GraphicsGL.java:88 
  at playn.core.gl.GraphicsGL.createSurface (Single width, Single height) [0x00001] in GraphicsGL.java:84 
  at ma.company.core.ProjectMainJava.init () [0x0006d] in ProjectMainJava.java:41 
  at playn.ios.IOSPlatform.run (Game game) [0x00009] in IOSPlatform.java:353 
  at playn.core.PlayN.run (Game game) [0x00001] in PlayN.java:47 
  at ma.company.AppDelegate.FinishedLaunching (MonoTouch.UIKit.UIApplication app, MonoTouch.Foundation.NSDictionary options) [0x00000] in <filename unknown>:0 
  at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
  at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0004c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:38 
  at ma.company.Application.Main (System.String[] args) [0x00000] in <filename unknown>:0 

mono-rt:Stacktrace:

我在我的代码中没有使用任何SurfaceLayer(因为它自版本1.7以来已被弃用)。

我的代码是:

@Override
public void init() {

    GroupLayer g = graphics().createGroupLayer();

    graphics().rootLayer().add(g);

    PlayN.log().error("BEFORE");
    SurfaceImage backgroundTopImage = graphics().createSurface(
            graphics().width(), graphics().height() / 2);
    PlayN.log().error("AFTER");

    backgroundTopImage.surface().setFillColor(SKY_COLOR);
    backgroundTopImage.surface().fillRect(0, 0, backgroundTopImage.width(),
            backgroundTopImage.height());

    ImageLayer backgroundTop = graphics().createImageLayer(
            backgroundTopImage);
    g.addAt(backgroundTop, 0, 0);

}

我可以看到“BEFORE”日志,但不能看到After。

PlayN 1.7版。

1 个答案:

答案 0 :(得分:0)

此问题已在此处解决:

https://groups.google.com/forum/#!topic/playn/wnyWiQRaX4Q

解决方案是:

final Image bi = PlayN.assets().getImage("images/BG.png");
AssetWatcher assetWatcher = new AssetWatcher(
    new AssetWatcher.Listener() {

        @Override
        public void done() {
            //Create all the layers here..
        }

        @Override
        public void error(Throwable e) {
            PlayN.log().debug("Interface game menu load error");
        });

    assetWatcher.add(bi);
    assetWatcher.start();