使用getIdentifier从动态可绘制列表中填充数组失败

时间:2012-12-07 17:57:54

标签: android arrays drawable

正如标题所说,当我尝试填充数组时,我的代码失败了,但它仅适用于一个项目

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    int width=getWidth(this);
    int height=getHeight(this);
    String resolution= new String();
    tamanhos= new String[]{ "240x320","320x480","480x800","800x1280"};
    wallpapers= new String[]{ "fdonegrobrillo"};
    resolution=tamanhos[3]

    mImages=new int[wallpapers.length];
    Resources res= this.getResources();
    for(int i=0;i<wallpapers.length;i++){
    mImages[i]=res.getIdentifier(wallpapers[i]+resolution, "drawable",this.getPackageName());
}

我有所有相应的drawables。当我尝试只有一个项目的应用程序工作,但如果我添加另一项目到壁纸阵列,它会抛出一个错误(致命信号11(SIGSEGV))

我试过像这样的带编码的数组

mImages=new int[]{
        this.getResources().getIdentifier("fdobyn800x1280",
                  "drawable", this.getPackageName())

};

我工作,但当我添加另一个项目

时它是一样的
mImages=new int[]{
        this.getResources().getIdentifier("fdobyn800x1280",
                  "drawable", this.getPackageName()),
        this.getResources().getIdentifier("fdonegrobrillo800x1280",
                          "drawable", this.getPackageName())

};

1 个答案:

答案 0 :(得分:0)

可能在壁纸阵列中没有问题。它的代码工作正常,不会引发任何错误。

package com.example;
    import android.app.Activity;
    import android.content.res.Resources;
    import android.os.Bundle;

    public class MyActivity extends Activity {

        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            //int width=getWidth(this);
            //int height=getHeight(this);
            String resolution= new String();
            String[] tamanhos= new String[]{"240x320","320x480","480x800","800x1280"};
            String[] wallpapers= new String[]{"fdonegrobrillo", "2", "3"};
            resolution=tamanhos[3];

            int[] mImages=new int[wallpapers.length];
            Resources res= this.getResources();
            for(int i=0;i<wallpapers.length;i++){
                mImages[i]=res.getIdentifier(wallpapers[i]+resolution, "drawable",this.getPackageName());
            }
        }
    }