如何在活动之间传递int数组?

时间:2014-02-19 12:26:15

标签: java android arrays

我想在活动之间传递高分辨率drawable ints并在ImageViews上显示,所以我在第一个活动 Examples.class 中将其视为无效:

private void PATHS(int PATH_1, int PATH_2, int PATH_3, int PATH_4, int PATH_5)
{
    int[] xPATHS = {PATH_1,PATH_2, PATH_3, PATH_4, PATH_5};
    Intent intent = new Intent(Examples.this, ExamplesViewer.class);
    intent.putExtra("PATHS", xPATHS);
    startActivity(intent);

}

我在列表监听器中调用它:

    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                PATHS(R.drawable.imageone, R.drawable.imagetwo, R.drawable.imagethree, R.drawable.imagefour, R.drawable.imagefive);
        }
    });
}}

然后我在第二个活动“ExamplesViewer.class”中收到了它:

    Bundle intent = getIntent().getExtras();
    int[] PATHS = intent.getIntArray("PATHS");
    ImageView xImageView_1 = (ImageView) findViewById(R.id.PATH_1);
    ImageView xImageView_2 = (ImageView) findViewById(R.id.PATH_2);
    ImageView xImageView_3 = (ImageView) findViewById(R.id.PATH_3);
    ImageView xImageView_4 = (ImageView) findViewById(R.id.PATH_4);
    ImageView xImageView_5 = (ImageView) findViewById(R.id.PATH_5);


    final BitmapFactory.Options xbm = new BitmapFactory.Options();
    xbm.inSampleSize = 2;

    DisplayMetrics metrics = this.getResources().getDisplayMetrics();
    int width = metrics.widthPixels;
    int height = metrics.heightPixels;

    Bitmap xImage_1 = BitmapFactory.decodeResource(getResources(), PATHS[0], xbm);
    xImageView_1.setImageBitmap(xImage_1);
    xImageView_1.getLayoutParams().height = height;
    xImageView_1.getLayoutParams().width = width;
    Bitmap xImage_2 = BitmapFactory.decodeResource(getResources(), PATHS[1], xbm);
    xImageView_2.setImageBitmap(xImage_2);
    xImageView_2.getLayoutParams().height = height;
    xImageView_2.getLayoutParams().width = width;
    Bitmap xImage_3 = BitmapFactory.decodeResource(getResources(), PATHS[2], xbm);
    xImageView_3.setImageBitmap(xImage_3);
    xImageView_3.getLayoutParams().height = height;
    xImageView_3.getLayoutParams().width = width;
    Bitmap xImage_4 = BitmapFactory.decodeResource(getResources(), PATHS[3], xbm);
    xImageView_4.setImageBitmap(xImage_4);
    xImageView_4.getLayoutParams().height = height;
    xImageView_4.getLayoutParams().width = width;
    Bitmap xImage_5 = BitmapFactory.decodeResource(getResources(), PATHS[4], xbm);
    xImageView_5.setImageBitmap(xImage_5);
    xImageView_5.getLayoutParams().height = height;
    xImageView_5.getLayoutParams().width = width; 

事情就是当我在我的模拟器上运行它时工作正常但是当我在实际设备上运行它时它会带我到我的MainActivity

PS:我试图在几台设备上运行它但仍无法正常工作。

-----------------的修改 -------------

日志cat错误Out of memory on a 8294416-byte allocation. 再加上它不会在仿真器上运行。

2 个答案:

答案 0 :(得分:3)

发送而不是

int[] xPATHS = {PATH_1,PATH_2, PATH_3, PATH_4, PATH_5};
Intent intent = new Intent(Examples.this, ExamplesViewer.class);
intent.putExtra("PATHS", xPATHS);

尝试

int[] xPATHS = {PATH_1,PATH_2, PATH_3, PATH_4, PATH_5};
Intent intent = new Intent(Examples.this, ExamplesViewer.class);
Bundle bundle =new Bundle();
bundle.putIntArray("PATHS", xPATHS);                                 // Change here

而不是

Bundle intent = getIntent().getExtras();
int[] PATHS = intent.getIntArray("PATHS");

Bundle bundle = new Bundle();
int[] PATHS = bundle.getIntArrayExtra("PATHS");   // change here

答案 1 :(得分:0)

这让你烦恼:

intent.putExtra("PATHS", xPATHS);

或者,如果您只有几个整数,则可能需要单独输入路径整数:

intent.putExtra("PATH_1", xPATHS[0]);
intent.putExtra("PATH_2", xPATHS[1]);
intent.putExtra("PATH_3", xPATHS[2]);
intent.putExtra("PATH_4", xPATHS[3]);
intent.putExtra("PATH_5", xPATHS[4]);
祝你好运。