1

完成libGDX android 库的本教程后,桌面项目运行良好,并且 eclipse 指出的任何地方都没有错误。当我在我的 Nexus 7 上运行它时,(运行 Android 4.3)白屏闪烁(可能是应用程序的默认背景),然后它返回主屏幕并显示“不幸的是 my-first-triangle-android 有停了。”

这是我从 LogCat 得到的错误:

08-02 20:11:52.940: E/AndroidRuntime(4095): FATAL EXCEPTION: main
08-02 20:11:52.940: E/AndroidRuntime(4095): java.lang.NoClassDefFoundError: com.test.myfirsttriangle.MyFirstTriangle
08-02 20:11:52.940: E/AndroidRuntime(4095):     at com.example.my_first_triangle_android.MainActivity.onCreate(MainActivity.java:12)
08-02 20:11:52.940: E/AndroidRuntime(4095):     at android.app.Activity.performCreate(Activity.java:5133)
08-02 20:11:52.940: E/AndroidRuntime(4095):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-02 20:11:52.940: E/AndroidRuntime(4095):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
08-02 20:11:52.940: E/AndroidRuntime(4095):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
08-02 20:11:52.940: E/AndroidRuntime(4095):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-02 20:11:52.940: E/AndroidRuntime(4095):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
08-02 20:11:52.940: E/AndroidRuntime(4095):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-02 20:11:52.940: E/AndroidRuntime(4095):     at android.os.Looper.loop(Looper.java:137)
08-02 20:11:52.940: E/AndroidRuntime(4095):     at android.app.ActivityThread.main(ActivityThread.java:5103)
08-02 20:11:52.940: E/AndroidRuntime(4095):     at java.lang.reflect.Method.invokeNative(Native Method)
08-02 20:11:52.940: E/AndroidRuntime(4095):     at java.lang.reflect.Method.invoke(Method.java:525)
08-02 20:11:52.940: E/AndroidRuntime(4095):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-02 20:11:52.940: E/AndroidRuntime(4095):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-02 20:11:52.940: E/AndroidRuntime(4095):     at dalvik.system.NativeStart.main(Native Method)

项目:我的第一个三角形

MyFirstTriangle.java:

package com.test.myfirsttriangle;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Mesh;
import com.badlogic.gdx.graphics.VertexAttribute;
import com.badlogic.gdx.graphics.VertexAttributes.Usage;

public class MyFirstTriangle implements ApplicationListener {
    private Mesh mesh;

    @Override
    public void create() {
        if (mesh == null) {
            mesh = new Mesh(true, 3, 3, 
                    new VertexAttribute(Usage.Position, 3, "a_position"));      

            mesh.setVertices(new float[] { -0.5f, -0.5f, 0,
                                           0.5f, -0.5f, 0,
                                           0, 0.5f, 0 });   
            mesh.setIndices(new short[] { 0, 1, 2 });           
        }
    }

    @Override
    public void dispose() { }

    @Override
    public void pause() { }

    @Override
    public void render() {
        mesh.render(GL10.GL_TRIANGLES, 0, 3);
    }

    @Override
    public void resize(int width, int height) { }

    @Override
    public void resume() { }
}

MyFirstTriangleDesktop.java

package com.test.myfirsttriangle;

import com.badlogic.gdx.backends.jogl.JoglApplication;

public class MyFirstTriangleDesktop {
    public static void main (String[] argv) {
        new JoglApplication(new MyFirstTriangle(), "My First Triangle", 480, 320, false);       
    }
}

项目:我的第一个三角形机器人

MainActivity.java:

package com.example.my_first_triangle_android;

import android.os.Bundle;

import com.badlogic.gdx.backends.android.AndroidApplication;
import com.test.myfirsttriangle.MyFirstTriangle;

public class MainActivity extends AndroidApplication {
    @Override
        public void onCreate (Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                initialize(new MyFirstTriangle(), false);               
        }
}

额外信息:我正在运行这个 Eclipse:http: //developer.android.com/sdk/installing/bundle.html

编辑

似乎来自 log cat 的错误

08-02 20:11:52.940: E/AndroidRuntime(4095): FATAL EXCEPTION: main
08-02 20:11:52.940: E/AndroidRuntime(4095): java.lang.NoClassDefFoundError: com.test.myfirsttriangle.MyFirstTriangle
08-02 20:11:52.940: E/AndroidRuntime(4095):     at com.example.my_first_triangle_android.MainActivity.onCreate(MainActivity.java:12)

这条线有问题:

initialize(new MyFirstTriangle(), false);               
4

2 回答 2

2

检查以确保您的构建路径正确。在最新版本的 Android 中,您需要明确导出您的私人图书馆。

编辑:您很可能需要转到您的属性 => Java 构建路径 => 订购和导出(选项卡),然后选中 Android Private Libraries 框以确保所有 libGDX 类都与您的项目链接。

于 2013-08-03T00:21:18.420 回答
2

从多个项目中构建 Android 应用程序远比应有的复杂得多。基本上,当 ADT 构建您的 Android 包时,它忘记在my-first-triangle构建中包含您的 Libgdx“核心”包 ()。

在 Package Explorer 中右键单击您的 Android 项目,单击Properties,选择Java Build Path. 问题很可能出在Order and Export选项卡上。确保您的my-first-triangle项目被标记为导出(它应该有一个复选标记集)。

这不是 Libgdx 问题。只是 Libgdx 将应用程序分解为多个包,因此比普通 Android 用户更容易被此绊倒。

于 2013-08-03T00:22:22.933 回答