我想从xml数组中获取可绘制的ID,但是使用字符串数组,这可能吗?
<array name="yoga0">
<item name="engName">Downward-Facing Dog</item>
<item name="sanName">Adho mukh a savanasana</item>
<item name="pic">@drawable/yoga_adho_mukha_svanasana</item>
<item name="description">foo</item>
<item name="steps">bar</item>
</array>
<array name="yoga1">
<item name="engName">Fire Log Pose</item>
<item name="sanName">Agnistambhasana</item>
<item name="pic">@drawable/yoga_agnistambhasana</item>
<item name="description">bar</item>
<item name="steps">foo</item>
</array>
我想抓住&#34; pic&#34;然后把它放在瑜伽对象中。我认为这就足够了(这只是一个片段,整个代码在获取字符串值之前都有效):
String[] handle;
handle = res.getStringArray(id);
String engName = handle[0]; //Access items in your XML array
String sanName = handle[1];
int pic = Integer.parseInt(handle[2]);
String description = handle[3];
String steps = handle[4];
我知道问题在于摘录,因为:
java.lang.NumberFormatException: Invalid int: "res/drawable-xhdpi-v4/yoga_adho_mukha_svanasana.png"
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
它直接引导我尝试将String解析为整数。有没有办法可以在不制作并行数组的情况下实现这一目标?
编辑:完整方法
private void getYogaArray() {
Resources res = getResources();
TypedArray index = res.obtainTypedArray(R.array.yogas); //call the index array
for (int i = 0; i < index.length(); i++) {
int id = index.getResourceId(i, 0); //get the index
if (id != 0) {
String[] handle;
handle = res.getStringArray(id); //use the index to get the actual array
String engName = handle[0]; //Access items in your XML array
String sanName = handle[1];
int pic = Integer.parseInt(handle[2]);
String description = handle[3];
String steps = handle[4];
//create object and add to array list
yogaArrayList.add(i, new Yoga(engName, sanName, pic, description, steps));
}
}
}
答案 0 :(得分:1)
您应该使用 public class MyApp extends MobileApplication{
private Button button;
private PopupView popupView;
@Override
public void init() {
addViewFactory(HOME_VIEW, () -> {
button = new Button("Click");
button.setOnAction(event -> popupView.show());
popupView = new PopupView(button);
VBox vBox = new VBox();
vBox.getChildren().addAll(new Label("Choice 1"), new Label("Choice 2"), new Label("Choice 3"));
vBox.setSpacing(5);
popupView.setContent(vBox);
return new View(button) {
@Override
protected void updateAppBar(AppBar appBar) {
appBar.setTitleText("PopupView");
}
};
});
}
}
代替obtainTypedArray()
您的代码应该是这样的
getStringArray()
有关TypedArray的更多功能,请参阅documentation
答案 1 :(得分:1)
基于堆栈跟踪,它试图解析字符串值&#34; res / drawable-xhdpi-v4 / yoga_adho_mukha_svanasana.png&#34;到一个显然不是整数的整数。我不认为Resources类正在将@ drawable / yoga_adho_mukha_svanasana扩展为您期望的ID。它正在将其扩展到当前屏幕密度的资源的文件路径。
如果您尝试获取资源ID,可以改为执行此操作。 Omit&#34; @drawable /&#34;来自你的&#34; pic&#34;项目。相反,只需将标识符部分放在这样:
<item name="pic">yoga_agnistambhasana</item>
然后在代码中,将PIC作为字符串读取,然后连接&#34; @drawable /&#34;到它并调用Resources.getIdentifier()将名称转换为如下所示的资源ID:
String pic = handle[2];
int picID = res.getIdentifier("@drawable/" + pic, null, null);
这是一个黑客攻击。如果您只是想加载Drawable,那么您并不需要ID,而Niko的答案看起来可能有效。你想要达到什么目的?