-2

I'm using libGDX and I got a huge problem with the split method. I'm working on a skillsystem for a game right now in which the player can choose 2 skills at the beginning of the game. The 2 chosen skills are displayed ingame as a button.

So here's my code which works fine for the desktop version but crashes if I reach the code in my android app:

AbstractTextureButton.java

protected void initButton(Skin skin) {
    texture = new Texture(Gdx.files.internal(getTexturePath())); //line 49
    TextureRegion textureRegion = new TextureRegion(texture);
    button = new Button(new Image(textureRegion), skin, "default");
    button.setBounds(getXPos(), getYPos(), button.getWidth(), button.getHeight());
    button.addListener(getInputListener());
}

Skill2Button.java

@Override
protected String getTexturePath() {
    //this string for e.g. is com.blub.skills.Slowmotion or com.blub.skills.Burst
    String selectedSkill = SkillManager.getInstance().getSelectedSkillName2();

    //split it at the .
    String[] splitResult = selectedSkill.split("\\."); // --> CRASH!*

    //get the last element in my string
    String skillName = splitResult[splitResult.length - 1];

    //return texturepath for the selected skill
    return "graphics/skills/skill_" + skillName + ".png";
}

Same crash happens if I'm using lastIndexOf() instead of split().

Logcat log:

10-17 12:20:05.631: E/AndroidRuntime(32132): at com.deinemudda.hoernchenattack.core.ui.buttons.AbstractTextureButton.initButton(AbstractTextureButton.java:49)
10-17 12:20:05.631: E/AndroidRuntime(32132): at com.deinemudda.hoernchenattack.core.ui.buttons.AbstractBaseButton.<init>(AbstractBaseButton.java:33)
10-17 12:20:05.631: E/AndroidRuntime(32132): at com.deinemudda.hoernchenattack.core.ui.buttons.AbstractTextureButton.<init>(AbstractTextureButton.java:31)
10-17 12:20:05.631: E/AndroidRuntime(32132): at com.deinemudda.hoernchenattack.core.ui.buttons.Skill2Button.<init>(Skill2Button.java:29)
10-17 12:20:05.631: E/AndroidRuntime(32132): at com.deinemudda.hoernchenattack.core.ui.screens.GameScreen.create(GameScreen.java:143)
10-17 12:20:05.631: E/AndroidRuntime(32132): at com.deinemudda.hoernchenattack.core.ui.screens.GameScreen.resume(GameScreen.java:347)
10-17 12:20:05.631: E/AndroidRuntime(32132): at com.deinemudda.hoernchenattack.core.Hoernchenattack.setCurrentScreen(Hoernchenattack.java:104)
10-17 12:20:05.631: E/AndroidRuntime(32132): at com.deinemudda.hoernchenattack.core.ui.buttons.LevelButton$1.touchUp(LevelButton.java:50)
10-17 12:20:05.641: E/EmbeddedLogger(593): App crashed! Process: com.deinemudda.hoernchenattack.android
10-17 12:20:05.641: E/EmbeddedLogger(593): App crashed! Package: com.deinemudda.hoernchenattack.android v1 (1.0)

What's the problem? Works perfectly for the desktop application... I tried to use '\.' instead of "\." and I tried to use Pattern.quote(".") as well -> no way :| I'm using the libGDX version 0.9.9 android version is 2.1.2

Geobits
  • 22,218
  • 6
  • 59
  • 103
novas1r1
  • 1,803
  • 21
  • 28
  • 2
    Check if `selectedSkill` is null? – JNL Oct 17 '13 at 14:09
  • You are missing some useful information from the Logcat output. Specifically, the exception being thrown. Most likely its a null pointer exception, as the other comment suggests. – P.T. Oct 17 '13 at 14:52
  • I'd add an extra `if(selectedSkill.contains("XYZ")){ }` to ensure the string contains what you need. – Ben Poulson Oct 22 '13 at 12:28

1 Answers1

0

I solved the problem... selectedSkill wasn't null, I proved that... The problem was case sensitivity. The app crashed because skillName was "Slowmotion", but the .png was named skill_slowmotion.png. So it works fine on the desktop application, but crashes on android.

novas1r1
  • 1,803
  • 21
  • 28