我正在使用 libGDX,但拆分方法遇到了很大的问题。我现在正在为一个游戏开发一个技能系统,玩家可以在游戏开始时选择 2 个技能。选择的 2 个技能在游戏中显示为按钮。
所以这是我的代码,它适用于桌面版本,但如果我在我的 android 应用程序中找到代码,则会崩溃:
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";
}
如果我使用 lastIndexOf() 而不是 split(),也会发生同样的崩溃。
Logcat 日志:
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)
有什么问题?非常适合桌面应用程序...我尝试使用“\。” 代替 ”\。” 我也尝试使用 Pattern.quote(".") -> 没办法:| 我用的是libGDX版本0.9.9 android版本是2.1.2