显示使用类2 libgdx中的对象在类1中加载的纹理

时间:2013-09-06 11:29:47

标签: java android textures libgdx

尝试在一个名为load的类中加载纹理,其中包含一些其他加载和显示资源

稍后在另一个类中尝试通过创建类加载的对象来使用相同的Texture solvebox

  public class load{

    @Override
    public void show() {

        try {
            XmlReader reader = new XmlReader();
            Element element = reader.parse(Gdx.files.internal(
                    "data/loadassets.xml").read());.....

            Element help = element.getChildByName("help");...
            solvebox = new Texture(
                    Gdx.files.internal("data/" + help.get("box")));
    }

    ...
    }

现在在另一个类中创建了一个obj并尝试绘制它...

class correct{
....
@Override
    public void render(float delta) {
    load obj=new load();
    batch.begin();
    batch.draw(obj.solvebox, ((w / 2) - (w / 15f)), (h / 3));
    batch.end();
...
    }

但它在这个位置显示NullPointerException,我希望第二次使​​用类加载的对象绘制它

> 09-06 16:56:04.910: E/AndroidRuntime(30523): FATAL EXCEPTION: GLThread
> 26856
>     09-06 16:56:04.910: E/AndroidRuntime(30523): java.lang.NullPointerException
>     09-06 16:56:04.910: E/AndroidRuntime(30523):  at com.badlogic.gdx.graphics.g2d.SpriteBatch.renderMesh(SpriteBatch.java:1042)
>     09-06 16:56:04.910: E/AndroidRuntime(30523):  at com.badlogic.gdx.graphics.g2d.SpriteBatch.end(SpriteBatch.java:267)
>     **
>     
>     > 09-06 16:56:04.910: E/AndroidRuntime(30523):    at
>     > com.invntn.screens.CorrectScreen.render(CorrectScreen.java:86)
>     
>     **
>     09-06 16:56:04.910: E/AndroidRuntime(30523):  at com.badlogic.gdx.Game.render(Game.java:46)
>     09-06 16:56:04.910: E/AndroidRuntime(30523):  at com.invntn.trycolor.TryColor.render(TryColor.java:42)
>     09-06 16:56:04.910: E/AndroidRuntime(30523):  at com.badlogic.gdx.backends.android.AndroidGraphics.onDrawFrame(AndroidGraphics.java:457)
>     09-06 16:56:04.910: E/AndroidRuntime(30523):  at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1516)
>     09-06 16:56:04.910: E/AndroidRuntime(30523):  at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1240)

2 个答案:

答案 0 :(得分:0)

从第二个代码片段看,在使用obj.solvebox之前,您似乎没有调用obj.show()。所以solvebox还没有初始化。

答案 1 :(得分:0)

检查此行:

>     09-06 16:56:04.910: E/AndroidRuntime(30523):  at com.badlogic.gdx.graphics.g2d.SpriteBatch.renderMesh(SpriteBatch.java:1042)

因为这是您获得NullPointerException的地方。检查此处的每个对象是否为null值,并检查导致null

的原因