LibGDX 'MyFirstTriangle' example crashes on startup

拈花ヽ惹草 提交于 2019-12-24 11:16:54

问题


Upon finishing this tutorial for the libGDX android library, the desktop project runs fine and there are no errors anywhere that eclipse points out. When I go to run it on my Nexus 7, (running Android 4.3) the white screen flashes (presumably the default background of the app) and then it goes back to the home screen and says "Unfortunately my-first-triangle-android has stopped."

This is the error I get from 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)

Project: my-first-triangle

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);       
    }
}

Project: my-first-triangle-android

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);               
        }
}

Extra info: I am running this eclipse: http://developer.android.com/sdk/installing/bundle.html

Edit

Seems like the error from 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)

has an issue with this line:

initialize(new MyFirstTriangle(), false);               

回答1:


Building Android apps out of multiple projects is far more fiddly than it should be. Basically, when the ADT built your Android package it forgot to include your Libgdx "core" package (my-first-triangle) in the build.

Right-click on your Android Project in the Package Explorer, click Properties, select the Java Build Path. The problem is most likely on the Order and Export tab. Make sure your my-first-triangle project is marked for export (it should have a checkmark set).

This isn't a Libgdx issue. Its just that Libgdx breaks applications into multiple packages so gets tripped up by this more often than regular Android users.




回答2:


Check to make sure your Build Path is correct. In the latest version of Android you need to explicitly export your Private Libraries.

EDIT: More likely than not you need to go to your properties => Java Build Path => Order and Export (tab) then check the box for Android Private Libraries to make sure that all the libGDX classes are linked with your project.



来源:https://stackoverflow.com/questions/18027859/libgdx-myfirsttriangle-example-crashes-on-startup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!