可绘制的ID为零

时间:2017-06-05 03:35:12

标签: java android

<integer-array name="ar_ex_1">
    <item>@drawable/ar_001_main</item>
    <item>@drawable/ar_001_1</item>
    <item>@drawable/ar_001_2</item>
    <item>@drawable/ar_001_3</item>
    <item>@drawable/ar_001_4</item>
    <item>@drawable/ar_001_5</item>
</integer-array>

我有这个整数数组,我使用:

将它加载到Java中
int[] arr = getResources().getIntArray(R.array.ar_ex_1);
for(int i: arr) {
   Log.e("anindya", ""+i);
}

输出结果为:

06-04 20:31:57.045 9360-9360/edu.usc.projecttalent.cognitive E/anindya: 0
06-04 20:31:57.045 9360-9360/edu.usc.projecttalent.cognitive E/anindya: 0
06-04 20:31:57.045 9360-9360/edu.usc.projecttalent.cognitive E/anindya: 0
06-04 20:31:57.045 9360-9360/edu.usc.projecttalent.cognitive E/anindya: 0
06-04 20:31:57.045 9360-9360/edu.usc.projecttalent.cognitive E/anindya: 0
06-04 20:31:57.045 9360-9360/edu.usc.projecttalent.cognitive E/anindya: 0

我期待整数,(drawableR.java s的值,但我只得到零。这不是加载一系列drawable的方法吗?

2 个答案:

答案 0 :(得分:1)

如果这是array.xml中的可绘制数组,请尝试使用此类代码。

 <integer-array name="ar_ex_1">
        <item>@drawable/ar_001_main</item>
        <item>@drawable/ar_001_1</item>
        <item>@drawable/ar_001_2</item>
        <item>@drawable/ar_001_3</item>
        <item>@drawable/ar_001_4</item>
        <item>@drawable/ar_001_5</item>
    </integer-array>

然后在您的活动中,像这样访问它们:

TypedArray imgs = getResources().obtainTypedArray(R.array.ar_ex_1);

// get resource ID by index
imgs.getResourceId(i, -1)

// or set you ImageView's resource to the id
mImgView1.setImageResource(imgs.getResourceId(i, -1));

// recycle the array
imgs.recycle();

答案 1 :(得分:0)

使用时应使用array代替integer-arrayInteger Array是定义的整数数组,它包含原始整数的列表。 array是一系列其他资源,例如drawables。您可以在this

中看到
相关问题