gpt4 book ai didi

java - 如何在 Activity 之间传递 int 数组?

转载 作者:搜寻专家 更新时间:2023-11-01 08:52:28 25 4
gpt4 key购买 nike

我想通过高分辨率drawable ints在 Activity 之间并将其显示在 ImageViews 上所以我在第一个 Activity 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);
}
});
}}

然后我在第二个 Activity “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:我尝试在多个设备上运行它,但仍然无法正常工作。

----------------编辑------------

记录猫错误 Out of memory on a 8294416-byte allocation.此外,它不再适用于模拟器。

最佳答案

虽然发送而不是

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

关于java - 如何在 Activity 之间传递 int 数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21880450/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com