作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我想通过高分辨率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/
我是一名优秀的程序员,十分优秀!