1

过去 3 个月我没有使用 libgdx,现在我尝试制作一个简单的游戏,但是当我导入 libgdx jar 并在我的手机(Android)上运行该应用程序时,它崩溃了。我在网上发现 ADT 版本处理所有库。因此,我将 Libgdx jar 放在 libs 文件夹中,但应用程序仍然崩溃。

日志猫:

未捕获的处理程序:线程主因未捕获的异常而退出

java.lang.RuntimeException:无法恢复活动 {com.class.path/com.class.path.ExampleActivity}:java.lang.NullPointerException

在此之后,我在 android 类中有 10 个引用(例如:在 android.app.ActivityThread.main ...),比我有这个:

引起:com.badlogic.gdx.backends.android.AndroidApplication.onResume(AndroidApplication.java:223) 处的 java.lang.NullPointerException

在 libgdx 源代码的第 223 行,我发现了这个:

((AndroidInput)getInput()).registerSensorListeners();

示例活动:

公共类 ExampleActivity 扩展 AndroidApplication {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    initialize(new Game(), false);
} 

}

游戏:公共类游戏实现ApplicationListener {

@Override
public void create() {

}

@Override
public void dispose() {
    // TODO Auto-generated method stub

}

@Override
public void pause() {
    // TODO Auto-generated method stub

}

@Override
public void render() {
}

@Override
public void resize(int arg0, int arg1) {
    // TODO Auto-generated method stub

}

@Override
public void resume() {
    // TODO Auto-generated method stub

}

}

PS:对不起我的英语不好

4

1 回答 1

0

我也碰巧遇到了这个问题。并通过使用基于ModelPreiviewApp的代码修复它

private boolean backFaceCulling = true;
private boolean alphaBlending = false;
private float alphaTest = -1;


public void setAlphaBlending(ModelInstance modelInstance, boolean alphaBlendingEnabled){
    if(modelInstance != null){
        if(alphaBlendingEnabled){
            for (Material mat : modelInstance.materials) {
                mat.set(new BlendingAttribute(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA));
            }
        }else{
            for (Material mat : modelInstance.materials) {
                mat.remove(BlendingAttribute.Type);
            }
        }
    }
}

public void setAlphaTest(ModelInstance modelInstance, float alphaTestValue){
    if(modelInstance != null){
        if(alphaTestValue>=0){
            for (Material mat : modelInstance.materials) {
                mat.set(new FloatAttribute(FloatAttribute.AlphaTest, alphaTestValue));
            }
        }else{
            for (Material mat : modelInstance.materials) {
                mat.remove(FloatAttribute.AlphaTest);
            }
        }
    }
}


public void setBackFaceCulling(ModelInstance modelInstance, boolean backFaceCullinEnabled) {

    if (modelInstance != null) {
        if (backFaceCullinEnabled) {
            for (Material mat : modelInstance.materials) {
                mat.remove(IntAttribute.CullFace);
            }
        } else {
            for (Material mat : modelInstance.materials) {
                mat.set(new IntAttribute(IntAttribute.CullFace, 0));
            }
        }
    }
}

public void onModelLoaded(ModelInstance modelInstance) {
    ...
    setBackFaceCulling(modelInstance, true);
    setAlphaBlending(modelInstance, false);
    setAlphaTest(modelInstance, -1);
}
于 2015-08-25T09:01:35.280 回答