所以我有一个imageview1,当它被点击一个新活动时,它会在下一个活动的新imageview2中显示相同的图像。点击的imageview1由我的mainactivity上的listview访问。列表视图由数组填充,每个列表视图项具有相应的图像。
因此,当单击listview项时,它将启动imageview1所在的新活动。使用xml typedarray填充此imageview,该类型引用可绘制图像。
我无法将图像从第一个图像视图传递到新活动的下一个图像视图。我收到了一个空指针异常,我在下面列出了这个异常。我是一个非常新的编程,我不确定如何解决这个问题。任何帮助都会非常感激。
继承我的routeDetails类,其中包含第一个imageview
public class RouteDetails extends AppCompatActivity {
ImageView routeImage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_route_details);
//TextView for route details
final TextView routeDetailsView = (TextView) findViewById(R.id.routeDetailsView);
routeDetailsView.setText(getIntent().getExtras().getString("route"));
//ImageView for route details
routeImage = (ImageView) findViewById(R.id.routeImage);
routeImage.setImageResource(getIntent().getIntExtra("imageResourceId", 0));
//passing image to fullscreenimage
routeImage.setDrawingCacheEnabled(true);
routeImage.buildDrawingCache();
final Bitmap bitmap = routeImage.getDrawingCache();
////////// Trying to pass image to new activity
routeImage.setOnClickListener(new View.OnClickListener() {
public void onClick(View view)
{
Intent i = new Intent(RouteDetails.this, FullScreenImage.class);
//Bitmap _bitmap; // your bitmap
ByteArrayOutputStream _bs = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 50, _bs);
i.putExtra("byteArray", _bs.toByteArray());
startActivity(i);
}
});
////////////
}
并且我的全屏图像类试图将图像传递给
public class FullScreenImage extends AppCompatActivity {
TouchImageView routeImageFull;
@Override
protected void onCreate(Bundle saveInstanceState){
super.onCreate(saveInstanceState);
setContentView(R.layout.full_screen_image);
routeImageFull = (TouchImageView) findViewById(R.id.fullScreenImageView);
if(getIntent().hasExtra("byteArray")) {
TouchImageView routeImageFull= new TouchImageView(this);
Bitmap bitmap = BitmapFactory.decodeByteArray(
getIntent().getByteArrayExtra("byteArray"),0,getIntent().getByteArrayExtra("byteArray").length);
routeImageFull.setImageBitmap(bitmap);
}
}
}
并且接下来的错误
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.zach.listview, PID: 3429
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null object reference
at com.example.zach.listview.RouteDetails$1.onClick(RouteDetails.java:47)
at android.view.View.performClick(View.java:5610)
at android.view.View$PerformClick.run(View.java:22265)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
答案 0 :(得分:0)
而不是传递整个位图。您只需将您在此处获取的资源ID发送到另一个活动,并且可以通过相同的方式显示图像,您在此处执行此操作。以下是更改后的代码。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_route_details);
//TextView for route details
final TextView routeDetailsView = (TextView)
findViewById(R.id.routeDetailsView);
routeDetailsView.setText(getIntent().getExtras().getString("route"));
//ImageView for route details
routeImage = (ImageView) findViewById(R.id.routeImage);
int mImageResource = getIntent().getIntExtra("imageResourceId", 0);
routeImage.setImageResource(mImageResource);
//passing image to fullscreenimage
routeImage.setDrawingCacheEnabled(true);
routeImage.buildDrawingCache();
final Bitmap bitmap = routeImage.getDrawingCache();
// Trying to pass image to new activity
routeImage.setOnClickListener(new View.OnClickListener() {
public void onClick(View view)
{
Intent i = new Intent(RouteDetails.this,FullScreenImage.class);
i.putExtra("mImageResource", mImageResource);
startActivity(i);
}
});
}