0

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

后来在另一个类中尝试通过创建类加载的对象来使用相同的纹理求解框

  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)
4

2 回答 2

0

检查这一行:

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

因为这是你得到的点NullPointerException。检查 this 上的每个对象的null值并检查导致它的原因null

于 2013-09-06T14:00:34.267 回答
0

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

于 2013-09-06T13:25:16.800 回答