我正在使用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(“。”) - &gt;没办法:| 我使用的是libGDX版本0.9.9,android版本是2.1.2
答案 0 :(得分:0)
我解决了这个问题...... selectedSkill不是null,我证明了...... 问题是区分大小写。该应用程序崩溃,因为skillName是“Slowmotion”,但.png被命名为skill_slowmotion.png。所以它在桌面应用程序上工作正常,但在android上崩溃。