IHazABone
IHazABone

Reputation: 525

Can't figure out this NoClassDefFoundError - class should exist at runtime?

As far as I've always been concerned, or seen in practice, you get that error when a referenced class can't be found at runtime. I'm pretty sure Tap gets created with the initialization of MenuScreen, but I might be doing it wrong. This is my first attempt at a menu, or having multiple screens at all in LibGDX.

This is the first section of the MenuScreen class, which is initialized in AndroidLauncher.java (initalize(new MenuScreen(new Tap()), config);)

public class MenuScreen extends Game implements Screen {

    ...

    Game g;
    public MenuScreen(Game g) {
        create();
        this.g = g;
    }

And the only relevant pieces of Tap.java

public class Tap extends Game implements ActionListener, Screen {  
...  
public Tap() {
    create();
}

I can't seem to figure out why it won't work. Am I missing something obvious? I've tried a few different things, but for the most part I don't have a good enough understanding yet to make any real changes.

Thanks.

EDIT: Stack trace

12-15 22:42:35.614: E/AndroidRuntime(16426): FATAL EXCEPTION: main
12-15 22:42:35.614: E/AndroidRuntime(16426): java.lang.NoClassDefFoundError: com.joelhunter.game.Tap
12-15 22:42:35.614: E/AndroidRuntime(16426):    at com.joelhunter.game.android.AndroidLauncher.onCreate(AndroidLauncher.java:16)
12-15 22:42:35.614: E/AndroidRuntime(16426):    at android.app.Activity.performCreate(Activity.java:5133)
12-15 22:42:35.614: E/AndroidRuntime(16426):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-15 22:42:35.614: E/AndroidRuntime(16426):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2230)
12-15 22:42:35.614: E/AndroidRuntime(16426):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2316)
12-15 22:42:35.614: E/AndroidRuntime(16426):    at android.app.ActivityThread.access$600(ActivityThread.java:150)
12-15 22:42:35.614: E/AndroidRuntime(16426):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1298)
12-15 22:42:35.614: E/AndroidRuntime(16426):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-15 22:42:35.614: E/AndroidRuntime(16426):    at android.os.Looper.loop(Looper.java:213)
12-15 22:42:35.614: E/AndroidRuntime(16426):    at android.app.ActivityThread.main(ActivityThread.java:5225)
12-15 22:42:35.614: E/AndroidRuntime(16426):    at java.lang.reflect.Method.invokeNative(Native Method)
12-15 22:42:35.614: E/AndroidRuntime(16426):    at java.lang.reflect.Method.invoke(Method.java:525)
12-15 22:42:35.614: E/AndroidRuntime(16426):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
12-15 22:42:35.614: E/AndroidRuntime(16426):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
12-15 22:42:35.614: E/AndroidRuntime(16426):    at dalvik.system.NativeStart.main(Native Method)

Upvotes: 0

Views: 84

Answers (2)

Daahrien
Daahrien

Reputation: 10320

You're misunderstanding how Game and Screens are used.

You must only have one Game:

public class Tap extends Game{  
...  
public Tap() {
    setScreen(new MenuScreen(this));
}

And several Screens:

public class MenuScreen implements Screen {

...

Game g;
public MenuScreen(Game g){
    create();
    this.g = g;

    //if touch on "play" button:
    g.setScreen(new GameScreen(this));
}

For example GameScreen:

public class GameScreen implements Screen {

...

Game g;
public GameScreen(Game g){
    create();
    this.g = g;
}

etc, etc.

Also check the core project is added as a dependency to the Android and the Desktop projects.

Right click Android/Desktop -> Java Build Path -> Projects tab, and check if the core project is added. If it isn't, add it.

Upvotes: 3

user207421
user207421

Reputation: 310860

As far as I've always been concerned, or seen in practice, you get that error when a referenced class can't be found at runtime

That's correct, but not because it just isn't there. Usually it means that the class in the file isn't the class that should have been in the file, because either its name or its package disagrees with the class name as suggested by the filename and the directoy it's in.

Upvotes: 1

Related Questions